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

#description_for(name) ⇒ Object



36
37
38
# File 'lib/smart_config/values.rb', line 36

def description_for(name)
  (@config || {})[name.to_sym]&.fetch(:description, '') || ''
end

#example_for(name) ⇒ Object



40
41
42
# File 'lib/smart_config/values.rb', line 40

def example_for(name)
  (@config || {})[name.to_sym]&.fetch(:example, '') || ''
end

#format_value(name, value) ⇒ Object



54
55
56
# File 'lib/smart_config/values.rb', line 54

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

#get_value(name) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/smart_config/values.rb', line 44

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, *opts) ⇒ Object



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

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

#keysObject



32
33
34
# File 'lib/smart_config/values.rb', line 32

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

#value(name, *opts) ⇒ Object



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

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