Class: AsciiChem::Model::Molecule

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

Overview

A molecule: an ordered sequence of atoms/groups with optional leading stoichiometric coefficient, optional stereochemistry marker (R/S/E/Z/α/β), and optional names/identifiers for CML metadata round-trip.

Constant Summary collapse

STEREO_LETTERS =
{
  "R" => :R,
  "S" => :S,
  "E" => :E,
  "Z" => :Z,
  "a" => :alpha,
  "alpha" => :alpha,
  "α" => :alpha,
  "b" => :beta,
  "beta" => :beta,
  "β" => :beta
}.freeze
STEREO_TO_LETTER =
{
  R: "R", S: "S", E: "E", Z: "Z",
  alpha: "alpha", beta: "beta"
}.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(nodes:, coefficient: nil, stereo: nil, names: [], identifiers: [], title: nil, formulas: [], properties: [], labels: [], metadata: []) ⇒ Molecule

Returns a new instance of Molecule.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/asciichem/model/molecule.rb', line 31

def initialize(nodes:, coefficient: nil, stereo: nil,
               names: [], identifiers: [], title: nil,
               formulas: [], properties: [], labels: [], metadata: [])
  @nodes = nodes
  @coefficient = coefficient
  @stereo = stereo
  @names = names
  @identifiers = identifiers
  @title = title
  @formulas = formulas
  @properties = properties
  @labels = labels
  @metadata = 
end

Instance Attribute Details

#coefficientObject

Returns the value of attribute coefficient.



28
29
30
# File 'lib/asciichem/model/molecule.rb', line 28

def coefficient
  @coefficient
end

#formulasObject

Returns the value of attribute formulas.



28
29
30
# File 'lib/asciichem/model/molecule.rb', line 28

def formulas
  @formulas
end

#identifiersObject

Returns the value of attribute identifiers.



28
29
30
# File 'lib/asciichem/model/molecule.rb', line 28

def identifiers
  @identifiers
end

#labelsObject

Returns the value of attribute labels.



28
29
30
# File 'lib/asciichem/model/molecule.rb', line 28

def labels
  @labels
end

#metadataObject

Returns the value of attribute metadata.



28
29
30
# File 'lib/asciichem/model/molecule.rb', line 28

def 
  @metadata
end

#namesObject

Returns the value of attribute names.



28
29
30
# File 'lib/asciichem/model/molecule.rb', line 28

def names
  @names
end

#nodesObject

Returns the value of attribute nodes.



28
29
30
# File 'lib/asciichem/model/molecule.rb', line 28

def nodes
  @nodes
end

#propertiesObject

Returns the value of attribute properties.



28
29
30
# File 'lib/asciichem/model/molecule.rb', line 28

def properties
  @properties
end

#stereoObject

Returns the value of attribute stereo.



28
29
30
# File 'lib/asciichem/model/molecule.rb', line 28

def stereo
  @stereo
end

#titleObject

Returns the value of attribute title.



28
29
30
# File 'lib/asciichem/model/molecule.rb', line 28

def title
  @title
end

Instance Method Details

#childrenObject



53
54
55
# File 'lib/asciichem/model/molecule.rb', line 53

def children
  nodes
end

#stereo_letterObject



57
58
59
# File 'lib/asciichem/model/molecule.rb', line 57

def stereo_letter
  STEREO_TO_LETTER.fetch(stereo) if stereo
end

#to_sObject



61
62
63
64
65
66
67
# File 'lib/asciichem/model/molecule.rb', line 61

def to_s
  prefix = coefficient ? "#{coefficient}" : ""
  stereo_str = stereo ? "(#{stereo_letter})-" : ""
  name_str = names.empty? ? "" : " #{names.map(&:to_s).join(', ')}"
  id_str = identifiers.empty? ? "" : " #{identifiers.map(&:to_s).join(', ')}"
  "#{stereo_str}#{prefix}Molecule[#{nodes.map(&:to_s).join(', ')}]#{name_str}#{id_str}"
end

#value_attributesObject



46
47
48
49
50
51
# File 'lib/asciichem/model/molecule.rb', line 46

def value_attributes
  { nodes: nodes, coefficient: coefficient, stereo: stereo,
    names: names, identifiers: identifiers, title: title,
    formulas: formulas, properties: properties, labels: labels,
    metadata:  }
end