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.2"
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
110
111
112
113
|
# File 'lib/objectifyhash.rb', line 110
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.
6
7
8
|
# File 'lib/objectifyhash.rb', line 6
def values_to_compare
@values_to_compare
end
|
Instance Method Details
#==(other) ⇒ Object
100
101
102
103
104
105
106
107
|
# File 'lib/objectifyhash.rb', line 100
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
116
117
118
119
120
|
# File 'lib/objectifyhash.rb', line 116
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
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
55
|
# File 'lib/objectifyhash.rb', line 23
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
139
140
141
|
# File 'lib/objectifyhash.rb', line 139
def empty?
self.values_to_compare.empty?
end
|
#eql? ⇒ Object
5
|
# File 'lib/objectifyhash.rb', line 5
alias_method :eql?, :==
|
#get_new_class(name) ⇒ Object
73
74
75
76
77
78
79
|
# File 'lib/objectifyhash.rb', line 73
def get_new_class name
name = "#{name.capitalize}_".to_sym if Object.constants.include? name.capitalize.to_sym
name = name.to_s.sub(/^_/, '').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
94
95
96
97
98
|
# File 'lib/objectifyhash.rb', line 94
def ignore_equal key = nil
@ignore_equal||=[]
@ignore_equal.push key if key
@ignore_equal
end
|
#objectify_array(array, name) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/objectifyhash.rb', line 58
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
82
83
84
85
86
|
# File 'lib/objectifyhash.rb', line 82
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/objectifyhash.rb', line 122
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
88
89
90
91
92
|
# File 'lib/objectifyhash.rb', line 88
def values_at *args
args.map do |key|
self.respond_to?( key ) ? self.method( key ).call : nil
end
end
|