Module: Twitter::ClassUtilMixin::InstanceMethods

Defined in:
lib/vendor/twitter/lib/twitter/core.rb

Overview

Instance methods defined for Twitter::ModelMixin module.

Instance Method Summary collapse

Instance Method Details

#initialize(params = {}) ⇒ Object

Constructor/initializer that takes a hash of parameters that will initialize members or instance attributes to the values given. For example,

class FilmActor
  include Twitter::ClassUtilMixin
  attr_accessor :name
end

class Production
  include Twitter::ClassUtilMixin
  attr_accessor :title, :year, :actors
end

# Favorite actress...
jodhi = FilmActor.new(:name => "Jodhi May")
jodhi.name # => "Jodhi May"

# Favorite actor...
robert = FilmActor.new(:name => "Robert Lindsay")
robert.name # => "Robert Lindsay"

# Jane is also an excellent pick...gotta love her accent!
jane = FilmActor.new(name => "Jane Horrocks")
jane.name # => "Jane Horrocks"

# Witty BBC series...
mrs_pritchard = Production.new(:title => "The Amazing Mrs. Pritchard",
                               :year => 2005,
                               :actors => [jodhi, jane])
mrs_pritchard.title  # => "The Amazing Mrs. Pritchard"
mrs_pritchard.year   # => 2005
mrs_pritchard.actors # => [#<FilmActor:0xb79d6bbc @name="Jodhi May">,
<FilmActor:0xb79d319c @name="Jane Horrocks">]
# Any Ros Pritchard's out there to save us from the Tony Blair
# and Gordon Brown *New Labour* debacle?  You've got my vote!

jericho = Production.new(:title => "Jericho",
                         :year => 2005,
                         :actors => [robert])
jericho.title   # => "Jericho"
jericho.year    # => 2005
jericho.actors  # => [#<FilmActor:0xc95d3eec @name="Robert Lindsay">]

Assuming class FilmActor includes Twitter::ClassUtilMixin in the class definition and has an attribute of name, then that instance attribute will be set to “Jodhi May” for the actress object during object initialization (aka construction for you Java heads).



71
72
73
74
75
76
# File 'lib/vendor/twitter/lib/twitter/core.rb', line 71

def initialize(params = {})
  params.each do |key,val|
    self.send("#{key}=", val) if self.respond_to? key
  end
  self.send(:init) if self.respond_to? :init
end