Sha256: 4e320f95ce556c5edc53d3b7408224920e79c2d0f1d1c80d8e914690358497a3

Contents?: true

Size: 1.14 KB

Versions: 21

Compression:

Stored size: 1.14 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(name)
    name = name.to_sym
    __clear_element(name)
    value = attributes.delete(name)
    trigger_by_attribute!('changed', name)

    @persistor.removed(name) if @persistor

    return value
  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

end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
volt-0.7.23 lib/volt/models/model_hash_behaviour.rb
volt-0.7.22 lib/volt/models/model_hash_behaviour.rb
volt-0.7.21 lib/volt/models/model_hash_behaviour.rb
volt-0.7.20 lib/volt/models/model_hash_behaviour.rb
volt-0.7.19 lib/volt/models/model_hash_behaviour.rb
volt-0.7.18 lib/volt/models/model_hash_behaviour.rb
volt-0.7.17 lib/volt/models/model_hash_behaviour.rb
volt-0.7.16 lib/volt/models/model_hash_behaviour.rb
volt-0.7.15 lib/volt/models/model_hash_behaviour.rb
volt-0.7.14 lib/volt/models/model_hash_behaviour.rb
volt-0.7.13 lib/volt/models/model_hash_behaviour.rb
volt-0.7.12 lib/volt/models/model_hash_behaviour.rb
volt-0.7.10 lib/volt/models/model_hash_behaviour.rb
volt-0.7.9 lib/volt/models/model_hash_behaviour.rb
volt-0.7.8 lib/volt/models/model_hash_behaviour.rb
volt-0.7.7 lib/volt/models/model_hash_behaviour.rb
volt-0.7.6 lib/volt/models/model_hash_behaviour.rb
volt-0.7.5 lib/volt/models/model_hash_behaviour.rb
volt-0.7.4 lib/volt/models/model_hash_behaviour.rb
volt-0.7.3 lib/volt/models/model_hash_behaviour.rb