Class: ConfigVar::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/configvar/context.rb

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



3
4
5
6
# File 'lib/configvar/context.rb', line 3

def initialize
  @definitions = {}
  @values = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Fetch a configuration value. The name must be a lowercase version of an uppercase name defined in the environment. A NoMethodError is raised if no value matching the specified name is available.



19
20
21
22
23
24
25
26
# File 'lib/configvar/context.rb', line 19

def method_missing(name, *args)
  value = @values[name]
  if value.nil? && !@values.has_key?(name)
    address = "<#{self.class.name}:0x00#{(self.object_id << 1).to_s(16)}>"
    raise NoMethodError.new("undefined method `#{name}' for ##{address}")
  end
  value
end

Instance Method Details

#optional_bool(name, default) ⇒ Object

Define a required boolean config var with a default value.



94
95
96
97
98
99
100
101
102
# File 'lib/configvar/context.rb', line 94

def optional_bool(name, default)
  optional_custom(name) do |env|
    if value = env[name.to_s.upcase]
      {name => parse_bool(name, value)}
    else
      {name => default}
    end
  end
end

#optional_custom(name, &blk) ⇒ Object

Define an optional custom config var. The block must take the environment as a parameter, load and process values from the it, and return either a single value to bind to the name of the configuration variable or a Hash that will be merged into the collection of all config vars.



109
110
111
# File 'lib/configvar/context.rb', line 109

def optional_custom(name, &blk)
  define_config(name, &blk)
end

#optional_int(name, default) ⇒ Object

Define a required integer config var with a default value.



83
84
85
86
87
88
89
90
91
# File 'lib/configvar/context.rb', line 83

def optional_int(name, default)
  optional_custom(name) do |env|
    if value = env[name.to_s.upcase]
      {name => parse_int(name, value)}
    else
      {name => default}
    end
  end
end

#optional_string(name, default) ⇒ Object

Define a required string config var with a default value.



72
73
74
75
76
77
78
79
80
# File 'lib/configvar/context.rb', line 72

def optional_string(name, default)
  optional_custom(name) do |env|
    if value = env[name.to_s.upcase]
      {name => value}
    else
      {name => default}
    end
  end
end

#reload(env) ⇒ Object

Reload the environment from a Hash of available environment values.



9
10
11
12
13
14
# File 'lib/configvar/context.rb', line 9

def reload(env)
  @values = {}
  @definitions.each_value do |function|
    @values.merge!(function.call(env))
  end
end

#required_bool(name) ⇒ Object

Define a required boolean config var.



51
52
53
54
55
56
57
58
59
# File 'lib/configvar/context.rb', line 51

def required_bool(name)
  required_custom(name) do |env|
    if value = env[name.to_s.upcase]
      {name => parse_bool(name, value)}
    else
      raise ConfigError.new("A value must be provided for #{name.to_s.upcase}")
    end
  end
end

#required_custom(name, &blk) ⇒ Object

Define a required custom config var. The block must take the environment as a parameter, load and process values from the it, and return either a single value to bind to the name of the configuration variable or a Hash that will be merged into the collection of all config vars. If a required value is not found in the environment the block must raise a ConfigVar::ConfigError exception.



67
68
69
# File 'lib/configvar/context.rb', line 67

def required_custom(name, &blk)
  define_config(name, &blk)
end

#required_int(name) ⇒ Object

Define a required integer config var.



40
41
42
43
44
45
46
47
48
# File 'lib/configvar/context.rb', line 40

def required_int(name)
  required_custom(name) do |env|
    if value = env[name.to_s.upcase]
      {name => parse_int(name, value)}
    else
      raise ConfigError.new("A value must be provided for #{name.to_s.upcase}")
    end
  end
end

#required_string(name) ⇒ Object

Define a required string config var.



29
30
31
32
33
34
35
36
37
# File 'lib/configvar/context.rb', line 29

def required_string(name)
  required_custom(name) do |env|
    if value = env[name.to_s.upcase]
      {name => value}
    else
      raise ConfigError.new("A value must be provided for #{name.to_s.upcase}")
    end
  end
end