class RouteNGNClient::Model
Attributes
attributes[RW]
request_account_id[RW]
Public Class Methods
new(attributes = {})
click to toggle source
# File lib/routengn_client/model.rb, line 7 def initialize(attributes = {}) @attributes = OpenStruct.new(attributes) @request_account_id = nil self.create_parent_accessors self.create_child_accessors self.create_only_child_accessors end
Public Instance Methods
to_hash()
click to toggle source
# File lib/routengn_client/model.rb, line 15 def to_hash @attributes.marshal_dump end
to_json(pretty = false)
click to toggle source
# File lib/routengn_client/model.rb, line 19 def to_json(pretty = false) MultiJson.dump(self.to_hash, :pretty => pretty) end
to_log(options = {})
click to toggle source
# File lib/routengn_client/model.rb, line 23 def to_log(options = {}) self.to_json(true) end
Protected Instance Methods
create_child_accessors()
click to toggle source
# File lib/routengn_client/model.rb, line 102 def create_child_accessors return if self.class.children.blank? p = self.class.to_s.demodulize.underscore.singularize.to_sym instance = self self.class.children.each do |klass| c = klass.to_s.demodulize.underscore.pluralize.to_sym instance_var = "@#{klass.to_s.demodulize.underscore.pluralize}".to_sym (class << self; self; end).class_eval do define_method(c) do |options = {}| if models = instance_variable_get(instance_var) return models else collection = attributes.send(c) || [] models = collection.collect do |h| if h.kind_of?(Hash) && h[c.to_s.singularize.to_sym] model = klass.send :from_hash, h[c.to_s.singularize.to_sym] elsif h.kind_of?(Hash) model = klass.send :from_hash, h else model = h end model.request_account_id = instance.request_account_id model end instance_variable_set(instance_var, models) models end end end end end
create_only_child_accessors()
click to toggle source
# File lib/routengn_client/model.rb, line 65 def create_only_child_accessors return if self.class.only_children.blank? p = self.class.to_s.demodulize.underscore.singularize.to_sym instance = self self.class.only_children.each do |klass| c = klass.to_s.demodulize.underscore.singularize.to_sym instance_var = "@#{klass.to_s.demodulize.underscore.singularize}".to_sym (class << self; self; end).class_eval do define_method(c) do |options = {}| if model = instance_variable_get(instance_var) return model else if attributes.send(c).is_a?(klass) model = attributes.send(c) elsif attributes.send(c).kind_of?(Hash) model = klass.send :from_hash, attributes.send(c) else child_id = attributes.send("#{c.to_s}_id".to_sym) model = klass.send(:find, child_id, :account_id => instance.request_account_id) unless options[:no_fetch] || !child_id end return unless model model.request_account_id = instance.request_account_id instance_variable_set(instance_var, model) model end end end end end
create_parent_accessors()
click to toggle source
# File lib/routengn_client/model.rb, line 143 def create_parent_accessors return if self.class.parents.blank? self.class.parents.each do |klass| p = klass.to_s.demodulize.underscore.singularize.to_sym instance_var = "@#{klass.to_s.demodulize.underscore.singularize}".to_sym instance = self (class << self; self; end).class_eval do define_method(p) do |options = {}| if model = instance_variable_get(instance_var) return model else if v = attributes.send(p) model = v.kind_of?(Hash) ? klass.send(:from_hash, v) : v model.request_account_id = instance.request_account_id instance_variable_set(instance_var, model) model elsif !(id = attributes.send("#{p.to_s}_id".to_sym)).blank? model = attributes.send(p) if attributes.send(p).is_a?(klass) model = klass.send(:find, id, :account_id => instance.request_account_id) unless model || options[:no_fetch] instance_variable_set(instance_var, model) model end end end end end end