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

#initialize(data = {}) ⇒ OpenOption

Returns a new instance of OpenOption



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

#method_missing(name, *args) ⇒ Object

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

#_dataObject

def initialize



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

def _data() @data end

Instance Method Details

#==(other) ⇒ Object



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

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



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

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

#_merge(*args) ⇒ Object



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

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

#_merge!(*args) ⇒ Object



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

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

#_replace(data) ⇒ Object



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

def _replace data
	@data = data
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#hashObject



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

def hash() @data.hash end

#inspectObject 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

#marshal_dumpObject



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

def marshal_dump 
	@data
end

#marshal_load(data) ⇒ Object



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

def marshal_load data
	@data = data
end