Sha256: 419b137de8d56bcaaad0ae406c028776b4475148d7fb8c03b7e103f3a09fc713

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

module Volt
  # 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)

      @size_dep.changed!
      @deps.delete(name)

      @persistor.removed(name) if @persistor

      value
    end

    def size
      @size_dep.depend
      @attributes.size
    end

    def keys
      @size_dep.depend
      @attributes.keys
    end

    def nil?
      @attributes.nil?
    end

    def empty?
      @size_dep.depend
      !@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
      @size_dep.changed!

      @persistor.removed(nil) if @persistor
    end

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

    # Convert the model to a hash all of the way down.
    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

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
volt-0.8.21 lib/volt/models/model_hash_behaviour.rb
volt-0.8.20 lib/volt/models/model_hash_behaviour.rb
volt-0.8.19 lib/volt/models/model_hash_behaviour.rb