module Volt::ModelHashBehaviour

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.

Public Instance Methods

clear() click to toggle source
# File lib/volt/models/model_hash_behaviour.rb, line 48
def clear
  @attributes.each_pair do |key, _|
    delete(key)
  end

  # @attributes.clear
  @size_dep.changed!
  #
  # @persistor.removed(nil) if @persistor
end
delete(name) click to toggle source
# File lib/volt/models/model_hash_behaviour.rb, line 6
def delete(name)
  name = name.to_sym

  value = @attributes.delete(name)

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

  @persistor.removed(name) if @persistor

  value
end
each(&block) click to toggle source
# File lib/volt/models/model_hash_behaviour.rb, line 63
def each(&block)
  # TODO: We shouldn't need to check the size for this to work
  size
  @array.each(&block)
end
each_pair() { |k, v| ... } click to toggle source
# File lib/volt/models/model_hash_behaviour.rb, line 69
def each_pair
  @attributes.each_pair do |k, v|
    yield(k, v) unless v.is_a?(Model) && v.nil?
  end
end
each_with_object(*args, &block) click to toggle source
# File lib/volt/models/model_hash_behaviour.rb, line 59
def each_with_object(*args, &block)
  (@attributes || {}).each_with_object(*args, &block)
end
empty?() click to toggle source
# File lib/volt/models/model_hash_behaviour.rb, line 43
def empty?
  @size_dep.depend
  !@attributes || @attributes.size == 0
end
key?(key) click to toggle source
# File lib/volt/models/model_hash_behaviour.rb, line 75
def key?(key)
  @attributes && @attributes.key?(key)
end
keys() click to toggle source

Returns all of the keys, skipping over nil models TODO: We should store nil-models elsewhere so we don't have to skip.

# File lib/volt/models/model_hash_behaviour.rb, line 27
def keys
  @size_dep.depend

  keys = []

  each_pair do |k, v|
    keys << k
  end

  keys
end
nil?() click to toggle source
# File lib/volt/models/model_hash_behaviour.rb, line 39
def nil?
  @attributes.nil?
end
size() click to toggle source
# File lib/volt/models/model_hash_behaviour.rb, line 19
def size
  @size_dep.depend
  @attributes.size
end
to_h() click to toggle source

Convert the model to a hash all of the way down.

# File lib/volt/models/model_hash_behaviour.rb, line 80
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