Class: AsciiChem::Transform::AtomBuilder

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

Overview

Builds an Atom, normalising nil/empty strings and pulling apart composite superscripts (charge vs oxidation state vs raw). Exactly one of { charge, oxidation_state, superscript } is set on the resulting atom — they are mutually exclusive views of the superscript position.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element, isotope: nil, subscript: nil, superscript: nil, lone_pairs: nil, radical_electrons: nil, ring_closures: nil, x2: nil, y2: nil, z2: nil, atom_parity: nil) ⇒ AtomBuilder

Returns a new instance of AtomBuilder.



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/asciichem/transform.rb', line 305

def initialize(element, isotope: nil, subscript: nil, superscript: nil,
               lone_pairs: nil, radical_electrons: nil,
               ring_closures: nil,
               x2: nil, y2: nil, z2: nil, atom_parity: nil)
  @element = element
  @isotope = isotope
  @subscript = subscript
  @superscript = superscript
  @lone_pairs = lone_pairs
  @radical_electrons = radical_electrons
  @ring_closures = ring_closures
  @x2 = x2
  @y2 = y2
  @z2 = z2
  @atom_parity = atom_parity
end

Class Method Details

.from_parse_tree(attrs) ⇒ Object

Construct from a parse-tree hash. The grammar wraps atoms in .as(:atom), so the transform receives one hash with any of these keys present: :element, :isotope, :subscript, :superscript, :lone_pairs, :radical_electrons, :ring_closures. Absent keys default to nil. Lewis markers (:lone_pairs, :radical_electrons) are captured as colon/dot strings whose length is the count.



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/asciichem/transform.rb', line 262

def self.from_parse_tree(attrs)
  hash = attrs.is_a?(Hash) ? attrs : {}
  new(
    hash[:element],
    isotope: hash[:isotope],
    subscript: hash[:subscript],
    superscript: hash[:superscript],
    lone_pairs: lewis_count(hash[:lone_pairs]),
    radical_electrons: lewis_count(hash[:radical_electrons]),
    ring_closures: ring_closures_string(hash[:ring_closures]),
    x2: float_or_nil(hash[:x2]),
    y2: float_or_nil(hash[:y2]),
    z2: float_or_nil(hash[:z2]),
    atom_parity: hash[:atom_parity]&.to_s
  )
end

Instance Method Details

#buildObject



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/asciichem/transform.rb', line 322

def build
  charge = detected_charge
  oxidation = detected_oxidation_state
  Model::Atom.new(
    element: @element.to_s,
    isotope: strip_marker(@isotope),
    subscript: strip_marker(@subscript, '_'),
    superscript: raw_superscript(charge, oxidation),
    charge: charge,
    oxidation_state: oxidation,
    lone_pairs: positive_int(@lone_pairs),
    radical_electrons: positive_int(@radical_electrons),
    ring_closures: @ring_closures,
    x2: @x2,
    y2: @y2,
    z2: @z2,
    atom_parity: @atom_parity
  )
end