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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/config_module/config_option.rb', line 26

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

  if result || @table.has_key?(name) then
    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



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

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

Instance Method Details

#[](name) ⇒ Object



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

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

#eachObject



13
14
15
16
# File 'lib/config_module/config_option.rb', line 13

def each
  return to_enum __method__ unless block_given?
  @table.each_pair{|p| yield p}
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/config_module/config_option.rb', line 22

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

#new_ostruct_member(name) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/config_module/config_option.rb', line 45

def new_ostruct_member name
  name = name.to_sym
  unless respond_to? name
    define_singleton_method(name) { self.class.wrap @table[name] }
    define_singleton_method("#{name}=") { |x| modifiable[name] = x }
  end
  name
end