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.



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

def anon=(value)
  @anonymous_fields = value
end

#initialize(hash = {}) ⇒ Object



107
108
109
# File 'lib/facebooker/model.rb', line 107

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

#populateObject

Raises:

  • (NotImplementedError)


111
112
113
# File 'lib/facebooker/model.rb', line 111

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.



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/facebooker/model.rb', line 121

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)


115
116
117
# File 'lib/facebooker/model.rb', line 115

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.



97
98
99
# File 'lib/facebooker/model.rb', line 97

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