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.



402
403
404
405
406
# File 'lib/asciichem/transform.rb', line 402

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

Instance Method Details

#as_terms(side) ⇒ Object



436
437
438
439
440
441
442
# File 'lib/asciichem/transform.rb', line 436

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

#buildObject



408
409
410
411
412
413
414
415
# File 'lib/asciichem/transform.rb', line 408

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

#condition_text(node) ⇒ Object



429
430
431
432
433
434
# File 'lib/asciichem/transform.rb', line 429

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

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

#conditionsObject



421
422
423
424
425
426
427
# File 'lib/asciichem/transform.rb', line 421

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



417
418
419
# File 'lib/asciichem/transform.rb', line 417

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