Class: Disposable::Composition

Inherits:
Expose
  • Object
show all
Defined in:
lib/disposable/composition.rb

Overview

Composition allows renaming properties and combining one or more objects in order to expose a different API. It can be configured from any Representable schema.

class AlbumTwin < Disposable::Twin
  property :name, on: :artist
end

class AlbumExpose < Disposable::Composition
  from AlbumTwin
end

AlbumExpose.new(artist: OpenStruct.new(name: "AFI")).name #=> "AFI"

Instance Method Summary collapse

Methods inherited from Expose

from

Methods included from Expose::Save

#save

Constructor Details

#initialize(models) ⇒ Composition

Returns a new instance of Composition.



16
17
18
19
20
21
22
# File 'lib/disposable/composition.rb', line 16

def initialize(models)
  models.each do |name, model|
    instance_variable_set(:"@#{name}", model)
  end

  @_models = models
end

Instance Method Details

#[](name) ⇒ Object

Allows accessing the contained models.



25
26
27
# File 'lib/disposable/composition.rb', line 25

def [](name)
  instance_variable_get("@#{name}")
end

#each(&block) ⇒ Object



29
30
31
32
# File 'lib/disposable/composition.rb', line 29

def each(&block)
  # TODO: test me.
  @_models.values.each(&block)
end