Class: DocumentHash::Core

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](*attr) ⇒ Object



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

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

    symbolize_keys new
  end
end

Instance Method Details

#[](key) ⇒ Object



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

def [] key
  super key.to_sym
end

#[]=(key, val) ⇒ Object



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

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

  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

#changedObject



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

def changed
  changed_attributes.dup.freeze
end

#changed?Boolean

Returns:

  • (Boolean)


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

def changed? 
  ! changed.empty?
end

#merge(other) ⇒ Object



45
46
47
48
# File 'lib/document_hash/core.rb', line 45

def merge other
  self.class.symbolize_keys other
  super other
end

#merge!(other) ⇒ Object



50
51
52
53
# File 'lib/document_hash/core.rb', line 50

def merge! other
  self.class.symbolize_keys other
  super other
end

#reset!Object



39
40
41
42
43
# File 'lib/document_hash/core.rb', line 39

def reset!
  changed_attributes.clear

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