Class: Sensi::HashToObject
- Inherits:
-
Object
- Object
- Sensi::HashToObject
show all
- Defined in:
- lib/sensi/hash_to_object.rb
Constant Summary
collapse
- TEST =
{'C': 'abc', 'A': ["hi", {:should=>"see"}], 'M': ['on','off','shutdown'], 'D': {'hola': 'hello'}, num: 5}
Instance Method Summary
collapse
Constructor Details
#initialize(hash = TEST, debug = false, json = nil) ⇒ HashToObject
Returns a new instance of HashToObject.
10
11
12
|
# File 'lib/sensi/hash_to_object.rb', line 10
def initialize(hash = TEST, debug = false, json = nil)
convert(hash, debug)
end
|
Instance Method Details
#add(k, v) ⇒ Object
57
58
59
60
61
|
# File 'lib/sensi/hash_to_object.rb', line 57
def add(k, v)
self.instance_variable_set("@#{k.underscore}", v)
self.class.send(:define_method, k.underscore, proc{self.instance_variable_get("@#{k.underscore}")})
self.class.send(:define_method, "#{k.underscore}=", proc{|v| self.instance_variable_set("@#{k.underscore}", v)})
end
|
#add_var(k, v) ⇒ Object
63
64
65
66
67
68
69
70
|
# File 'lib/sensi/hash_to_object.rb', line 63
def add_var(k, v)
(class << self; self; end).class_eval do
define_method( k.underscore, proc{ instance_variable_get("@#{k.underscore}") } )
define_method( "#{k.underscore}=", proc{ |v| instance_variable_set("@#{k.underscore}", v) } )
end
self.instance_variable_set("@#{k.underscore}", v)
end
|
#contains_hash(arr) ⇒ Object
50
51
52
53
54
55
|
# File 'lib/sensi/hash_to_object.rb', line 50
def contains_hash(arr)
arr.each do |item|
return true if item.is_a? Hash
end
false
end
|
#convert(obj, debug = false) ⇒ Object
14
15
16
17
18
19
20
21
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
|
# File 'lib/sensi/hash_to_object.rb', line 14
def convert(obj, debug = false)
obj.each do |obj|
puts "#{obj}" if debug
if obj.is_a? Array
puts 'found array' if debug
if obj.size == 2 and !obj[1].is_a? Hash and !obj[1].is_a? Array
puts 'found key/value' if debug
add_var(obj[0].to_s, obj[1])
elsif obj.size == 2 and obj[1].is_a? Hash
puts 'found hash' if debug
add_var(obj[0].to_s, HashToObject.new(obj[1]))
elsif obj.size == 2 and obj[1].is_a? Array and contains_hash(obj[1])
puts 'found array and contains hash' if debug
add_var(obj[0].to_s, HashToObject.new(obj[1]))
elsif obj.size == 2 and obj[1].is_a? Array
puts 'found array and array' if debug
add_var(obj[0].to_s, obj[1])
elsif obj.size > 2
puts 'found other' if debug
obj.each do |item|
add_var(k.to_s, HashToObject.new(item))
end
end
elsif obj.is_a? Hash
puts 'found hash 2' if debug
obj.each do |k, v|
if v.is_a? Array or v.is_a? Hash
add_var(k.to_s, HashToObject.new(v))
else
add_var(k.to_s,v)
end
end
end
end
end
|
#from_json!(string) ⇒ Object
84
85
86
87
88
|
# File 'lib/sensi/hash_to_object.rb', line 84
def from_json!(string)
JSON.load(string).each do |var, val|
self.instance_variable_set var, val
end
end
|
#to_json ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/sensi/hash_to_object.rb', line 72
def to_json
hash = {}
self.instance_variables.each do |var|
if var.is_a? HashToObject
hash[var] = self.instance_variable_get(var).to_json
else
hash[var] = self.instance_variable_get(var)
end
end
hash.to_json
end
|