Module: Facebooker::Model::ClassMethods

Defined in:
lib/facebooker/model.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) from_hash(hash) {|instance| ... }

Instantiate a new instance of the class into which we are included and populate that instance's attributes given the provided Hash. Key names in the Hash should map to attribute names on the model.

Yields:

  • (instance)


17
18
19
20
21
# File 'lib/facebooker/model.rb', line 17

def from_hash(hash)
  instance = new(hash)
  yield instance if block_given?
  instance
end

- (Object) hash_settable_accessor(symbol, klass)

Declares an attribute named ::symbol

which can be set with either an instance of ::klass

or a Hash which will be used to populate a new instance of ::klass::.



54
55
56
57
# File 'lib/facebooker/model.rb', line 54

def hash_settable_accessor(symbol, klass)
  attr_reader symbol
  hash_settable_writer(symbol, klass)
end

- (Object) hash_settable_list_accessor(symbol, klass)

Declares an attribute named ::symbol

which can be set with either a list of instances of ::klass

or a list of Hashes which will be used to populate a new instance of ::klass::.



68
69
70
71
# File 'lib/facebooker/model.rb', line 68

def hash_settable_list_accessor(symbol, klass)
  attr_reader symbol
  hash_settable_list_writer(symbol, klass)
end

- (Object) hash_settable_list_writer(symbol, klass)



73
74
75
76
77
78
79
# File 'lib/facebooker/model.rb', line 73

def hash_settable_list_writer(symbol, klass)
  define_method("#{symbol}=") do |list|
    instance_variable_set("@#{symbol}", list.map do |item|
      item.kind_of?(Hash) ? klass.from_hash(item) : item
    end)
  end
end

- (Object) hash_settable_writer(symbol, klass)



59
60
61
62
63
# File 'lib/facebooker/model.rb', line 59

def hash_settable_writer(symbol, klass)
  define_method("#{symbol}=") do |value|
    instance_variable_set("@#{symbol}", value.kind_of?(Hash) ? klass.from_hash(value) : value)
  end
end

- (Object) id_is(attribute)



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/facebooker/model.rb', line 81

def id_is(attribute)
  (file, line) = caller.first.split(':')

  class_eval(<<-EOS, file, line.to_i)
  def #{attribute}=(value)
    @#{attribute} = value.to_i
  end

  attr_reader #{attribute.inspect}
  alias :id #{attribute.inspect}
  alias :id= #{"#{attribute}=".to_sym.inspect}
  EOS
end

- (Object) populating_attr_accessor(*symbols)

Create a standard attr_writer and a populating_attr_reader



25
26
27
28
# File 'lib/facebooker/model.rb', line 25

def populating_attr_accessor(*symbols)
  attr_writer(*symbols)
  populating_attr_reader(*symbols)
end

- (Object) populating_attr_reader(*symbols)

Create a reader that will attempt to populate the model if it has not already been populated



32
33
34
35
36
37
38
39
# File 'lib/facebooker/model.rb', line 32

def populating_attr_reader(*symbols)
  symbols.each do |symbol|
    define_method(symbol) do
      populate unless populated?
      instance_variable_get("@#{symbol}")
    end
  end
end

- (Object) populating_hash_settable_accessor(symbol, klass)



41
42
43
44
# File 'lib/facebooker/model.rb', line 41

def populating_hash_settable_accessor(symbol, klass)
  populating_attr_reader symbol
  hash_settable_writer(symbol, klass)
end

- (Object) populating_hash_settable_list_accessor(symbol, klass)



46
47
48
49
# File 'lib/facebooker/model.rb', line 46

def populating_hash_settable_list_accessor(symbol, klass)
  populating_attr_reader symbol
  hash_settable_list_writer(symbol, klass)
end