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



33
34
35
36
37
38
39
40
41
# File 'lib/volt/models/model_hash_behaviour.rb', line 33

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

  attributes.clear

  @persistor.removed(nil) if @persistor
end

#delete(name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/volt/models/model_hash_behaviour.rb', line 6

def delete(name)
  name = name.to_sym

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

  @persistor.removed(name) if @persistor

  value
end

#each_with_object(*args, &block) ⇒ Object



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

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

#empty?Boolean

Returns:



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

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

#false?Boolean

Returns:



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

def false?
  attributes.false?
end

#nil?Boolean

Returns:



17
18
19
# File 'lib/volt/models/model_hash_behaviour.rb', line 17

def nil?
  attributes.nil?
end

#to_hObject

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



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

def to_h
  if @attributes.nil?
    nil
  else
    hash = {}
    attributes.each_pair do |key, value|
      hash[key] = deep_unwrap(value)
    end
    hash
  end
end

#true?Boolean

Returns:



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

def true?
  attributes.true?
end