class Mogli::Model
Attributes
type[RW]
Public Class Methods
add_creation_method(name,klass)
click to toggle source
# File lib/mogli/model.rb, line 128 def self.add_creation_method(name,klass) define_method "#{name}_create" do |*args| arg = args.first params = arg.nil? ? {} : arg.post_params klass_to_send = arg.nil? ? nil : klass client.post("#{id}/#{name}", klass_to_send, params) end end
creation_keys()
click to toggle source
# File lib/mogli/model.rb, line 99 def self.creation_keys @creation_properties || [] end
creation_properties(*args)
click to toggle source
# File lib/mogli/model.rb, line 95 def self.creation_properties(*args) @creation_properties = args end
define_properties(*args)
click to toggle source
# File lib/mogli/model.rb, line 89 def self.define_properties(*args) args.each do |arg| property arg end end
find(id,client=nil, *fields)
click to toggle source
# File lib/mogli/model.rb, line 179 def self.find(id,client=nil, *fields) body_args = fields.empty? ? {} : {:fields => fields.join(',')} (id, body_args[:ids] = "", id.join(',')) if id.is_a?(Array) (client||Mogli::Client.new).get_and_map(id,self, body_args) end
fql_mapping(hash=nil)
click to toggle source
# File lib/mogli/model.rb, line 58 def self.fql_mapping(hash=nil) if hash @fql_mapping = hash end @fql_mapping || {} end
has_association(name,klass)
click to toggle source
# File lib/mogli/model.rb, line 137 def self.has_association(name,klass) define_method name do |*fields| body_args = fields.empty? ? {} : {:fields => fields.join(',')} if (ret=instance_variable_get("@#{name}")).nil? ret = client.get_and_map("#{id}/#{name}",klass, body_args) instance_variable_set("@#{name}",ret) end return ret end define_method "#{name}=" do |value| instance_variable_set("@#{name}",client.map_to_class(client.extract_hash_or_array(value,klass),klass)) end add_creation_method(name,klass) (@associations ||= []) << name end
hash_populating_accessor(method_name,*klass)
click to toggle source
# File lib/mogli/model.rb, line 103 def self.hash_populating_accessor(method_name,*klass) define_method "#{method_name}=" do |hash| instance_variable_set("@#{method_name}",client.map_data(hash,klass)) end define_method "#{method_name}" do instance_variable_get "@#{method_name}" end add_creation_method(method_name,klass) (@populating_accessors ||= []) << method_name end
hash_populating_accessor_with_default_field(method_name,default_field,*klass)
click to toggle source
# File lib/mogli/model.rb, line 115 def self.hash_populating_accessor_with_default_field(method_name,default_field,*klass) define_method "#{method_name}=" do |hash| hash={default_field=>hash} if hash.is_a?(String) instance_variable_set("@#{method_name}",client.map_data(hash,klass)) end define_method "#{method_name}" do instance_variable_get "@#{method_name}" end add_creation_method(method_name,klass) (@populating_accessors ||= []) << method_name end
included(other)
click to toggle source
# File lib/mogli/model.rb, line 54 def self.included(other) other.extend(ClassMethods) end
new(hash={},client=nil)
click to toggle source
# File lib/mogli/model.rb, line 19 def initialize(hash={},client=nil) @_values = {} self.client=client hash.each do |k,v| self.send("#{self.class.fql_mapping[k]||k}=",v) end end
property(arg)
click to toggle source
# File lib/mogli/model.rb, line 76 def self.property(arg) @properties ||= [] @properties << arg define_method arg do @_values[arg.to_s] end define_method "#{arg}=" do |val| @_values[arg.to_s] = val end end
recognize?(data)
click to toggle source
# File lib/mogli/model.rb, line 175 def self.recognize?(data) true end
Public Instance Methods
==(other)
click to toggle source
# File lib/mogli/model.rb, line 161 def ==(other) other.is_a?(Model) and self.id == other.id end
client()
click to toggle source
# File lib/mogli/model.rb, line 15 def client @client || Mogli::Client.new end
client=(val)
click to toggle source
# File lib/mogli/model.rb, line 11 def client=(val) @client=val end
destroy()
click to toggle source
# File lib/mogli/model.rb, line 49 def destroy client.delete(id) freeze end
fetch(*fields)
click to toggle source
# File lib/mogli/model.rb, line 154 def fetch(*fields) raise ArgumentError.new("You cannot fetch models without a populated id attribute") if id.nil? other = self.class.find(id,client,fields) merge!(other) if other self end
merge!(other)
click to toggle source
# File lib/mogli/model.rb, line 165 def merge!(other) @_values.merge!(other.instance_variable_get("@_values")) # We need to copy not only root values, but, for example, user.location ( (self.class.instance_variable_get("@populating_accessors") || []) + (self.class.instance_variable_get("@associations") || [])).each do |var_name| instance_variable_set("@#{var_name}", other.instance_variable_get("@#{var_name}")) end end
method_missing(method, *args)
click to toggle source
Calls superclass method
# File lib/mogli/model.rb, line 64 def method_missing(method, *args) method_as_s = method.to_s if method_as_s.to_s[-1].chr == "=" warn_about_invalid_property(method_as_s.chop) else super end end
post_params()
click to toggle source
# File lib/mogli/model.rb, line 28 def post_params post_params = {} self.class.creation_keys.each do |key| post_params[key] = @_values[key.to_s] # make sure actions and any other creation_properties that aren't just # hash entries get added... if post_params[key].nil? && self.respond_to?(key.to_sym) && !(val=self.send(key.to_sym)).nil? post_params[key] = if val.is_a?(Array) "[#{val.map { |v| v.respond_to?(:to_json) ? v.to_json : nil }.compact.join(',')}]" elsif val.respond_to?(:to_json) val.to_json else nil end end end post_params end
warn_about_invalid_property(property)
click to toggle source
# File lib/mogli/model.rb, line 72 def warn_about_invalid_property(property) puts "Warning: property #{property} doesn't exist for class #{self.class.name}" end