Module: ObjectifyHash
- Included in:
- GenericObject
- Defined in:
- lib/objectifyhash.rb,
lib/objectifyhash/version.rb
Defined Under Namespace
Classes: GenericObject
Constant Summary
collapse
- EXCEPTIONS =
{
data: Proc.new do | value | value; end
}
- NULLABLE_KEYS =
[]
- VERSION =
"2.3.0"
Instance Attribute Summary collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
108
109
110
111
|
# File 'lib/objectifyhash.rb', line 108
def method_missing m, *args, &block
super m, *args, &block if args.length > 0 or block_given?
nil
end
|
Instance Attribute Details
#values_to_compare ⇒ Object
Returns the value of attribute values_to_compare.
5
6
7
|
# File 'lib/objectifyhash.rb', line 5
def values_to_compare
@values_to_compare
end
|
Instance Method Details
#==(other) ⇒ Object
98
99
100
101
102
103
104
105
|
# File 'lib/objectifyhash.rb', line 98
def == other
return false unless other.respond_to? :values_to_compare
return false unless self.values_to_compare - other.values_to_compare == [] (self.values_to_compare - ignore_equal ).each do |value|
return false unless self.method(value).() == other.method(value).()
end
return true
end
|
#[](val) ⇒ Object
retro bunker compatibility
114
115
116
117
118
|
# File 'lib/objectifyhash.rb', line 114
def [] val
$stderr.puts 'DEPRECATION WARNING #[] CALLED ON OBJECT'
raise NameError unless self.respond_to?( val.to_sym )
self.method( val ).call
end
|
#convert_and_define(hash) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/objectifyhash.rb', line 22
def convert_and_define hash
@original_hash = hash
@values_to_compare||=[]
@original_hash.each do |key, value|
key = 'klass' if key == 'class'
key = key.to_sym
case
when key == :id
set :guid, value
when key == :method
set :verb, value
when EXCEPTIONS.key?( key )
set key, EXCEPTIONS[ key ].call( value )
when value.is_a?(Hash)
klass = get_new_class key
object = klass.new( value )
set key, object
when value.is_a?(Array)
set key, objectify_array(value, key)
else
set key, value
end
end
NULLABLE_KEYS.each do |key|
unless self.respond_to?( key )
set key, nil
end
end
end
|
#empty? ⇒ Boolean
137
138
139
|
# File 'lib/objectifyhash.rb', line 137
def empty?
self.values_to_compare.empty?
end
|
#eql? ⇒ Object
4
|
# File 'lib/objectifyhash.rb', line 4
alias_method :eql?, :==
|
#get_new_class(name) ⇒ Object
72
73
74
75
76
77
|
# File 'lib/objectifyhash.rb', line 72
def get_new_class name
name = "#{name.capitalize}_".to_sym if Object.constants.include? name.capitalize.to_sym
self.class.const_defined?(name.capitalize) ?
self.class.const_get(name.capitalize) :
self.class.const_set(name.capitalize, ObjectifyHash::GenericObject)
end
|
#ignore_equal(key = nil) ⇒ Object
92
93
94
95
96
|
# File 'lib/objectifyhash.rb', line 92
def ignore_equal key = nil
@ignore_equal||=[]
@ignore_equal.push key if key
@ignore_equal
end
|
#objectify_array(array, name) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/objectifyhash.rb', line 57
def objectify_array array,name
array.map do |elem|
if EXCEPTIONS.key? name
EXCEPTIONS[ name ].call( elem )
else
if elem.is_a? Hash
klass = get_new_class name.to_s.sub(/s$/, '').to_sym
klass.new elem
else
elem
end
end
end
end
|
#set(key, value) ⇒ Object
80
81
82
83
84
|
# File 'lib/objectifyhash.rb', line 80
def set key, value
method_name = Object.instance_methods.include?( key ) ? :"_#{key}_" : key.to_sym
@values_to_compare.push method_name
self.__send__(:define_singleton_method, method_name, Proc.new do value; end )
end
|
#to_h ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/objectifyhash.rb', line 120
def to_h
h = {}
values_to_compare.each do |m|
if self.method( m ).().nil? and NULLABLE_KEYS.include?( m )
next
end
if self.method( m ).().respond_to? :values_to_compare
h[ m.to_sym ] = self.method( m ).().to_h
elsif self.method( m ).().is_a? Array
h[ m.to_sym ] = un_objectify_array( self.method( m ).() )
else
h[ m.to_sym ] = self.method( m ).()
end
end
return h
end
|
#values_at(*args) ⇒ Object
86
87
88
89
90
|
# File 'lib/objectifyhash.rb', line 86
def values_at *args
args.map do |key|
self.respond_to?( key ) ? self.method( key ).call : nil
end
end
|