Class: Chemicalml::Dictionary::Enum

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

Overview

An entry's enum constraint. CML dictionary entries can be tagged as either an open set (the listed values are recommended but others are allowed) or a closed set (only the listed values are valid).

Constant Summary collapse

KINDS =
%i[open closed].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind: :open, values: []) ⇒ Enum

Returns a new instance of Enum.

Raises:

  • (ArgumentError)


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

def initialize(kind: :open, values: [])
  raise ArgumentError, "unknown enum kind: #{kind.inspect}" unless KINDS.include?(kind.to_sym)

  @kind = kind.to_sym
  @values = values.to_a
  freeze
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



12
13
14
# File 'lib/chemicalml/dictionary/enum.rb', line 12

def kind
  @kind
end

#valuesObject (readonly)

Returns the value of attribute values.



12
13
14
# File 'lib/chemicalml/dictionary/enum.rb', line 12

def values
  @values
end

Instance Method Details

#allows?(value) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'lib/chemicalml/dictionary/enum.rb', line 30

def allows?(value)
  return true if open?
  return true if values.empty?

  values.include?(value)
end

#closed?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/chemicalml/dictionary/enum.rb', line 26

def closed?
  kind == :closed
end

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

Returns:

  • (Boolean)


41
42
43
# File 'lib/chemicalml/dictionary/enum.rb', line 41

def eql?(other)
  other.is_a?(Enum) && kind == other.kind && values == other.values
end

#hashObject



46
47
48
# File 'lib/chemicalml/dictionary/enum.rb', line 46

def hash
  [kind, values].hash
end

#open?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/chemicalml/dictionary/enum.rb', line 22

def open?
  kind == :open
end

#to_hObject



37
38
39
# File 'lib/chemicalml/dictionary/enum.rb', line 37

def to_h
  { kind: kind, values: values }
end