lib/apimaster/mapper.rb in apimaster-0.1.2 vs lib/apimaster/mapper.rb in apimaster-0.1.3
- old
+ new
@@ -53,10 +53,11 @@
name = (key.to_s + '=').to_sym
raise "#{self.class} lost of #{name}" unless self.respond_to?(name)
self.send name, val
end
+ # to be deleted
def to_hash accessor = :all
record = {}
fields = self.class.find_attrs_in_options(:accessor, accessor)
fields.each do |field|
if self.respond_to?(field)
@@ -67,10 +68,24 @@
end
end
record
end
+ def to_api_hash accessor = :all
+ record = {}
+ fields = self.class.find_attrs_in_options(:accessor, accessor)
+ fields.each do |field|
+ if self.respond_to?(field)
+ val = self.send(field)
+ record[field] = val.respond_to?(:to_api_hash) ? val.to_api_hash(accessor) : val
+ else
+ raise "Dataset #{self.class} has no method with the name of #{field}"
+ end
+ end
+ record
+ end
+
def class_name
@class_name ||= self.class.to_s.split("::").last
end
def self.included(base)
@@ -103,9 +118,17 @@
def to_hashes accessor = :all
result = []
self.each do |val|
result << val.to_hash(accessor)
+ end
+ result
+ end
+
+ def to_api_hashes accessor = :all
+ result = []
+ self.each do |val|
+ result << val.to_api_hash(accessor)
end
result
end
end