Module: Titi::Adaptor

Defined in:
lib/titi/adaptor.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



53
54
55
56
57
# File 'lib/titi/adaptor.rb', line 53

def self.included base
  base.class_eval do
    extend Titi::Adaptor::ClassMethods
  end
end

Instance Method Details

#adapt(hsh = {}) {|_self| ... } ⇒ Object

Adopt attributes from given hash, and programatically-set attributes from block

ActivityStreams::Entry.adapt(

:id        => status.id,
:title     => status.text,
:content   => status.text,
:verb      => :post
) do |entry|
status_time     = Time.parse(status.created_at) rescue nil
entry.published = status_time
entry.author    = ActivityStreams::Author.new(:name => status.user.name, :url => status.user.url)
entry.object    = ActivityStreams::Object.adapt do |activity_obj|
  activity_obj.id        = status.id
  activity_obj.title     = status.text
  activity_obj.published = status_time
  activity_obj.updated   = status_time
  activity_obj.author    = ActivityStreams::Author.new(:name => status.user.name, :url => status.user.url)
end

end

Yields:

  • (_self)

Yield Parameters:

  • _self (Titi::Adaptor)

    the object that the method was called on



38
39
40
41
# File 'lib/titi/adaptor.rb', line 38

def adapt hsh={}, &block
  self.attributes = hsh
  yield self if block
end

#attributes=(hsh) ⇒ Object

for each key in hsh, sets that attribute to the corresponding value.

Examples:

foo
# => #<Struct Simian name="Mojo Jojo" status="villain" nemesis=nil species='Chimpanzee'>
foo.attributes = { :nemesis => 'Powerpuff girls', :species => 'Pan Troglodytes' }
foo
# => #<Struct Simian name="Mojo Jojo" status="villain" nemesis='Powerpuff Girls' species='Pan Troglodytes'>


11
12
13
14
15
16
# File 'lib/titi/adaptor.rb', line 11

def attributes= hsh
  hsh.each do |attr, val|
    setter = "#{attr}="
    self.send(setter, val) if respond_to?(setter)
  end
end