Module: Volt::ModelHashBehaviour
- Included in:
- Model
- Defined in:
- lib/volt/models/model_hash_behaviour.rb
Overview
Contains all of the methods on a model that make it behave like a hash. Moving this into a module cleans up the main Model class for things that make it behave like a model.
Instance Method Summary collapse
- #clear ⇒ Object
- #delete(name) ⇒ Object
- #each_with_object(*args, &block) ⇒ Object
- #empty? ⇒ Boolean
- #false? ⇒ Boolean
- #keys ⇒ Object
- #nil? ⇒ Boolean
- #size ⇒ Object
-
#to_h ⇒ Object
Convert the model to a hash all of the way down.
- #true? ⇒ Boolean
Instance Method Details
#clear ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/volt/models/model_hash_behaviour.rb', line 46 def clear @attributes.each_pair do |key, value| @deps.changed!(key) end @attributes.clear @size_dep.changed! @persistor.removed(nil) if @persistor end |
#delete(name) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/volt/models/model_hash_behaviour.rb', line 6 def delete(name) name = name.to_sym value = @attributes.delete(name) @size_dep.changed! @deps.delete(name) @persistor.removed(name) if @persistor value end |
#each_with_object(*args, &block) ⇒ Object
57 58 59 |
# File 'lib/volt/models/model_hash_behaviour.rb', line 57 def each_with_object(*args, &block) (@attributes || {}).each_with_object(*args, &block) end |
#empty? ⇒ Boolean
33 34 35 36 |
# File 'lib/volt/models/model_hash_behaviour.rb', line 33 def empty? @size_dep.depend !@attributes || @attributes.size == 0 end |
#false? ⇒ Boolean
38 39 40 |
# File 'lib/volt/models/model_hash_behaviour.rb', line 38 def false? @attributes.false? end |
#keys ⇒ Object
24 25 26 27 |
# File 'lib/volt/models/model_hash_behaviour.rb', line 24 def keys @size_dep.depend @attributes.keys end |
#nil? ⇒ Boolean
29 30 31 |
# File 'lib/volt/models/model_hash_behaviour.rb', line 29 def nil? @attributes.nil? end |
#size ⇒ Object
19 20 21 22 |
# File 'lib/volt/models/model_hash_behaviour.rb', line 19 def size @size_dep.depend @attributes.size end |
#to_h ⇒ Object
Convert the model to a hash all of the way down.
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/volt/models/model_hash_behaviour.rb', line 62 def to_h @size_dep.depend if @attributes.nil? nil else hash = {} @attributes.each_pair do |key, value| hash[key] = deep_unwrap(value) end hash end end |
#true? ⇒ Boolean
42 43 44 |
# File 'lib/volt/models/model_hash_behaviour.rb', line 42 def true? @attributes.true? end |