Module: 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.
Class Method Summary collapse
Instance Method Summary collapse
- #[](val) ⇒ Object
- #clear ⇒ Object
- #delete(*args) ⇒ Object
- #false? ⇒ Boolean
- #nil? ⇒ Boolean
- 
  
    
      #to_h  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Convert the model to a hash all of the way down. 
- #true? ⇒ Boolean
Class Method Details
.included(base) ⇒ Object
| 5 6 7 8 9 10 11 12 13 14 15 | # File 'lib/volt/models/model_hash_behaviour.rb', line 5 def self.included(base) # In modules, since we need to tag on the main class, we setup the # tags with included. base.tag_method(:delete) do destructive! end base.tag_method(:clear) do destructive! end end | 
Instance Method Details
#[](val) ⇒ Object
| 62 63 64 | # File 'lib/volt/models/model_hash_behaviour.rb', line 62 def [](val) raise "Models do not support hash style lookup. Hashes inserted into other models are converted to models, see https://github.com/voltrb/volt#automatic-model-conversion" end | 
#clear ⇒ Object
| 39 40 41 42 43 44 45 46 47 48 | # File 'lib/volt/models/model_hash_behaviour.rb', line 39 def clear attributes.each_pair do |key,value| __clear_element(key) end attributes.clear trigger!('changed') @persistor.removed(nil) if @persistor end | 
#delete(*args) ⇒ Object
| 30 31 32 33 34 35 36 | # File 'lib/volt/models/model_hash_behaviour.rb', line 30 def delete(*args) __clear_element(args[0]) attributes.delete(*args) trigger_by_attribute!('changed', args[0]) @persistor.removed(args[0]) if @persistor end | 
#false? ⇒ Boolean
| 22 23 24 | # File 'lib/volt/models/model_hash_behaviour.rb', line 22 def false? attributes.false? end | 
#nil? ⇒ Boolean
| 18 19 20 | # File 'lib/volt/models/model_hash_behaviour.rb', line 18 def nil? attributes.nil? end | 
#to_h ⇒ Object
Convert the model to a hash all of the way down.
| 52 53 54 55 56 57 58 59 | # File 'lib/volt/models/model_hash_behaviour.rb', line 52 def to_h hash = {} attributes.each_pair do |key, value| hash[key] = deep_unwrap(value) end return hash end | 
#true? ⇒ Boolean
| 26 27 28 | # File 'lib/volt/models/model_hash_behaviour.rb', line 26 def true? attributes.true? end |