Module: Disposable::Composition

Defined in:
lib/disposable/composition.rb

Overview

Composition delegates accessors to models as per configuration. Composition doesn’t know anything but methods (readers and writers) to expose and the mappings to the internal models. Optionally, it knows about renamings such as mapping #song_id to song.id.

class Album
  include Disposable::Composition

  map( {cd: [[:id], [:name]], band: [[:id, :band_id], [:title]]} )
end

album = Album.new(cd: CD.find(1), band: Band.new)
album.id #=> 1
album.title = "Ten Foot Pole"

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.extend(Forwardable)
  base.extend(ClassMethods)
end

Instance Method Details

#each(&block) ⇒ Object

Allows multiplexing method calls to all composed models.



62
63
64
# File 'lib/disposable/composition.rb', line 62

def each(&block)
  _models.each(&block)
end

#initialize(models) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/disposable/composition.rb', line 53

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

  @_models = models.values
end