class FantasticRobot::Model::Base
Constants
- FIELD_CONVERSIONS
As the models include a lot of different objects related, we build a conversion hash that indicates the type of objects required in special fields.
Public Class Methods
new(attributes = {})
click to toggle source
Initializer that converts the fields received if necessary
Calls superclass method
# File lib/fantastic_robot/model/base.rb, line 15 def initialize(attributes = {}) # Define a setter for each field that need to be converted self.class::FIELD_CONVERSIONS.each do |field_name, field_type| self.class.send(:define_method, "#{field_name}=") do |value| if (value.is_a?(Array)) # If we receive an array, process each value value.map! do |v| # Convert each object except if it is already converted or nil v = ((v.class == field_type || v.nil?) ? v : field_type.new(v)) end else # If it isn't an array, try to convert the object value = field_type.new(value) unless (value.class == field_type || value.nil?) end # Set the result instance_variable_set("@#{field_name}", value) end end attributes = attributes.delete_if{|k, v| !self.respond_to?("#{k}=")} super(attributes) end
Public Instance Methods
attributes()
click to toggle source
Attribute map for ActiveModel::Serialization.
# File lib/fantastic_robot/model/base.rb, line 42 def attributes Hash[instance_variables.map{|attrib| [attrib.to_s[1..attrib.to_s.size], nil]}] end
to_h()
click to toggle source
Proxy to get a serialized version of the object.
# File lib/fantastic_robot/model/base.rb, line 47 def to_h recursive_serialization(self) end
Private Instance Methods
recursive_serialization(object)
click to toggle source
Method to try to recursively seralize the objects received
# File lib/fantastic_robot/model/base.rb, line 54 def recursive_serialization object if (object.is_a?(Array)) # If it's an array, try to serialize each element return object.map{|o| recursive_serialization(o)} elsif (object.is_a?(Hash)) # It's a hash, convert each key to sym and try to serialize each value return Hash[object.map{|k,v| [k.to_sym, recursive_serialization(v)]}] elsif (object.respond_to?(:serializable_hash)) # If it can be seralized, serialize and try to recursively convert it's attributes return recursive_serialization(object.serializable_hash) else return object end end