Class: ConfigModule::ConfigOption

Inherits:
OpenStruct
  • Object
show all
Includes:
Enumerable
Defined in:
lib/config_module/config_option.rb

Defined Under Namespace

Classes: NotFoundError

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/config_module/config_option.rb', line 48

def method_missing name, *args, &block
  result = super

  if result || @table.has_key?(name)
    self.class.wrap result
  else
    raise(
      ConfigOption::NotFoundError.new(name, self, caller),
      "Key not found: #{name}", caller(3)
    )
  end
rescue NoMethodError => error
  raise(
    ConfigOption::NotFoundError.new(name, self, error),
    error.message, caller(3)
  )
end

Class Method Details

.wrap(data) ⇒ Object


7
8
9
10
11
12
13
# File 'lib/config_module/config_option.rb', line 7

def self.wrap data
  if data.is_a? Hash
    new data
  else
    data
  end
end

Instance Method Details

#[](name) ⇒ Object


38
39
40
# File 'lib/config_module/config_option.rb', line 38

def [] name
  @table[name.to_sym]
end

#each_keyObject


23
24
25
26
27
28
# File 'lib/config_module/config_option.rb', line 23

def each_key
  return to_enum(__method__) { @table.size } unless block_given?

  @table.each_key { |key| yield key }
  self
end

#each_pairObject Also known as: each


15
16
17
18
19
20
# File 'lib/config_module/config_option.rb', line 15

def each_pair
  return to_enum(__method__) { @table.size } unless block_given?

  @table.each_pair { |pair| yield pair }
  self
end

#each_valueObject


30
31
32
33
34
35
# File 'lib/config_module/config_option.rb', line 30

def each_value
  return to_enum(__method__) { @table.size } unless block_given?

  @table.each_value { |value| yield value }
  self
end

#has_key?(key) ⇒ Boolean Also known as: key?

Returns:

  • (Boolean)

43
44
45
# File 'lib/config_module/config_option.rb', line 43

def has_key? key
  @table.has_key? key.to_sym
end