Class: Confuse::KeySplitter

Inherits:
Object
  • Object
show all
Defined in:
lib/confuse/key_splitter.rb

Overview

This class encapsulates the code required to support searching for items with a single key item even for nested items. config instead of config[:bar]

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ KeySplitter

Returns a new instance of KeySplitter.



8
9
10
# File 'lib/confuse/key_splitter.rb', line 8

def initialize(key)
  @key = key
end

Instance Method Details

#possible_namespacesObject

Returns an array of possible namespaces based on splitting the key at every underscore.



20
21
22
23
24
25
26
27
28
# File 'lib/confuse/key_splitter.rb', line 20

def possible_namespaces
  namespaces = []
  key = @key.to_s
  while (index = key.rindex('_'))
    key = key[0, index]
    namespaces << key.to_sym
  end
  namespaces
end

#rest_of_key(namespace) ⇒ Object

Returns the rest of the key for a given namespace



31
32
33
34
35
36
37
38
# File 'lib/confuse/key_splitter.rb', line 31

def rest_of_key(namespace)
  return nil if @key == namespace

  key = @key.to_s

  index = key.index(namespace.to_s) && (namespace.to_s.length + 1)
  key[index, key.length].to_sym if index
end

#splitObject



12
13
14
15
16
# File 'lib/confuse/key_splitter.rb', line 12

def split
  possible_namespaces.map do |ns|
    [ns, rest_of_key(ns)]
  end
end