Module: Chemicalml::Cml::Visitable

Defined in:
lib/chemicalml/cml/visitable.rb

Overview

Marker mixin included by every wire class. Provides a uniform interface the convention-constraint walker relies on:

- `wire_children` returns the child wire nodes
- `node_id` returns the id attribute or `nil`
- `element_name` returns the XML tag name

All three use lutaml-model's attribute registry — no duck typing. A wire class that doesn't declare an :id attribute simply has node_id return nil.

Instance Method Summary collapse

Instance Method Details

#collect_wire_nodes(value) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/chemicalml/cml/visitable.rb', line 36

def collect_wire_nodes(value)
  return [] if value.nil?

  if value.is_a?(::Array)
    value.select { |v| wire_node?(v) }
  elsif wire_node?(value)
    [value]
  else
    []
  end
end

#declared_attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/chemicalml/cml/visitable.rb', line 58

def declared_attribute?(name)
  self.class.model.attributes.key?(name)
rescue StandardError
  false
end

#declared_attribute_namesObject



52
53
54
55
56
# File 'lib/chemicalml/cml/visitable.rb', line 52

def declared_attribute_names
  self.class.model.attributes.keys
rescue StandardError
  []
end

#element_nameObject



28
29
30
31
32
33
34
# File 'lib/chemicalml/cml/visitable.rb', line 28

def element_name
  xml_mapping = self.class.mappings[:xml]
  root = xml_mapping && xml_mapping.root
  root && root.name || self.class.name.split("::").last
rescue StandardError
  self.class.name.split("::").last
end

#node_idObject



22
23
24
25
26
# File 'lib/chemicalml/cml/visitable.rb', line 22

def node_id
  return id if declared_attribute?(:id)

  nil
end

#wire_childrenObject



16
17
18
19
20
# File 'lib/chemicalml/cml/visitable.rb', line 16

def wire_children
  declared_attribute_names.flat_map do |name|
    collect_wire_nodes(public_send(name))
  end
end

#wire_node?(value) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/chemicalml/cml/visitable.rb', line 48

def wire_node?(value)
  value.is_a?(Lutaml::Model::Serializable)
end