Class: Compendium::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/compendium/option.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Option

Returns a new instance of Option.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
# File 'lib/compendium/option.rb', line 13

def initialize(hash = {})
  raise ArgumentError, "name must be provided" unless hash.key?(:name)

  @name = hash.delete(:name).to_sym
  @default = hash.delete(:default)
  @choices = hash.delete(:choices)
  self.type = hash.delete(:type)
  @options = hash.with_indifferent_access
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



27
28
29
30
31
# File 'lib/compendium/option.rb', line 27

def method_missing(name, *args, &block)
  return options[name] if options.key?(name)
  return options.key?(name[0...-1]) if name.to_s.end_with?('?')
  super
end

Instance Attribute Details

#choicesObject

Returns the value of attribute choices.



8
9
10
# File 'lib/compendium/option.rb', line 8

def choices
  @choices
end

#defaultObject

Returns the value of attribute default.



8
9
10
# File 'lib/compendium/option.rb', line 8

def default
  @default
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/compendium/option.rb', line 8

def name
  @name
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/compendium/option.rb', line 8

def options
  @options
end

#typeObject

Returns the value of attribute type.



8
9
10
# File 'lib/compendium/option.rb', line 8

def type
  @type
end

Instance Method Details

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

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/compendium/option.rb', line 33

def respond_to_missing?(name, include_private = false)
  return true if options.key?(name)
  super
end