Class: Resto::Property::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/resto/property/handler.rb

Instance Method Summary collapse

Constructor Details

#initializeHandler

Returns a new instance of Handler.



5
6
7
8
# File 'lib/resto/property/handler.rb', line 5

def initialize
  @properties = {} # TODO fix indifferent access
  @properties_with_indifferent_access = {}
end

Instance Method Details

#add(property) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/resto/property/handler.rb', line 10

def add(property)
  @properties_with_indifferent_access.store(property.remote_key, property)
  @properties_with_indifferent_access.
    store(property.attribute_key, property)
  @properties_with_indifferent_access.
    store(property.attribute_key_as_string, property)

  @properties.store(property.attribute_key, property)
end

#attribute_key(key) ⇒ Object



20
21
22
# File 'lib/resto/property/handler.rb', line 20

def attribute_key(key)
  get(key, 'attribute_key')
end

#cast(key, value, errors) ⇒ Object



35
36
37
# File 'lib/resto/property/handler.rb', line 35

def cast(key, value, errors)
  get(key).cast(value, errors)
end

#remote_attributes(attributes) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/resto/property/handler.rb', line 24

def remote_attributes(attributes)
  remote_attributes = {}

  attributes.each do |key, value|
    remote_key = get(key, 'remote_key') || key
    remote_attributes[remote_key] = value
  end

  remote_attributes
end

#validate(resource) ⇒ Object



39
40
41
42
43
# File 'lib/resto/property/handler.rb', line 39

def validate(resource)
  @properties.each do |key, property|
    property.validate(resource, key)
  end
end