Sha256: 93adcf284290acc1f0e2eec2941a56fd40d2fb35662f1bc8399c211d80862c4e

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 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 delete(name)
    name = name.to_sym

    value = attributes.delete(name)
    @deps.delete(name)

    @persistor.removed(name) if @persistor

    return value
  end

  def nil?
    attributes.nil?
  end

  def empty?
    !attributes || attributes.size == 0
  end

  def false?
    attributes.false?
  end

  def true?
    attributes.true?
  end

  def clear
    attributes.each_pair do |key,value|
      @deps.changed!(key)
    end

    attributes.clear

    @persistor.removed(nil) if @persistor
  end

  def each_with_object(*args, &block)
    return (@attributes || {}).each_with_object(*args, &block)
  end


  # Convert the model to a hash all of the way down.
  def to_h
    hash = {}
    if empty?
      return nil
    else
      attributes.each_pair do |key, value|
        hash[key] = deep_unwrap(value)
      end

      return hash
    end
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
volt-0.8.14 lib/volt/models/model_hash_behaviour.rb
volt-0.8.13 lib/volt/models/model_hash_behaviour.rb
volt-0.8.11 lib/volt/models/model_hash_behaviour.rb
volt-0.8.10 lib/volt/models/model_hash_behaviour.rb
volt-0.8.9 lib/volt/models/model_hash_behaviour.rb