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

new(data={})


data’s key can be string or symbol, but internal store key is use symbol.

data = { “a” => 1 }

same as

data = { :a => 1 }

it is a deep convertion of Hash.

a = { a: 1 } o = OpenOption.new(a) o #=> <#OpenOption a: <#OpenOption b:1> > # so you can access b by o.a.b

inheritance


class Foo < OpenOption def initialize data={} super @data = data.to_i end end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ OpenOption

Returns a new instance of OpenOption.



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

def initialize(data={})
	@data = OpenOption.convert_hash(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object

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



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tagen/core/open_option.rb', line 103

def method_missing(name, *args, &blk) 
	if name =~ /^_(.*)/
		return @data.send($1.to_sym, *args, &blk)
	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

#_dataObject

def initialize



96
97
98
# File 'lib/tagen/core/open_option.rb', line 96

def _data
  @_data
end

Class Method Details

.convert_hash(data, ret = {}) ⇒ Object

I’m rescurive deep convert hash to OpenOption



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tagen/core/open_option.rb', line 76

def convert_hash data, ret={}
	data.each do |k,v|
		if Hash === v
			new_v = self.new(v)
			ret[k.to_sym] = new_v
			convert_hash(data[k], ret[k.to_sym])
		else
			ret[k.to_sym] = v
		end
	end
	ret
end

Instance Method Details

#==(other) ⇒ Object



123
124
125
126
# File 'lib/tagen/core/open_option.rb', line 123

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

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



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

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

#_merge(*args) ⇒ Object



152
153
154
# File 'lib/tagen/core/open_option.rb', line 152

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

#_merge!(*args) ⇒ Object



156
157
158
159
# File 'lib/tagen/core/open_option.rb', line 156

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

#_replace(data) ⇒ Object



148
149
150
# File 'lib/tagen/core/open_option.rb', line 148

def _replace data
	@data = data
end

#dupObject



136
137
138
# File 'lib/tagen/core/open_option.rb', line 136

def dup
	@data.dup
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#hashObject



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

def hash() @data.hash end

#inspectObject Also known as: to_s



140
141
142
143
144
# File 'lib/tagen/core/open_option.rb', line 140

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

#marshal_dumpObject



115
116
117
# File 'lib/tagen/core/open_option.rb', line 115

def marshal_dump 
	@data
end

#marshal_load(data) ⇒ Object



119
120
121
# File 'lib/tagen/core/open_option.rb', line 119

def marshal_load data
	@data = data
end