Class: Chemicalml::Dictionary::Enum
- Inherits:
-
Object
- Object
- Chemicalml::Dictionary::Enum
- 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
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Instance Method Summary collapse
- #allows?(value) ⇒ Boolean
- #closed? ⇒ Boolean
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
-
#initialize(kind: :open, values: []) ⇒ Enum
constructor
A new instance of Enum.
- #open? ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(kind: :open, values: []) ⇒ Enum
Returns a new instance of Enum.
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
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
12 13 14 |
# File 'lib/chemicalml/dictionary/enum.rb', line 12 def kind @kind end |
#values ⇒ Object (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
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
26 27 28 |
# File 'lib/chemicalml/dictionary/enum.rb', line 26 def closed? kind == :closed end |
#eql?(other) ⇒ Boolean Also known as: ==
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 |
#hash ⇒ Object
46 47 48 |
# File 'lib/chemicalml/dictionary/enum.rb', line 46 def hash [kind, values].hash end |
#open? ⇒ Boolean
22 23 24 |
# File 'lib/chemicalml/dictionary/enum.rb', line 22 def open? kind == :open end |
#to_h ⇒ Object
37 38 39 |
# File 'lib/chemicalml/dictionary/enum.rb', line 37 def to_h { kind: kind, values: values } end |