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.



538
539
540
# File 'lib/asciichem/transform.rb', line 538

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

Instance Method Details

#buildObject



542
543
544
545
546
547
548
549
# File 'lib/asciichem/transform.rb', line 542

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