Class: Titi::Provider::ActivityStreams::ActivityStreamsStruct

Inherits:
Struct
  • Object
show all
Includes:
Adaptor
Defined in:
lib/titi/provider/activity_streams/activity_streams_struct.rb

Instance Method Summary collapse

Methods included from Adaptor

#adapt, included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Use method_missing to dispatch the has_ setters



30
31
32
33
34
35
36
# File 'lib/titi/provider/activity_streams/activity_streams_struct.rb', line 30

def method_missing meth, *args, &block
  if (meth.to_s =~ /has_(\w+)/) && (self.respond_to?("#{$1}="))
    has_thingy $1, *args, &block
  else
    super
  end
end

Instance Method Details

#attributes=(hsh) ⇒ Object

Merge given attributes into the hash



6
7
8
9
10
11
12
13
# File 'lib/titi/provider/activity_streams/activity_streams_struct.rb', line 6

def attributes= hsh
  conv_hsh = {}
  hsh.each do |k,v|
    k = k.to_s.gsub(/:/,'_') if k =~ /:/
    conv_hsh[k] = v
  end
  super hsh
end

#has_thingy(thingy, *args, &block) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/titi/provider/activity_streams/activity_streams_struct.rb', line 38

def has_thingy thingy, *args, &block
  thingy_klass  = ('Titi::Provider::ActivityStreams::'+thingy.to_s.camelize).constantize
  thingy_setter = "#{thingy}="
  self.send(thingy_setter, thingy_klass.new) unless self.send(thingy)
  self.send(thingy).adapt *args, &block
  self.send(thingy)
end

#to_hash(*args) ⇒ Object

expand to hash, and expand all hash values to hash



16
17
18
19
20
21
22
# File 'lib/titi/provider/activity_streams/activity_streams_struct.rb', line 16

def to_hash *args
  hsh = super(*args)
  hsh.each do |attr, val|
    hsh[attr] = val.to_hash if val.respond_to?(:to_hash)
  end
  hsh
end