lib/apimaster/mapper.rb in apimaster-0.0.4 vs lib/apimaster/mapper.rb in apimaster-0.0.5
- old
+ new
@@ -1,12 +1,11 @@
# encoding: utf-8
#
# Copyright (C) 2011-2012 AdMaster, Inc.
module Apimaster
- class Mapper
- #include ::Mongoid::Document
+ module Mapper
def post hash
save_with_hash hash, :post
end
@@ -21,11 +20,11 @@
def save_with_hash hash, method
from_hash hash, method
if valid?
save
else
- raise InvalidFieldError.new(class_name, errors.keys.first)
+ raise Apimaster::InvalidFieldError.new(class_name, errors.keys.first)
end
self
end
def from_hash(hash, method = :all)
@@ -59,11 +58,12 @@
def to_hash accessor = :all
record = {}
fields = self.class.find_attrs_in_options(:accessor, accessor)
fields.each do |field|
if self.respond_to?(field)
- record[field] = self.send(field)
+ val = self.send(field)
+ record[field] = val.respond_to?(:to_hash) ? val.to_hash(accessor) : val
else
raise "Dataset #{self.class} has no method with the name of #{field}"
end
end
record
@@ -71,10 +71,14 @@
def class_name
@class_name ||= self.class.to_s.split("::").last
end
- class << self
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
OPTION_TYPES = [:accessor, :required, :optional]
# attr_options :url, accessor: [:get, :list]
def attr_options name, options = {}