Module: CConfig::HashUtils::Extensions

Defined in:
lib/cconfig/hash_utils.rb

Overview

Extensions contains the methods to be provided for each hash object produced by this gem.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultsObject

Returns the value of attribute defaults.



27
28
29
# File 'lib/cconfig/hash_utils.rb', line 27

def defaults
  @defaults
end

Instance Method Details

#default_of(key) ⇒ Object

Returns the default value of the given key. Note that this key can specify nested values with the period notation (e.g. “a.b”).



55
56
57
58
59
60
61
62
63
64
# File 'lib/cconfig/hash_utils.rb', line 55

def default_of(key)
  cur = defaults

  key.split('.').each do |part|
    cur = cur[part]
    break if cur.nil?
  end

  cur
end

#disabled?(feature) ⇒ Boolean

Returns true if the given feature is disabled or doesn’t exist. This is a shorthand for ‘!enabled?`.

Returns:

  • (Boolean)


49
50
51
# File 'lib/cconfig/hash_utils.rb', line 49

def disabled?(feature)
  !enabled?(feature)
end

#enabled?(feature) ⇒ Boolean

Returns true if the given feature is enabled, false otherwise. This also works in embedded configuration values. For example: enabled?(“a.b”) will return true for:

a:
  b:
    enabled: true

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cconfig/hash_utils.rb', line 35

def enabled?(feature)
  cur   = self
  parts = feature.split('.')

  parts.each do |part|
    cur = cur[part]
    return false if !cur || cur.empty?
  end

  cur.key?('enabled') && cur['enabled'].eql?(true)
end