Class: DocumentHash::Core
- Inherits:
-
Hash
- Object
- Hash
- DocumentHash::Core
- Defined in:
- lib/document_hash/core.rb,
lib/document_hash/callbacks.rb
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #after_change(&block) ⇒ Object
- #before_change(&block) ⇒ Object
- #changed ⇒ Object
- #changed? ⇒ Boolean
- #merge(other) ⇒ Object
- #merge!(other) ⇒ Object
- #reset! ⇒ Object
- #to_hash ⇒ Object
- #touch! ⇒ Object
Class Method Details
.[](hash, parent = nil, parent_key = nil) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/document_hash/core.rb', line 3 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 #symbolize_keys new end end |
Instance Method Details
#[](key) ⇒ Object
26 27 28 |
# File 'lib/document_hash/core.rb', line 26 def [] key super key.to_sym end |
#[]=(key, val) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/document_hash/core.rb', line 30 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
18 19 20 |
# File 'lib/document_hash/core.rb', line 18 def changed changed_attributes.dup.freeze end |
#changed? ⇒ Boolean
22 23 24 |
# File 'lib/document_hash/core.rb', line 22 def changed? ! changed.empty? end |
#merge(other) ⇒ Object
58 59 60 |
# File 'lib/document_hash/core.rb', line 58 def merge other dup.merge! other end |
#merge!(other) ⇒ Object
62 63 64 65 66 |
# File 'lib/document_hash/core.rb', line 62 def merge! other other.each { |k, v| self[k] = v } self end |
#reset! ⇒ Object
42 43 44 45 46 |
# File 'lib/document_hash/core.rb', line 42 def reset! changed_attributes.clear values.select{|v| v.is_a? self.class }.each{ |v| v.reset! } end |
#to_hash ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/document_hash/core.rb', line 68 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
48 49 50 51 52 53 54 55 56 |
# File 'lib/document_hash/core.rb', line 48 def touch! self.each do |key, value| if value.is_a? self.class value.touch! else changed_key key,value end end end |