Module: Volt::ModelHashBehaviour

Included in:
Model
Defined in:
lib/volt/models/model_hash_behaviour.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#clearObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/volt/models/model_hash_behaviour.rb', line 56

def clear
  @attributes.each_pair do |key, _|
    delete(key)
  end

  # @attributes.clear
  @size_dep.changed!
  #
  # @persistor.removed(nil) if @persistor
end

#delete(name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# 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) ⇒ Object



71
72
73
74
75
# File 'lib/volt/models/model_hash_behaviour.rb', line 71

def each(&block)
  # TODO: We shouldn't need to check the size for this to work
  size
  @array.each(&block)
end

#each_pairObject



77
78
79
80
81
# File 'lib/volt/models/model_hash_behaviour.rb', line 77

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) ⇒ Object



67
68
69
# File 'lib/volt/models/model_hash_behaviour.rb', line 67

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

#empty?Boolean

Returns:



43
44
45
46
# File 'lib/volt/models/model_hash_behaviour.rb', line 43

def empty?
  @size_dep.depend
  !@attributes || @attributes.size == 0
end

#false?Boolean

Returns:



48
49
50
# File 'lib/volt/models/model_hash_behaviour.rb', line 48

def false?
  @attributes.false?
end

#key?(key) ⇒ Boolean

Returns:



83
84
85
# File 'lib/volt/models/model_hash_behaviour.rb', line 83

def key?(key)
  @attributes && @attributes.key?(key)
end

#keysObject

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



27
28
29
30
31
32
33
34
35
36
37
# 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?Boolean

Returns:



39
40
41
# File 'lib/volt/models/model_hash_behaviour.rb', line 39

def nil?
  @attributes.nil?
end

#sizeObject



19
20
21
22
# File 'lib/volt/models/model_hash_behaviour.rb', line 19

def size
  @size_dep.depend
  @attributes.size
end

#to_hObject

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



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/volt/models/model_hash_behaviour.rb', line 88

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

#true?Boolean

Returns:



52
53
54
# File 'lib/volt/models/model_hash_behaviour.rb', line 52

def true?
  @attributes.true?
end