Skip to content

Commit

Permalink
Refactored single attribute mapping into separate method in DataMappi…
Browse files Browse the repository at this point in the history
…ngs.
  • Loading branch information
tszolar committed May 12, 2014
1 parent 9b2f7cf commit 3eb61df
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions lib/kosapi_client/entity/data_mappings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,27 @@ def parse(content)
# Creates new domain object instance and sets values
# of mapped domain object attributes from source hash.
# Attributes are mapped by .map_data method.
def set_mapped_attributes(instance, content)
def set_mapped_attributes(instance, source_hash)
if self.superclass.respond_to? :set_mapped_attributes
self.superclass.set_mapped_attributes(instance, content)
self.superclass.set_mapped_attributes(instance, source_hash)
end
raise "Missing data mappings for entity #{self}" unless @@data_mappings[self]
@@data_mappings[self].each do |name, options|
value_to_convert = content[name]
if value_to_convert.nil?
raise "Missing value for attribute #{name}" if options[:required]
next
end
value = convert_type(content[name], options[:type])
instance.send("#{name}=".to_sym, value)
set_mapped_attribute(instance, name, source_hash, options)
end
end

private
def set_mapped_attribute(instance, name, source_hash, mapping_options)
value = source_hash[name]
if value.nil?
raise "Missing value for attribute #{name}" if mapping_options[:required]
return
end
value = convert_type(value, mapping_options[:type])
instance.send("#{name}=".to_sym, value)
end

def convert_type(value, type)
return value.to_i if type == Integer
return value if type == String
Expand Down

0 comments on commit 3eb61df

Please sign in to comment.