Class: Vedeu::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/support/options.rb

Overview

Converts an options Hash into a class containing methods for each of the keys, which when called returns the value associated. When the value is either true or false, an additional predicate method is created.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Vedeu::Options

Parameters:

  • options (Hash) (defaults to: {})


20
21
22
23
# File 'lib/vedeu/support/options.rb', line 20

def initialize(options = {})
  @_options = options
  @_defined = []
end

Instance Attribute Details

#_definedArray<Symbol> (readonly)

Returns:

  • (Array<Symbol>)


10
11
12
# File 'lib/vedeu/support/options.rb', line 10

def _defined
  @_defined
end

#_optionsHash (readonly, protected)

Returns:

  • (Hash)


42
43
44
# File 'lib/vedeu/support/options.rb', line 42

def _options
  @_options
end

Class Method Details

.build(options = {}) ⇒ Vedeu::Options

Parameters:

  • options (Hash) (defaults to: {})

Returns:



14
15
16
# File 'lib/vedeu/support/options.rb', line 14

def self.build(options = {})
  new(options)._build
end

Instance Method Details

#_buildVedeu::Options

Returns:



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vedeu/support/options.rb', line 26

def _build
  _options.each do |name, value|
    _create_method(name) { value }

    if value.is_a?(TrueClass) || value.is_a?(FalseClass)
      _create_alias("#{name}?", name)
    end
  end

  self
end

#_create_alias(alias_name, method_name) ⇒ void (private)

This method returns an undefined value.

Parameters:

  • alias_name (String|Symbol)
  • method_name (String|Symbol)


49
50
51
52
53
# File 'lib/vedeu/support/options.rb', line 49

def _create_alias(alias_name, method_name)
  @_defined << alias_name.to_sym

  define_singleton_method(alias_name, method(method_name))
end

#_create_method(name, &block) ⇒ void (private)

This method returns an undefined value.

Parameters:

  • name (String|Symbol)
  • block (Proc)


58
59
60
61
62
# File 'lib/vedeu/support/options.rb', line 58

def _create_method(name, &block)
  @_defined << name.to_sym

  self.class.send(:define_method, name, &block)
end