Class: Confuse::KeySplitter
- Inherits:
-
Object
- Object
- Confuse::KeySplitter
- Defined in:
- lib/confuse/key_splitter.rb
Overview
Instance Method Summary collapse
-
#initialize(key) ⇒ KeySplitter
constructor
A new instance of KeySplitter.
-
#possible_namespaces ⇒ Object
Returns an array of possible namespaces based on splitting the key at every underscore.
-
#rest_of_key(namespace) ⇒ Object
Returns the rest of the key for a given namespace.
- #split ⇒ Object
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_namespaces ⇒ Object
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 |
#split ⇒ Object
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 |