Module: ObjHash
- Includes:
- Enumerable
- Included in:
- Gwtf::Item
- Defined in:
- lib/objhash.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
simple read from the class:
>> i.description
=> "Sample Item"
method like writes:
>> i.description "This is a test"
=> "This is a test"
assignment
>> i.description = "This is a test"
=> "This is a test"
boolean
>> i.description?
=> false
>> i.description "foo"
=> foo
>> i.has_description?
=> true
>> i.has_description
=> true
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/objhash.rb', line 201
def method_missing(method, *args)
method = method.to_s
if include?(method)
if args.empty?
return objhash_values[method]
else
return update_property(method, args.first)
end
elsif method =~ /^(has_)*(.+?)\?$/
return !!objhash_values[$2]
elsif method =~ /^(.+)=$/
property = $1
return update_property(property, args.first) if include?(property)
end
raise NameError, "undefined local variable or method `#{method}'"
end
|
Class Method Details
.included(base) ⇒ Object
36
37
38
|
# File 'lib/objhash.rb', line 36
def self.included(base)
base.extend ClassMethods
end
|
Instance Method Details
#[](property) ⇒ Object
151
152
153
154
155
|
# File 'lib/objhash.rb', line 151
def [](property)
raise "No such property: #{property}" unless include?(property)
objhash_values[property.to_s]
end
|
#[]=(property, value) ⇒ Object
157
158
159
|
# File 'lib/objhash.rb', line 157
def []=(property, value)
update_property(property, value)
end
|
#default_property_value(property) ⇒ Object
40
41
42
|
# File 'lib/objhash.rb', line 40
def default_property_value(property)
self.class.objhash_default_value(property)
end
|
#each ⇒ Object
145
146
147
148
149
|
# File 'lib/objhash.rb', line 145
def each
objhash_values.keys.sort.each do |property|
yield [property, objhash_values[property]]
end
end
|
#include?(property) ⇒ Boolean
60
61
62
|
# File 'lib/objhash.rb', line 60
def include?(property)
objhash_config.include?(property.to_s)
end
|
#merge(hsh) ⇒ Object
161
162
163
|
# File 'lib/objhash.rb', line 161
def merge(hsh)
objhash_values.merge(hsh)
end
|
#merge!(hsh) ⇒ Object
165
166
167
168
169
170
171
172
173
174
|
# File 'lib/objhash.rb', line 165
def merge!(hsh)
raise TypeError, "Can't convert #{hsh.class} into Hash" unless hsh.respond_to?(:to_hash)
objhash_values.keys.each do |k|
next if ["edited_at", "created_at"].include?(k)
update_property(k, hsh[k]) if hsh.include?(k)
end
self
end
|
#objhash_config ⇒ Object
44
45
46
|
# File 'lib/objhash.rb', line 44
def objhash_config
self.class.objhash_config
end
|
#objhash_values ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/objhash.rb', line 48
def objhash_values
return @objhash_values if @objhash_values
@objhash_values = {}
objhash_config.each_pair do |property, args|
update_property(property, default_property_value(property))
end
@objhash_values
end
|
#to_hash ⇒ Object
129
130
131
|
# File 'lib/objhash.rb', line 129
def to_hash
objhash_values
end
|
#to_json ⇒ Object
137
138
139
|
# File 'lib/objhash.rb', line 137
def to_json
objhash_values.to_json
end
|
#to_s ⇒ Object
133
134
135
|
# File 'lib/objhash.rb', line 133
def to_s
objhash_values.inspect
end
|
#to_yaml ⇒ Object
141
142
143
|
# File 'lib/objhash.rb', line 141
def to_yaml
objhash_values.to_yaml
end
|
#update_property(property, value) ⇒ Object
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/objhash.rb', line 64
def update_property(property, value)
property = property.to_s
raise "Unknown property #{property}" unless include?(property)
validate_property(property, value)
objhash_values[property] = value
objhash_values["edited_at"] = Time.now
end
|
#validate_property(property, value) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/objhash.rb', line 75
def validate_property(property, value)
raise "Unknown property #{property}" unless include?(property)
validation = objhash_config[property.to_s][:validation]
return true if validation.nil?
return true if value == objhash_config[property.to_s][:default]
raise "#{property} should be #{validation}" if value.nil? && !validation.nil?
if validation.is_a?(Symbol)
case validation
when :boolean
raise "#{property} should be a boolean" unless [TrueClass, FalseClass].include?(value.class)
when :ipv6
begin
require 'ipaddr'
ip = IPAddr.new(value)
raise "#{property} should be a valid IPv6 address" unless ip.ipv6?
rescue
raise "#{property} should be a valid IPv6 address"
end
when :ipv4
begin
require 'ipaddr'
ip = IPAddr.new(value)
raise "#{property} should be a valid IPv4 address" unless ip.ipv4?
rescue
raise "#{property} should be a valid IPv4 address"
end
else
raise "Don't know how to validate #{property} using #{validation}"
end
elsif validation.is_a?(Array)
raise "%s should be one of %s" % [property, validation.join(", ")] unless validation.include?(value)
elsif validation.is_a?(Regexp)
raise "#{property} should match #{validation}" unless value.match(validation)
elsif validation.is_a?(Proc)
raise "#{property} does not validate against lambda" unless validation.call(value)
else
raise "#{property} is a #{value.class} should be a #{validation}" unless value.is_a?(validation)
end
return true
end
|