Class: DocumentHash::Core
- Inherits:
-
Hash
- Object
- Hash
- DocumentHash::Core
show all
- Defined in:
- lib/document_hash/core.rb,
lib/document_hash/callbacks.rb
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
33
34
35
36
37
38
39
|
# File 'lib/document_hash/core.rb', line 33
def method_missing method, *args
if method =~ /^(.*)=$/
self.__send__(:[]=, method[0..-2], args.pop)
else
self.__send__(:[], method) || DocumentHash::Nil.new(self, [method])
end
end
|
Class Method Details
.[](hash, parent = nil, parent_key = nil) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/document_hash/core.rb', line 41
def self.[] hash, parent = nil, parent_key = nil
super(hash).tap do|new|
new.__send__ :parent=, parent if parent
new.__send__ :parent_key=, parent_key if parent_key
new.keys.each do |k|
if new[k].is_a?(Hash) && ! new[k].is_a?(self.class)
new[k] = new.class[new.delete(k)]
else
new[k] = new.delete(k)
end
end
end
end
|
.symbolize_keys(hash) ⇒ Object
155
156
157
158
159
|
# File 'lib/document_hash/core.rb', line 155
def self.symbolize_keys hash
hash.keys.each do |key|
hash[(key.to_sym rescue key) || key] = hash.delete(key)
end
end
|
Instance Method Details
#[](key) ⇒ Object
64
65
66
|
# File 'lib/document_hash/core.rb', line 64
def [] key
super key.to_sym
end
|
#[]=(key, val) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/document_hash/core.rb', line 68
def []= key, val
key = key.to_sym
if val.is_a? Hash
val = self.class[val, self, key]
end
val = execute_before_change_callback key,val
super key, val
changed_key key, val
end
|
#after_change(&block) ⇒ Object
3
4
5
|
# File 'lib/document_hash/callbacks.rb', line 3
def after_change &block
@after_change = block
end
|
#before_change(&block) ⇒ Object
7
8
9
|
# File 'lib/document_hash/callbacks.rb', line 7
def before_change &block
@before_change = block
end
|
#changed ⇒ Object
56
57
58
|
# File 'lib/document_hash/core.rb', line 56
def changed
changed_attributes.dup.freeze
end
|
#changed? ⇒ Boolean
60
61
62
|
# File 'lib/document_hash/core.rb', line 60
def changed?
! changed.empty?
end
|
#merge(other) ⇒ Object
96
97
98
|
# File 'lib/document_hash/core.rb', line 96
def merge other
dup.merge! other
end
|
#merge!(other) ⇒ Object
100
101
102
103
104
|
# File 'lib/document_hash/core.rb', line 100
def merge! other
other.each { |k, v| self[k] = v }
self
end
|
#reset! ⇒ Object
80
81
82
83
84
|
# File 'lib/document_hash/core.rb', line 80
def reset!
changed_attributes.clear
values.select{|v| v.is_a? self.class }.each{ |v| v.reset! }
end
|
#to_hash ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/document_hash/core.rb', line 106
def to_hash
Hash[
self.collect do |k,v|
if v.is_a? DocumentHash::Core
[ k, v.to_hash ]
else
[ k, v ]
end
end
]
end
|
#touch! ⇒ Object
86
87
88
89
90
91
92
93
94
|
# File 'lib/document_hash/core.rb', line 86
def touch!
self.each do |key, value|
if value.is_a? self.class
value.touch!
else
changed_key key,value
end
end
end
|