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



46
47
48
49
50
51
52
53
54
55
# File 'lib/volt/models/model_hash_behaviour.rb', line 46

def clear
  @attributes.each_pair do |key, value|
    @deps.changed!(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_with_object(*args, &block) ⇒ Object



57
58
59
# File 'lib/volt/models/model_hash_behaviour.rb', line 57

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

#empty?Boolean

Returns:



33
34
35
36
# File 'lib/volt/models/model_hash_behaviour.rb', line 33

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

#false?Boolean

Returns:



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

def false?
  @attributes.false?
end

#keysObject



24
25
26
27
# File 'lib/volt/models/model_hash_behaviour.rb', line 24

def keys
  @size_dep.depend
  @attributes.keys
end

#nil?Boolean

Returns:



29
30
31
# File 'lib/volt/models/model_hash_behaviour.rb', line 29

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.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/volt/models/model_hash_behaviour.rb', line 62

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:



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

def true?
  @attributes.true?
end