Sha256: 75b78b9cff36e3a9f1ec701cc6144c3e1b7438ca7bd077e5b95d0b90652c7924

Contents?: true

Size: 1.34 KB

Versions: 15

Compression:

Stored size: 1.34 KB

Contents

# 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.
module ModelHashBehaviour
  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
  
  
  def nil?
    attributes.nil?
  end
  
  def false?
    attributes.false?
  end
  
  def true?
    attributes.true?
  end
  
  def delete(*args)
    __clear_element(args[0])
    attributes.delete(*args)
    trigger_by_attribute!('changed', args[0])
    
    @persistor.removed(args[0]) if @persistor
  end
  

  def clear
    attributes.each_pair do |key,value|
      __clear_element(key)      
    end

    attributes.clear
    trigger!('changed')
    
    @persistor.removed(nil) if @persistor
  end
  
  
  # Convert the model to a hash all of the way down.
  def to_h
    hash = {}
    attributes.each_pair do |key, value|
      hash[key] = deep_unwrap(value)
    end
    
    return hash
  end
  
  
  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
  
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
volt-0.6.1 lib/volt/models/model_hash_behaviour.rb
volt-0.6.0 lib/volt/models/model_hash_behaviour.rb
volt-0.5.18 lib/volt/models/model_hash_behaviour.rb
volt-0.5.17 lib/volt/models/model_hash_behaviour.rb
volt-0.5.16 lib/volt/models/model_hash_behaviour.rb
volt-0.5.15 lib/volt/models/model_hash_behaviour.rb
volt-0.5.14 lib/volt/models/model_hash_behaviour.rb
volt-0.5.13 lib/volt/models/model_hash_behaviour.rb
volt-0.5.12 lib/volt/models/model_hash_behaviour.rb
volt-0.5.11 lib/volt/models/model_hash_behaviour.rb
volt-0.5.10 lib/volt/models/model_hash_behaviour.rb
volt-0.5.9 lib/volt/models/model_hash_behaviour.rb
volt-0.5.8 lib/volt/models/model_hash_behaviour.rb
volt-0.5.7 lib/volt/models/model_hash_behaviour.rb
volt-0.5.6 lib/volt/models/model_hash_behaviour.rb