Class: Confuse::Definition
- Inherits:
-
Object
- Object
- Confuse::Definition
- Defined in:
- lib/confuse/definition.rb
Instance Method Summary collapse
- #add_item(name, opts = {}) ⇒ Object
- #add_namespace(name, &block) ⇒ Object
- #defines?(name) ⇒ Boolean
- #find_item(namespace, key) ⇒ Object
-
#initialize(&block) ⇒ Definition
constructor
A new instance of Definition.
- #namespace_and_key(name) ⇒ Object
- #namespaces ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(&block) ⇒ Definition
Returns a new instance of Definition.
5 6 7 8 |
# File 'lib/confuse/definition.rb', line 5 def initialize(&block) @default_namespace = :default block.call(self) end |
Instance Method Details
#add_item(name, opts = {}) ⇒ Object
19 20 21 |
# File 'lib/confuse/definition.rb', line 19 def add_item(name, opts = {}) namespaces[@default_namespace].add_item(name, opts) end |
#add_namespace(name, &block) ⇒ Object
14 15 16 17 |
# File 'lib/confuse/definition.rb', line 14 def add_namespace(name, &block) new_namespace = Namespace.new(name, &block) namespaces[name.to_sym] = new_namespace end |
#defines?(name) ⇒ Boolean
10 11 12 |
# File 'lib/confuse/definition.rb', line 10 def defines?(name) !!find_item_by_name(name) end |
#find_item(namespace, key) ⇒ Object
34 35 36 |
# File 'lib/confuse/definition.rb', line 34 def find_item(namespace, key) (ns = find_namespace(namespace)) && ns[key] end |
#namespace_and_key(name) ⇒ Object
29 30 31 32 |
# File 'lib/confuse/definition.rb', line 29 def namespace_and_key(name) KeySplitter.new(name).split.find { |(ns, _)| namespaces[ns] } || [nil, name] end |
#namespaces ⇒ Object
23 24 25 26 27 |
# File 'lib/confuse/definition.rb', line 23 def namespaces @namespaces ||= { @default_namespace => Namespace.new(nil, &(Proc.new {})) } end |
#to_hash ⇒ Object
38 39 40 |
# File 'lib/confuse/definition.rb', line 38 def to_hash @namespaces.reduce({}) { |a, (_k, v)| a.merge(v) } end |