Class: AsciiChem::Transform::ReactionBuilder

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

Overview

Builds a Reaction, mapping the arrow hash into arrow kind and conditions.

Constant Summary collapse

ARROW_KINDS =
{
  '<=>' => :equilibrium,
  '<->' => :resonance,
  '->' => :forward,
  '<-' => :reverse
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(reactants, arrow, products) ⇒ ReactionBuilder

Returns a new instance of ReactionBuilder.



486
487
488
489
490
# File 'lib/asciichem/transform.rb', line 486

def initialize(reactants, arrow, products)
  @reactants = reactants
  @arrow = arrow
  @products = products
end

Instance Method Details

#as_terms(side) ⇒ Object



520
521
522
523
524
525
526
# File 'lib/asciichem/transform.rb', line 520

def as_terms(side)
  case side
  when Array then side
  when nil   then []
  else            [side]
  end
end

#buildObject



492
493
494
495
496
497
498
499
# File 'lib/asciichem/transform.rb', line 492

def build
  Model::Reaction.new(
    reactants: as_terms(@reactants),
    products: as_terms(@products),
    arrow: kind,
    conditions: conditions
  )
end

#condition_text(node) ⇒ Object



513
514
515
516
517
518
# File 'lib/asciichem/transform.rb', line 513

def condition_text(node)
  return nil if node.nil?

  text = node.is_a?(Hash) ? node[:text] : node
  text&.to_s
end

#conditionsObject



505
506
507
508
509
510
511
# File 'lib/asciichem/transform.rb', line 505

def conditions
  above = condition_text(@arrow[:above])
  below = condition_text(@arrow[:below])
  return nil unless above || below

  Model::Reaction::Conditions.new(above: above, below: below)
end

#kindObject



501
502
503
# File 'lib/asciichem/transform.rb', line 501

def kind
  ARROW_KINDS.fetch(@arrow[:kind].to_s, :forward)
end