Module: Schizo::Data

Defined in:
lib/schizo/data.rb

Overview

Include Data into a class to give it the ability to be adorned with a Role.

Instance Method Summary collapse

Instance Method Details

#as(*roles, &block) ⇒ Object

Adorn a Data object with a Role.

If a block is given, then a facade for self is yielded to the block and the return value is self with the facade’s instance variables copied over to it.

user.name = "callie"
user.as(Poster) do |poster|
  poster.name = "coco"
end
user.name # => "coco"

Without a block, a facade for self is returned.

user.name = "callie"
poster = user.as(Poster)
poster.name = "coco"
user.name # => "callie"


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/schizo/data.rb', line 21

def as(*roles, &block)
  first_role = roles.first
  rest_roles = *roles[1..-1]
  if rest_roles.empty?
    facade = Facade::ObjectBuilder.new(self, first_role).product
    if block_given?
      block.call(facade)
      facade.actualize
    else
      facade
    end
  else
    as(first_role).as(*rest_roles, &block)        
  end
end