Module: 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.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  # In modules, since we need to tag on the main class, we setup the
  # tags with included.
  base.tag_method(:delete) do
    destructive!
  end

  base.tag_method(:clear) do
    destructive!
  end
end

Instance Method Details

#clearObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/volt/models/model_hash_behaviour.rb', line 42

def clear
  attributes.each_pair do |key,value|
    __clear_element(key)
  end

  attributes.clear
  trigger!('changed')

  @persistor.removed(nil) if @persistor
end

#delete(name) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/volt/models/model_hash_behaviour.rb', line 30

def delete(name)
  name = name.to_sym
  __clear_element(name)
  value = attributes.delete(name)
  trigger_by_attribute!('changed', name)

  @persistor.removed(name) if @persistor

  return value
end

#false?Boolean

Returns:



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

def false?
  attributes.false?
end

#nil?Boolean

Returns:



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.



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

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

  return hash
end

#true?Boolean

Returns:



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

def true?
  attributes.true?
end