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.

Instance Method Summary collapse

Instance Method Details

#clearObject



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

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

  return value
end

#false?Boolean

Returns:



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

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.



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

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

  return hash
end

#true?Boolean

Returns:



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

def true?
  attributes.true?
end