Class: Draught::Transformations::Composer

Inherits:
Object
  • Object
show all
Defined in:
lib/draught/transformations/composer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transforms) ⇒ Composer

Returns a new instance of Composer.



12
13
14
# File 'lib/draught/transformations/composer.rb', line 12

def initialize(transforms)
  @transforms = transforms
end

Instance Attribute Details

#transformsObject (readonly)

Returns the value of attribute transforms.



10
11
12
# File 'lib/draught/transformations/composer.rb', line 10

def transforms
  @transforms
end

Class Method Details

.compose(*transforms) ⇒ Object



6
7
8
# File 'lib/draught/transformations/composer.rb', line 6

def self.compose(*transforms)
  new(transforms).composition
end

Instance Method Details

#coalesced_transformsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/draught/transformations/composer.rb', line 20

def coalesced_transforms
  return [] if transforms.empty?
  start_transforms = flattened_transforms
  finished = start_transforms.shift(1)
  return finished if start_transforms.empty?

  start_transforms.each do |next_transform|
    coalesce_pair(finished.pop, next_transform).each do |coalesced_transform|
      finished << coalesced_transform
    end
  end
  finished
end

#compositionObject



16
17
18
# File 'lib/draught/transformations/composer.rb', line 16

def composition
  Composition.new(coalesced_transforms)
end

#flattened_transformsObject



34
35
36
37
38
# File 'lib/draught/transformations/composer.rb', line 34

def flattened_transforms
  transforms.flat_map { |transform|
    transform.to_transform.transforms
  }
end