Class: CouchRest::Model::CastedHash

Inherits:
Hash
  • Object
show all
Includes:
CastedBy, Dirty
Defined in:
lib/couchrest/model/casted_hash.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dirty

#couchrest_attribute_will_change!, #couchrest_parent_will_change!, #use_dirty?

Methods included from CastedBy

#base_doc, #base_doc?

Methods inherited from Hash

===

Instance Attribute Details

#casted_by_propertyObject

Returns the value of attribute casted_by_property.



8
9
10
# File 'lib/couchrest/model/casted_hash.rb', line 8

def casted_by_property
  @casted_by_property
end

Class Method Details

.[](hash, property, parent = nil) ⇒ Object



10
11
12
13
14
15
# File 'lib/couchrest/model/casted_hash.rb', line 10

def self.[](hash, property, parent = nil)
  obj = super(hash)
  obj.casted_by_property = property
  obj.casted_by = parent unless parent.nil?
  obj
end

Instance Method Details

#[]=(key, obj) ⇒ Object



22
23
24
25
# File 'lib/couchrest/model/casted_hash.rb', line 22

def []= key, obj
  couchrest_attribute_will_change!(key) if use_dirty? && obj != self[key]
  super(key, obj)
end

#attributesObject

needed for dirty



18
19
20
# File 'lib/couchrest/model/casted_hash.rb', line 18

def attributes
  self
end

#clearObject



59
60
61
62
# File 'lib/couchrest/model/casted_hash.rb', line 59

def clear
  self.keys.each { |key| couchrest_attribute_will_change!(key) } if use_dirty?
  super
end

#delete(key) ⇒ Object



27
28
29
30
# File 'lib/couchrest/model/casted_hash.rb', line 27

def delete(key)
  couchrest_attribute_will_change!(key) if use_dirty? && include?(key)
  super(key)
end

#delete_ifObject



64
65
66
67
68
69
70
71
# File 'lib/couchrest/model/casted_hash.rb', line 64

def delete_if
  if use_dirty? && block_given?
    self.keys.each do |key|
      couchrest_attribute_will_change!(key) if yield key, self[key]
    end
  end
  super
end

#keep_ifObject

ruby 1.9



74
75
76
77
78
79
80
81
# File 'lib/couchrest/model/casted_hash.rb', line 74

def keep_if
  if use_dirty? && block_given?
    self.keys.each do |key|
      couchrest_attribute_will_change!(key) if !yield key, self[key]
    end
  end
  super
end

#merge!(other_hash) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/couchrest/model/casted_hash.rb', line 32

def merge!(other_hash)
  if use_dirty? && other_hash && other_hash.kind_of?(Hash)
    other_hash.keys.each do |key|
      if self[key] != other_hash[key] || !include?(key)
        couchrest_attribute_will_change!(key)
      end
    end
  end
  super(other_hash)
end

#replace(other_hash) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/couchrest/model/casted_hash.rb', line 43

def replace(other_hash)
  if use_dirty? && other_hash && other_hash.kind_of?(Hash)
    # new keys and changed keys
    other_hash.keys.each do |key|
      if self[key] != other_hash[key] || !include?(key) 
        couchrest_attribute_will_change!(key) 
      end
    end
    # old keys
    old_keys = self.keys.reject { |key| other_hash.include?(key) }
    old_keys.each { |key| couchrest_attribute_will_change!(key) }
  end

  super(other_hash)
end