Module: Apimaster::Mapper
- Defined in:
- lib/apimaster/mapper.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #class_name ⇒ Object
- #from_hash(hash, method = :all) ⇒ Object
- #patch(hash) ⇒ Object
- #post(hash) ⇒ Object
- #put(hash) ⇒ Object
- #respond_setter(key, val) ⇒ Object
- #save_with_hash(hash, method) ⇒ Object
- #to_api_hash(accessor = :all) ⇒ Object
-
#to_hash(accessor = :all) ⇒ Object
to be deleted.
Class Method Details
.included(base) ⇒ Object
91 92 93 |
# File 'lib/apimaster/mapper.rb', line 91 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#class_name ⇒ Object
87 88 89 |
# File 'lib/apimaster/mapper.rb', line 87 def class_name @class_name ||= self.class.to_s.split("::").last end |
#from_hash(hash, method = :all) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/apimaster/mapper.rb', line 30 def from_hash(hash, method = :all) data = {} attrs = [:required, :optional] attrs.each do |type| fields = self.class.(type, method) fields.each do |field| if hash.has_key?(field) data[field] = hash[field] elsif hash.has_key?(field.to_s) data[field] = hash[field.to_s] else raise Apimaster::MissingFieldError.new(class_name, field) if type == :required end end end data.each do |key, val| respond_setter key, val end end |
#patch(hash) ⇒ Object
16 17 18 |
# File 'lib/apimaster/mapper.rb', line 16 def patch hash save_with_hash hash, :patch end |
#post(hash) ⇒ Object
8 9 10 |
# File 'lib/apimaster/mapper.rb', line 8 def post hash save_with_hash hash, :post end |
#put(hash) ⇒ Object
12 13 14 |
# File 'lib/apimaster/mapper.rb', line 12 def put hash save_with_hash hash, :put end |
#respond_setter(key, val) ⇒ Object
52 53 54 55 56 |
# File 'lib/apimaster/mapper.rb', line 52 def respond_setter key, val name = (key.to_s + '=').to_sym raise "#{self.class} lost of #{name}" unless self.respond_to?(name) self.send name, val end |
#save_with_hash(hash, method) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/apimaster/mapper.rb', line 20 def save_with_hash hash, method from_hash hash, method if valid? save else raise Apimaster::InvalidFieldError.new(class_name, errors.keys.first) end self end |
#to_api_hash(accessor = :all) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/apimaster/mapper.rb', line 73 def to_api_hash accessor = :all record = {} fields = self.class.(: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 |
#to_hash(accessor = :all) ⇒ Object
to be deleted
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/apimaster/mapper.rb', line 59 def to_hash accessor = :all record = {} fields = self.class.(:accessor, accessor) fields.each do |field| if self.respond_to?(field) val = self.send(field) record[field] = (val.respond_to?(:to_hash) and not val.is_a?(Hash)) ? val.to_hash(accessor) : val else raise "Dataset #{self.class} has no method with the name of #{field}" end end record end |