Class: AsciiChem::Transform::CascadeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/asciichem/transform.rb

Overview

Builds a ReactionCascade. Parslet delivers the cascade shape in one of two forms depending on how the grammar's sequence flattened:

- Array of segment hashes:
[{ first: <Reaction> }, { arrow:, products: }, ...]
- Single hash with array values:
{ first: <Reaction>, arrow: [...], products: [...] }

Normalise to a canonical form before building.

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ CascadeBuilder

Returns a new instance of CascadeBuilder.



454
455
456
# File 'lib/asciichem/transform.rb', line 454

def initialize(data)
  @first, @tail = canonicalise(data)
end

Instance Method Details

#buildObject



458
459
460
461
462
463
464
465
# File 'lib/asciichem/transform.rb', line 458

def build
  steps = [@first]
  @tail.each do |arrow, products|
    prev_products = steps.last.products
    steps << ReactionBuilder.new(prev_products, arrow, products).build
  end
  Model::ReactionCascade.new(steps: steps)
end