Class: AsciiChem::Model::Reaction

Inherits:
Node
  • Object
show all
Defined in:
lib/asciichem/model/reaction.rb

Overview

A chemical reaction: reactants, an arrow, and products. Conditions render above and below the arrow per IUPAC.

Defined Under Namespace

Classes: Conditions

Constant Summary collapse

ARROWS =
{
  forward:     { ascii: "->",   mathml_entity: "" },
  reverse:     { ascii: "<-",   mathml_entity: "" },
  equilibrium: { ascii: "<=>",  mathml_entity: "" },
  resonance:   { ascii: "<->",  mathml_entity: "" }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#==, #accept, #diagnostic_label, #hash, short_name, #to_cml, #to_html, #to_latex, #to_mathml, #to_svg, #to_text

Constructor Details

#initialize(reactants:, products:, arrow: :forward, conditions: nil) ⇒ Reaction

Returns a new instance of Reaction.



19
20
21
22
23
24
# File 'lib/asciichem/model/reaction.rb', line 19

def initialize(reactants:, products:, arrow: :forward, conditions: nil)
  @reactants = reactants
  @products = products
  @arrow = arrow
  @conditions = conditions || Conditions.new
end

Instance Attribute Details

#arrowObject

Returns the value of attribute arrow.



17
18
19
# File 'lib/asciichem/model/reaction.rb', line 17

def arrow
  @arrow
end

#conditionsObject

Returns the value of attribute conditions.



17
18
19
# File 'lib/asciichem/model/reaction.rb', line 17

def conditions
  @conditions
end

#productsObject

Returns the value of attribute products.



17
18
19
# File 'lib/asciichem/model/reaction.rb', line 17

def products
  @products
end

#reactantsObject

Returns the value of attribute reactants.



17
18
19
# File 'lib/asciichem/model/reaction.rb', line 17

def reactants
  @reactants
end

Instance Method Details

#arrow_asciiObject



35
36
37
# File 'lib/asciichem/model/reaction.rb', line 35

def arrow_ascii
  ARROWS.fetch(arrow).fetch(:ascii)
end

#arrow_entityObject



39
40
41
# File 'lib/asciichem/model/reaction.rb', line 39

def arrow_entity
  ARROWS.fetch(arrow).fetch(:mathml_entity)
end

#childrenObject



31
32
33
# File 'lib/asciichem/model/reaction.rb', line 31

def children
  reactants + products
end

#to_sObject



43
44
45
46
# File 'lib/asciichem/model/reaction.rb', line 43

def to_s
  "Reaction(#{reactants.map(&:to_s).join(' + ')} " \
    "#{arrow_ascii} #{products.map(&:to_s).join(' + ')})"
end

#value_attributesObject



26
27
28
29
# File 'lib/asciichem/model/reaction.rb', line 26

def value_attributes
  { reactants: reactants, products: products, arrow: arrow,
    conditions: conditions }
end