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, spin_multiplicity: nil, atom_title: nil, x_fract: nil, y_fract: nil, z_fract: nil) ⇒ AtomBuilder

Returns a new instance of AtomBuilder.



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/asciichem/transform.rb', line 377

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,
               spin_multiplicity: nil, atom_title: nil,
               x_fract: nil, y_fract: nil, z_fract: 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
  @spin_multiplicity = spin_multiplicity
  @atom_title = atom_title
  @x_fract = x_fract
  @y_fract = y_fract
  @z_fract = z_fract
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.



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/asciichem/transform.rb', line 329

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,
    spin_multiplicity: hash[:spin_multiplicity]&.to_s,
    atom_title: hash[:atom_title]&.to_s,
    x_fract: float_or_nil(hash[:x_fract]),
    y_fract: float_or_nil(hash[:y_fract]),
    z_fract: float_or_nil(hash[:z_fract])
  )
end

Instance Method Details

#buildObject



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/asciichem/transform.rb', line 401

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,
    spin_multiplicity: @spin_multiplicity,
    atom_title: @atom_title,
    x_fract: @x_fract,
    y_fract: @y_fract,
    z_fract: @z_fract
  )
end