Class: GOBL::Enum

Inherits:
Value show all
Defined in:
lib/gobl/enum.rb

Overview

Base class for single value structures in the GOBL schema that have a controlled set of values associated to them (i.e., an enumeration) that may constrain them

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Value

#==, #as_json, #eql?, #hash, #to_s, #to_sym

Methods inherited from Struct

#as_json, from_data, from_json!, #to_json

Constructor Details

#initialize(value) ⇒ Enum

Returns an enum value object that corresponds to a given value. The value can be a ‘Symbol`, a `String` or anything coercible into one (via `#to_s`)

Parameters:

  • value (Symbol, String, #to_s)

    the value of the object



13
14
15
16
17
18
19
# File 'lib/gobl/enum.rb', line 13

def initialize(value)
  if value.is_a? Symbol
    self._value = value_from_sym(value) || value.to_s
  else
    super
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Enables dynamic value inquirers for each of the declared enum values. Each inquirer returns a ‘Boolean` denoting whether the value equals the enquired value (`true`) or not (`false`)



31
32
33
34
35
36
37
# File 'lib/gobl/enum.rb', line 31

def method_missing(method_name, *args, &block)
  if (value = self.class.find_by_inquirer(method_name))
    self == value
  else
    super
  end
end

Class Method Details

.allArray<#namee}

Returns an array with all the enumerated objects of this type

Returns:

  • (Array<#namee})

    ] the array of enumerated objects



48
49
50
# File 'lib/gobl/enum.rb', line 48

def all
  self::ENUM.keys.map { |key| new(key) }
end

.find_by_inquirer(method_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
# File 'lib/gobl/enum.rb', line 58

def find_by_inquirer(method_name)
  method_name =~ /(.+)\?$/ && find_by_sym(Regexp.last_match(1).to_sym)
end

.find_by_sym(sym) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



53
54
55
# File 'lib/gobl/enum.rb', line 53

def find_by_sym(sym)
  all.find { |object| object.to_sym == sym }
end

Instance Method Details

#descriptionString

Returns the description of the current enum value

Returns:

  • (String)

    the description of the current enum value



24
25
26
# File 'lib/gobl/enum.rb', line 24

def description
  self.class::ENUM.fetch(_value, _value)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


40
41
42
# File 'lib/gobl/enum.rb', line 40

def respond_to_missing?(method_name, include_private = false)
  self.class.find_by_inquirer(method_name) || super
end