module Facebooker::Model

helper methods primarily supporting the management of Ruby objects which are populatable via Hashes. Since most Facebook API calls accept and return hashes of data (as XML), the Model module allows us to directly populate a model’s attributes given a Hash with matching key names.

Public Instance Methods

anon=(value) click to toggle source

This gets populated from FQL queries.

# File lib/facebooker/model.rb, line 105
def anon=(value)
  @anonymous_fields = value
end
populate() click to toggle source
# File lib/facebooker/model.rb, line 113
def populate
  raise NotImplementedError, "#{self.class} included me and should have overriden me"
end
populate_from_hash!(hash) click to toggle source

Set model’s attributes via Hash. Keys should map directly to the model’s attribute names.

# File lib/facebooker/model.rb, line 123
def populate_from_hash!(hash)
  unless hash.nil? || hash.empty?
    hash.each do |key, value|
      set_attr_method = "#{key}="
      unless value.nil?
        if respond_to?(set_attr_method)
          self.__send__(set_attr_method, value) 
        else
          Facebooker::Logging.log_info("**Warning**, Attempt to set non-attribute: #{key}",hash)
        end
      end
    end
    @populated = true
  end      
end
populated?() click to toggle source
# File lib/facebooker/model.rb, line 117
def populated?
  @populated
end
session() click to toggle source

Centralized, error-checked place for a model to get the session to which it is bound. Any Facebook API queries require a Session instance.

# File lib/facebooker/model.rb, line 99
def session
  @session || (raise UnboundSessionException, "Must bind this object to a Facebook session before querying")
end

Public Class Methods

included(includer) click to toggle source
# File lib/facebooker/model.rb, line 8
def self.included(includer)
  includer.extend ClassMethods
  includer.__send__(:attr_writer, :session)
  includer.__send__(:attr_reader, :anonymous_fields)
end
new(hash = {}) click to toggle source
# File lib/facebooker/model.rb, line 109
def initialize(hash = {})
  populate_from_hash!(hash)
end