Module: Titi::Adaptor

Included in:
Provider::ActivityStreams::ActivityStreamsStruct
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



55
56
57
58
59
# File 'lib/titi/adaptor.rb', line 55

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

Instance Method Details

#adapt(*args) {|_self| ... } ⇒ Object

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

Examples:

ActivityStreams::Entry.adapt(
  :id        => %Q{tag:twitter.com,2007:http://twitter.com/#{user.screen_name}/statuses/#{id}},
  :title     => text,
  :content   => text,
  :published => created_at,
  :verb      => :post
  ) do |entry|
  entry.has_author user.name, user.url
  entry.has_obj do |activity_obj|
    activity_obj.id        = id
    activity_obj.title     = text
    activity_obj.published = created_at
    activity_obj.updated   = created_at
    activity_obj.author    = entry.author
  end
end

Yields:

  • (_self)

Yield Parameters:

  • _self (Titi::Adaptor)

    the object that the method was called on



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

def adapt *args, &block
  hsh = args.extract_options!
  args_hsh = Hash.zip(self.members[0...args.length], args)
  self.attributes = hsh.merge(args_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