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.



105
106
107
# File 'lib/facebooker/model.rb', line 105

def anon=(value)
  @anonymous_fields = value
end

#initialize(hash = {}) ⇒ Object



109
110
111
# File 'lib/facebooker/model.rb', line 109

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

#populateObject

Raises:

  • (NotImplementedError)


113
114
115
# 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) ⇒ Object

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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/facebooker/model.rb', line 117

def populated?
  @populated
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.



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

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