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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



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

def method_missing(name, *args, &)
  return format_value(name, get_value(name)) if keys.include?(name)

  super
end

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



49
50
51
52
53
54
# File 'lib/smart_config/values.rb', line 49

def format_value(name, value)
  @formatters ||= {}

  @formatters[name] ||= SmartConfig::Formatters.find(@config[name].fetch(:format, :string))
  @formatters[name].format(value)
end

#get_value(name) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/smart_config/values.rb', line 39

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



17
18
19
20
21
# File 'lib/smart_config/values.rb', line 17

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

#keysObject



23
24
25
# File 'lib/smart_config/values.rb', line 23

def keys
  @config.keys
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(name, include_private = false)
  return true if keys.include?(name)

  super
end

#value(name, *opts) ⇒ Object



12
13
14
15
# File 'lib/smart_config/values.rb', line 12

def value(name, *opts)
  @config ||= {}
  @config[name.to_sym] = opts.reduce({}, :merge)
end