Module: SmartConfig::Values

Included in:
Config, Group
Defined in:
lib/smart_config/values.rb

Overview

Handles the retrieval of values, but with already provided data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



10
11
12
# File 'lib/smart_config/values.rb', line 10

def namespace
  @namespace
end

Instance Method Details

#format_value(name, value) ⇒ Object



43
44
45
# File 'lib/smart_config/values.rb', line 43

def format_value(name, value)
  @config[name][:formatter].format(value)
end

#get_value(name) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/smart_config/values.rb', line 33

def get_value(name)
  return @config[name][:group] if @config[name].key?(:group)

  path = walker.walk(full_name(name))
  return path.first unless path.first.nil?
  return @config[name][:default] if @config[name].key?(:default)

  raise SmartConfig::MissingConfigValue, full_name(name)
end

#group(name) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/smart_config/values.rb', line 21

def group(name, &)
  @config ||= {}
  key = name.to_sym
  @config[key] ||= {}
  @config[key][:group] = SmartConfig::Group.new([namespace, name].compact.flatten, method(:walker), &)
  define_singleton_method(name) { get_value(key) }
end

#keysObject



29
30
31
# File 'lib/smart_config/values.rb', line 29

def keys
  (@config || {}).keys
end

#value(name, *opts) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/smart_config/values.rb', line 12

def value(name, *opts)
  @config ||= {}
  key = name.to_sym
  config = opts.reduce({}, :merge)
  config[:formatter] = SmartConfig::Formatters.find(config.fetch(:format, :string))
  @config[key] = config
  define_singleton_method(name) { format_value(key, get_value(key)) }
end