Class: Calculi::OptionSet

Inherits:
Object
  • Object
show all
Includes:
Utility, Enumerable
Defined in:
lib/calculi/option_set.rb

Constant Summary collapse

REQUIRED =
Object.new

Constants included from Utility

Utility::NON_IVAR, Utility::NULL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utility

#at_prefixed, #callable?, #constantly, #eval_or_value, #instance_variable_compute, #procable?, #set_attribute

Constructor Details

#initializeOptionSet

Returns a new instance of OptionSet.



14
15
16
# File 'lib/calculi/option_set.rb', line 14

def initialize
  @defined_options = {}
end

Instance Attribute Details

#defined_optionsObject (readonly)

Returns the value of attribute defined_options.



10
11
12
# File 'lib/calculi/option_set.rb', line 10

def defined_options
  @defined_options
end

Instance Method Details

#<<(option_name) ⇒ Object



26
27
28
29
30
# File 'lib/calculi/option_set.rb', line 26

def <<(option_name)
  define option_name

  return self
end

#add(*option_names) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/calculi/option_set.rb', line 18

def add(*option_names)
  option_names.flatten.each do |name|
    define name
  end

  return self
end

#define(option_name, default_value = REQUIRED) ⇒ Object Also known as: []=



32
33
34
35
36
37
38
# File 'lib/calculi/option_set.rb', line 32

def define(option_name, default_value = REQUIRED)
  defined_options.store(option_name, default_value)

  clear_sorted!

  return self
end

#fetch(option_name) ⇒ Object Also known as: []



42
43
44
# File 'lib/calculi/option_set.rb', line 42

def fetch(option_name)
  defined_options.fetch option_name
end

#process!(context, options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/calculi/option_set.rb', line 48

def process!(context, options)
  options = options.with_indifferent_access

  each do |name, default_value|
    value = options.fetch name do
      eval_or_value default_value, context: context
    end

    raise ArgumentError, "Missing required attribute: #{name}" if required?(value)

    set_attribute(name, value, context: context)
  end
end