Module: Dirty::Hash::InstanceMethods
- Defined in:
- lib/dirty/hash.rb
Instance Method Summary collapse
- #[](key, mapped = false) ⇒ Object
- #change(key, mapped = false) ⇒ Object
- #changed?(key = nil, mapped = false) ⇒ Boolean (also: #dirty?)
- #changes ⇒ Object
- #clean_up! ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #initialize(constructor = {}, map_methods = false, restricted_keys = nil) ⇒ Object
- #regular_writer(key, value) ⇒ Object
- #replace(other) ⇒ Object
- #was(key, mapped = false) ⇒ Object
Instance Method Details
#[](key, mapped = false) ⇒ Object
43 44 45 46 |
# File 'lib/dirty/hash.rb', line 43 def [](key, mapped = false) validate_read!(key) if mapped || restricted_keys? super(key) end |
#change(key, mapped = false) ⇒ Object
74 75 76 |
# File 'lib/dirty/hash.rb', line 74 def change(key, mapped = false) changes[key] if changed?(key, mapped) end |
#changed?(key = nil, mapped = false) ⇒ Boolean Also known as: dirty?
68 69 70 71 |
# File 'lib/dirty/hash.rb', line 68 def changed?(key = nil, mapped = false) validate_read!(correct_key(key, mapped)) if !key.nil? && (mapped || restricted_keys?) key.nil? ? !changes.empty? : changes.key?(key) end |
#changes ⇒ Object
64 65 66 |
# File 'lib/dirty/hash.rb', line 64 def changes @changes ||= self.class.superclass.new end |
#clean_up! ⇒ Object
82 83 84 85 |
# File 'lib/dirty/hash.rb', line 82 def clean_up! changes.clear nil end |
#clear ⇒ Object
39 40 41 |
# File 'lib/dirty/hash.rb', line 39 def clear keys.each{|key| delete key} end |
#delete(key) ⇒ Object
59 60 61 62 |
# File 'lib/dirty/hash.rb', line 59 def delete(key) self[key] = nil super end |
#initialize(constructor = {}, map_methods = false, restricted_keys = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/dirty/hash.rb', line 20 def initialize(constructor = {}, map_methods = false, restricted_keys = nil) constructor.each do |key, value| self[key] = value end if map_methods extend MethodMap dirty_map! end if restricted_keys restricted_keys.each{|key| self[correct_key(key, map_methods)] ||= nil} @restricted_keys = keys end end |
#regular_writer(key, value) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/dirty/hash.rb', line 48 def regular_writer(key, value) validate_write!(key) original_value = changes.key?(key) ? was(key) : fetch(key, nil) if original_value == value changes.delete key else changes[key] = [original_value, value] end defined?(_store) ? _store(key, value) : super(key, value) end |
#replace(other) ⇒ Object
34 35 36 37 |
# File 'lib/dirty/hash.rb', line 34 def replace(other) clear merge! other end |
#was(key, mapped = false) ⇒ Object
78 79 80 |
# File 'lib/dirty/hash.rb', line 78 def was(key, mapped = false) change(key).first if changed?(key, mapped) end |