Module: Facebooker::Model

Overview

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.

Defined Under Namespace

Modules: ClassMethods Classes: UnboundSessionException

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object



8
9
10
11
12
# 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

Instance Method Details

#anon=(value) ⇒ Object

This gets populated from FQL queries.



91
92
93
# File 'lib/facebooker/model.rb', line 91

def anon=(value)
  @anonymous_fields = value
end

#initialize(hash = {}) ⇒ Object



95
96
97
# File 'lib/facebooker/model.rb', line 95

def initialize(hash = {})
  populate_from_hash!(hash)
end

#populateObject

Raises:

  • (NotImplementedError)


99
100
101
# File 'lib/facebooker/model.rb', line 99

def populate
  raise NotImplementedError, "#{self.class} included me and should have overriden me"
end

#populate_from_hash!(hash) ⇒ Object

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



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/facebooker/model.rb', line 109

def populate_from_hash!(hash)
  unless hash.empty?
    hash.each do |key, value|
      set_attr_method = "#{key}="
      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
    @populated = true
  end      
end

#populated?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/facebooker/model.rb', line 103

def populated?
  !@populated.nil?
end

#sessionObject

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



85
86
87
# File 'lib/facebooker/model.rb', line 85

def session
  @session || (raise UnboundSessionException, "Must bind this object to a Facebook session before querying")
end