Class: Chemicalml::Dictionary::Entry

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

Overview

One dictionary entry. Plain Ruby value object — format-agnostic. The YAML shape mirrors the structure documented in TODO.cml-full/05-dictionary-layer.md.

Constant Summary collapse

ID_PATTERN =
/\A[A-Za-z][A-Za-z0-9._-]*\z/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, term:, definition:, description: nil, data_type: nil, unit_type: nil, units: nil, enum: nil, links: [], source_code: nil) ⇒ Entry

Returns a new instance of Entry.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/chemicalml/dictionary/entry.rb', line 15

def initialize(id:, term:, definition:, description: nil,
               data_type: nil, unit_type: nil, units: nil,
               enum: nil, links: [], source_code: nil)
  raise ArgumentError, "invalid entry id: #{id.inspect}" unless id.to_s.match?(ID_PATTERN)

  @id          = id.to_s
  @term        = term
  @definition  = definition
  @description = description
  @data_type   = data_type
  @unit_type   = unit_type
  @units       = units
  @enum        = enum
  @links       = links.to_a
  @source_code = source_code
  freeze
end

Instance Attribute Details

#data_typeObject (readonly)

Returns the value of attribute data_type.



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

def data_type
  @data_type
end

#definitionObject (readonly)

Returns the value of attribute definition.



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

def definition
  @definition
end

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#enumObject (readonly)

Returns the value of attribute enum.



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

def enum
  @enum
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

Returns the value of attribute links.



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

def links
  @links
end

#source_codeObject (readonly)

Returns the value of attribute source_code.



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

def source_code
  @source_code
end

#termObject (readonly)

Returns the value of attribute term.



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

def term
  @term
end

#unit_typeObject (readonly)

Returns the value of attribute unit_type.



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

def unit_type
  @unit_type
end

#unitsObject (readonly)

Returns the value of attribute units.



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

def units
  @units
end

Instance Method Details

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

Returns:

  • (Boolean)


49
50
51
# File 'lib/chemicalml/dictionary/entry.rb', line 49

def eql?(other)
  other.is_a?(Entry) && id == other.id && to_h == other.to_h
end

#hashObject



54
55
56
# File 'lib/chemicalml/dictionary/entry.rb', line 54

def hash
  [id, to_h].hash
end

#to_hObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/chemicalml/dictionary/entry.rb', line 33

def to_h
  h = {
    id: id,
    term: term,
    definition: definition
  }
  h[:description] = description if description
  h[:data_type]   = data_type   if data_type
  h[:unit_type]   = unit_type   if unit_type
  h[:units]       = units       if units
  h[:enum]        = enum.to_h   if enum
  h[:links]       = links.map(&:to_h) unless links.empty?
  h[:source_code] = source_code if source_code
  h
end