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



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

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

  attributes.clear

  @persistor.removed(nil) if @persistor
end

#delete(name) ⇒ Object



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

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



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

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

#empty?Boolean



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

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

#false?Boolean



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

def false?
  attributes.false?
end

#nil?Boolean



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

def nil?
  attributes.nil?
end

#to_hObject

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



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

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

#true?Boolean



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

def true?
  attributes.true?
end