Bobochka
9/7/2016 - 11:52 AM

Decorator chain POC

Decorator chain POC

class LineDataBuilder
  def self.for(lines)
    @lines = lines
  end

  def restricted_by(rules)
    @rules = rules
    self
  end

  def include_depots
    @with_depots = true
    self
  end

  def available_at(time)
    @available_at = time
    self
  end

  def build
    _lines = lines

    decorators.each{ |d| _lines = d.decorate(_lines) }

    _lines
  end

  private

  def decorators
    decorators = []

    decorators << AvailabilityDecorator.new(@rules, @available_at)
    decorators << RuleDecorator.new(@rules, @available_at)
    decorators << DepotDecorator.new(@rules, @division_id) if @division_id

    decorators
  end
  
  
end

LineDataBuilder
  .for(lines)
  .restricted_by(rules)
  .available_at(Time.current)
  .include_depots
  .build


  LineDataBuilder
    .for(lines)
    .restricted_by(rules)
    .build