Class: Chemicalml::Dictionary::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/chemicalml/dictionary/model.rb

Overview

The canonical dictionary model: namespace, prefix, title, description, and an ordered collection of Entry instances.

Plain Ruby — no XML, no YAML. Loading from YAML goes through Loader; the wire (XML) shape is Chemicalml::Cml::Dictionary.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace:, prefix:, title:, description: nil, entries: []) ⇒ Model

Returns a new instance of Model.



13
14
15
16
17
18
19
20
# File 'lib/chemicalml/dictionary/model.rb', line 13

def initialize(namespace:, prefix:, title:, description: nil, entries: [])
  @namespace   = namespace
  @prefix      = prefix
  @title       = title
  @description = description
  @entries     = entries.to_a
  freeze
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



11
12
13
# File 'lib/chemicalml/dictionary/model.rb', line 11

def description
  @description
end

#entriesObject (readonly)

Returns the value of attribute entries.



11
12
13
# File 'lib/chemicalml/dictionary/model.rb', line 11

def entries
  @entries
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



11
12
13
# File 'lib/chemicalml/dictionary/model.rb', line 11

def namespace
  @namespace
end

#prefixObject (readonly)

Returns the value of attribute prefix.



11
12
13
# File 'lib/chemicalml/dictionary/model.rb', line 11

def prefix
  @prefix
end

#titleObject (readonly)

Returns the value of attribute title.



11
12
13
# File 'lib/chemicalml/dictionary/model.rb', line 11

def title
  @title
end

Instance Method Details

#entry_idsObject



34
35
36
# File 'lib/chemicalml/dictionary/model.rb', line 34

def entry_ids
  entries.map(&:id)
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


48
49
50
# File 'lib/chemicalml/dictionary/model.rb', line 48

def eql?(other)
  other.is_a?(Model) && namespace == other.namespace && to_h == other.to_h
end

#hashObject



53
54
55
# File 'lib/chemicalml/dictionary/model.rb', line 53

def hash
  [namespace, to_h].hash
end

#lookup(qualified_name) ⇒ Object



22
23
24
25
26
27
# File 'lib/chemicalml/dictionary/model.rb', line 22

def lookup(qualified_name)
  _, id = split_qname(qualified_name)
  return nil unless id

  entries.find { |e| e.id == id }
end

#lookup!(qualified_name) ⇒ Object



29
30
31
32
# File 'lib/chemicalml/dictionary/model.rb', line 29

def lookup!(qualified_name)
  lookup(qualified_name) ||
    raise(KeyError, "no entry #{qualified_name.inspect} in #{namespace}")
end

#to_hObject



38
39
40
41
42
43
44
45
46
# File 'lib/chemicalml/dictionary/model.rb', line 38

def to_h
  {
    namespace: namespace,
    prefix: prefix,
    title: title,
    description: description,
    entries: entries.map(&:to_h)
  }
end