Class: DocumentHash::Core

Inherits:
Hash
  • Object
show all
Defined in:
lib/document_hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](*attr) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/document_hash.rb', line 5

def self.[] *attr
  super(*attr).tap do|new|
    new.each do |k,v|
      p v.class
      new[k] = new.class[v] if v.is_a?(Hash) && ! v.is_a?(self.class)
    end
  end
end

Instance Method Details

#[](key) ⇒ Object



22
23
24
# File 'lib/document_hash.rb', line 22

def [] key
  super key.to_sym
end

#[]=(key, val) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/document_hash.rb', line 26

def []= key, val
  key = key.to_sym

  if val.is_a? Hash
    val = self.class[val] 
    val.__send__ :parent=, self;
    val.__send__ :parent_key=, key;
  end

  super key, val
  changed_key key

  parent.__send__ :changed_key, parent_key if parent
end

#changedObject



14
15
16
# File 'lib/document_hash.rb', line 14

def changed
  changed_attributes.dup.freeze
end

#changed?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/document_hash.rb', line 18

def changed? 
  ! changed.empty?
end

#reset!Object



41
42
43
44
45
# File 'lib/document_hash.rb', line 41

def reset!
  changed_attributes.clear

  values.select{|v| v.is_a? self.class }.each{ |v| v.reset! }
end