Class: OpenOption

Inherits:
Object show all
Defined in:
lib/tagen/core/open_option.rb

Overview

main purpose of this Class is provide an option support.

Example: o = OpenOption.new o.name = 'alice' p o.name #=> 'alice'

o.force = true p o.force? #=> true

OverView:

o = OpenOption.new

define value, they are all the same

o.key = value o = value o = value

access value, they are all the same

o o o.key o.key?

access hash method, some are special

o._keys #=> [:a] o._merge(a: 1) #=> a new <#OpenOption a: 1> o._merge!(a: 1) #=> self

access data

o._data #=> 1

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (OpenOption) initialize(data = {})



47
48
49
# File 'lib/tagen/core/open_option.rb', line 47

def initialize(data={})
	@data = data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(name, *args)

method return value _method goes to Hash method? return !!value method= define a new key



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tagen/core/open_option.rb', line 58

def method_missing(name, *args) 
	if name =~ /^_(.*)/
		return @data.send($1.to_sym, *args)
	elsif name =~ /(.*)\?$/
		return !!@data[$1.to_sym]
	elsif name =~ /(.*)=$/
		@data[$1.to_sym] = args[0]
	else
		return @data[name.to_sym]
	end
end

Instance Attribute Details

- (Object) _data

def initialize



51
# File 'lib/tagen/core/open_option.rb', line 51

def _data() @data end

Instance Method Details

- (Object) ==(other)



78
79
80
81
# File 'lib/tagen/core/open_option.rb', line 78

def ==(other)
	return false unless other.kind_of?(self.class)
	@data == other._data
end

- (Object) [](key)



87
# File 'lib/tagen/core/open_option.rb', line 87

def [](key) @data[key.to_sym] end

- (Object) []=(key, value)



85
# File 'lib/tagen/core/open_option.rb', line 85

def []=(key, value) @data[key.to_sym] = value end

- (Object) _merge(*args)



103
104
105
# File 'lib/tagen/core/open_option.rb', line 103

def _merge *args
	self.class.new @data.merge(*args)
end

- (Object) _merge!(*args)



107
108
109
110
# File 'lib/tagen/core/open_option.rb', line 107

def _merge! *args
	@data.merge! *args
	return self
end

- (Object) _replace(data)



99
100
101
# File 'lib/tagen/core/open_option.rb', line 99

def _replace data
	@data = data
end

- (Boolean) eql?(other)



83
# File 'lib/tagen/core/open_option.rb', line 83

def eql?(other) @data == other._data end

- (Object) hash



89
# File 'lib/tagen/core/open_option.rb', line 89

def hash() @data.hash end

- (Object) inspect Also known as: to_s



91
92
93
94
95
# File 'lib/tagen/core/open_option.rb', line 91

def inspect
	out = "#<#{self.class} "
	out << @data.map{|k,v| "#{k}: #{v.inspect}"}.join(', ')
	out << ">"
end

- (Object) marshal_dump



70
71
72
# File 'lib/tagen/core/open_option.rb', line 70

def marshal_dump 
	@data
end

- (Object) marshal_load(data)



74
75
76
# File 'lib/tagen/core/open_option.rb', line 74

def marshal_load data
	@data = data
end