Module: Amiando::Attributes::ClassMethods

Defined in:
lib/amiando/attributes.rb

Instance Method Summary collapse

Instance Method Details

#inverse_typecast(key, value) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/amiando/attributes.rb', line 55

def inverse_typecast(key, value)
  if typecasting[key] == :time
    Time.parse(value)
  else
    value
  end
end

#map(local, remote, options = {}) ⇒ Object



11
12
13
14
# File 'lib/amiando/attributes.rb', line 11

def map(local, remote, options = {})
  mapping[local] = remote
  typecasting[local] = options[:type] if options[:type]
end

#map_params(attributes) ⇒ Object

From { :first_name => ‘1’, :last_name => ‘2’ } to { :firstName => ‘1’, :lastName => ‘2’ }



27
28
29
30
31
32
33
34
# File 'lib/amiando/attributes.rb', line 27

def map_params(attributes)
  mapped_attributes = attributes.map do |key,value|
    mapped_key = mapping[key] || Utils.camelize(key, :lower).to_sym
    value = typecast(key, value)
    [mapped_key, value]
  end
  Hash[mapped_attributes]
end

#mappingObject



20
21
22
# File 'lib/amiando/attributes.rb', line 20

def mapping
  @mapping ||= {}
end

#reverse_map_params(attributes) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/amiando/attributes.rb', line 36

def reverse_map_params(attributes)
  inverted_mapping = mapping.invert
  mapped_attributes = attributes.map do |key,value|
    key        = key.to_sym
    mapped_key = inverted_mapping[key] || Utils.underscore(key).to_sym
    value      = inverse_typecast(mapped_key, value)
    [mapped_key, value]
  end
  Hash[mapped_attributes]
end

#typecast(key, value) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/amiando/attributes.rb', line 47

def typecast(key, value)
  if typecasting[key] == :time || value.is_a?(Time)
    value.iso8601
  else
    value
  end
end

#typecastingObject



16
17
18
# File 'lib/amiando/attributes.rb', line 16

def typecasting
  @typecasting ||= {}
end