Class: PDK::Config::Namespace
- Inherits:
-
Object
- Object
- PDK::Config::Namespace
- Defined in:
- lib/pdk/config/namespace.rb
Instance Attribute Summary collapse
-
#file ⇒ String
readonly
The path to the file associated with the contents of this namespace.
-
#name ⇒ String
Determines the fully qualified name of the namespace.
-
#parent ⇒ self
The parent namespace of this namespace.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get the value of the named key.
-
#[]=(key, value) ⇒ nil
Set the value of the named key.
-
#child_namespace? ⇒ Boolean
True if the namespace has a parent, otherwise false.
-
#fetch(key, default_value) ⇒ Object
Get the value of the named key or the provided default value if not present.
-
#include_in_parent? ⇒ Boolean
Determines if the contents of the namespace should be included in the parent namespace when persisting to disk.
-
#initialize(name = nil, file: nil, parent: nil, &block) ⇒ Namespace
constructor
Initialises the PDK::Config::Namespace object.
-
#mount(key, obj, &block) ⇒ self
Mount a provided [self] (or subclass) into the namespace.
-
#namespace(name, &block) ⇒ Object
Create and mount a new child namespace.
-
#to_h ⇒ Hash{String => Object}
Convert the namespace into a Hash of values, suitable for serialising and persisting to disk.
-
#value(key, &block) ⇒ nil
Pre-configure a value in the namespace.
Constructor Details
#initialize(name = nil, file: nil, parent: nil, &block) ⇒ Namespace
Initialises the PDK::Config::Namespace object.
24 25 26 27 28 29 30 31 |
# File 'lib/pdk/config/namespace.rb', line 24 def initialize(name = nil, file: nil, parent: nil, &block) @file = File.(file) unless file.nil? @values = {} @name = name.to_s @parent = parent instance_eval(&block) if block_given? end |
Instance Attribute Details
#file ⇒ String (readonly)
Returns the path to the file associated with the contents of this namespace.
9 10 11 |
# File 'lib/pdk/config/namespace.rb', line 9 def file @file end |
#name ⇒ String
Determines the fully qualified name of the namespace.
If this is a child namespace, then fully qualified name for the namespace will be “<parent>.<child>”.
157 158 159 |
# File 'lib/pdk/config/namespace.rb', line 157 def name child_namespace? ? [parent.name, @name].join('.') : @name end |
#parent ⇒ self
Returns the parent namespace of this namespace.
12 13 14 |
# File 'lib/pdk/config/namespace.rb', line 12 def parent @parent end |
Instance Method Details
#[](key) ⇒ Object
Unlike a Ruby Hash, this will not return ‘nil` in the event that the key does not exist (see #fetch).
Get the value of the named key.
If there is a value for that key, return it. If not, follow the logic described in #default_config_value to determine the default value to return.
86 87 88 |
# File 'lib/pdk/config/namespace.rb', line 86 def [](key) data[key.to_s] end |
#[]=(key, value) ⇒ nil
Set the value of the named key.
If the key has been pre-configured with #value, then the value of the key will be validated against any validators that have been configured.
After the value has been set in memory, the value will then be persisted to disk.
119 120 121 122 123 124 |
# File 'lib/pdk/config/namespace.rb', line 119 def []=(key, value) @values[key.to_s].validate!([name, key.to_s].join('.'), value) if @values.key?(key.to_s) data[key.to_s] = value save_data end |
#child_namespace? ⇒ Boolean
Returns true if the namespace has a parent, otherwise false.
147 148 149 |
# File 'lib/pdk/config/namespace.rb', line 147 def child_namespace? !parent.nil? end |
#fetch(key, default_value) ⇒ Object
103 104 105 |
# File 'lib/pdk/config/namespace.rb', line 103 def fetch(key, default_value) data.fetch(key.to_s, default_value) end |
#include_in_parent? ⇒ Boolean
Determines if the contents of the namespace should be included in the parent namespace when persisting to disk.
If the namespace has been mounted into a parent namespace and is not associated with its own file on disk, then the values in the namespace should be included in the parent namespace when persisting to disk.
170 171 172 |
# File 'lib/pdk/config/namespace.rb', line 170 def include_in_parent? child_namespace? && file.nil? end |
#mount(key, obj, &block) ⇒ self
Mount a provided [self] (or subclass) into the namespace.
58 59 60 61 62 63 64 |
# File 'lib/pdk/config/namespace.rb', line 58 def mount(key, obj, &block) raise ArgumentError, _('Only PDK::Config::Namespace objects can be mounted into a namespace') unless obj.is_a?(PDK::Config::Namespace) obj.parent = self obj.name = key.to_s obj.instance_eval(&block) if block_given? data[key.to_s] = obj end |
#namespace(name, &block) ⇒ Object
Create and mount a new child namespace.
70 71 72 |
# File 'lib/pdk/config/namespace.rb', line 70 def namespace(name, &block) mount(name, PDK::Config::Namespace.new, &block) end |
#to_h ⇒ Hash{String => Object}
Convert the namespace into a Hash of values, suitable for serialising and persisting to disk.
Child namespaces that are associated with their own files are excluded from the Hash (as their values will be persisted to their own files) and nil values are removed from the Hash.
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/pdk/config/namespace.rb', line 135 def to_h data.inject({}) do |new_hash, (key, value)| new_hash[key] = if value.is_a?(PDK::Config::Namespace) value.include_in_parent? ? value.to_h : nil else value end new_hash.delete_if { |_, v| v.nil? } end end |
#value(key, &block) ⇒ nil
Pre-configure a value in the namespace.
Allows you to specify validators and a default value for value in the namespace (see PDK::Config::Value#initialize).
42 43 44 45 |
# File 'lib/pdk/config/namespace.rb', line 42 def value(key, &block) @values[key.to_s] ||= PDK::Config::Value.new(key.to_s) @values[key.to_s].instance_eval(&block) if block_given? end |