Class: Iqeo::Configuration
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- Iqeo::Configuration
- Defined in:
- lib/iqeo/configuration.rb
Overview
Configuration class.
A DSL representing configuration files.
Constant Summary
- OPTIONS =
{ :blankslate => true, :case_sensitive => true }
Instance Attribute Summary (collapse)
-
- (Object) _items
Returns the value of attribute _items.
-
- (Object) _options
Returns the value of attribute _options.
-
- (Object) _parent
Returns the value of attribute _parent.
Class Method Summary (collapse)
-
+ (Object) load(file, options = {})
Creates a new Configuration instance from filename or File/IO object.
- + (Object) new_defer_block_for_parent(parent, options = {}, &block)
-
+ (Object) read(string, options = {})
Creates a new Configuration instance from string.
-
+ (Object) version
Returns Configuration version number.
Instance Method Summary (collapse)
-
- (Object) *
todo: method '*' for wildcard dir glob like selections eg top.*.bottom ?.
- - (Object) _configurations
-
- (Object) _get(key)
(also: #[])
Retrieves value for key, indifferent storage permits key to be a string or symbol.
- - (Object) _load(file)
- - (Object) _merge(other)
- - (Object) _merge!(other)
- - (Object) _process_options(options)
- - (Object) _read(string)
- - (Object) _set(key, value) (also: #[]=)
-
- (Configuration) initialize(default = nil, options = {}, &block)
constructor
todo: why can't :_parent= be protected ? protected :_parent, :_items, :_items= #, :_get, :[], :_set, :[]=.
- - (Object) method_missing(name, *values, &block)
Constructor Details
- (Configuration) initialize(default = nil, options = {}, &block)
todo: why can't :_parent= be protected ? protected :_parent, :_items, :_items= #, :_get, :[], :_set, :[]=
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/iqeo/configuration.rb', line 95 def initialize default = nil, = {}, &block @_items = HashWithIndifferentAccess.new @_parent = nil _merge! default if default.kind_of?( Configuration ) if block_given? if block.arity > 0 # cannot set parent for yield block here as context is unknowable yield self # parent is being set in new_defer_block_for_parent else if block.binding.eval('self').kind_of?( Configuration ) # for eval block if nested configuration @_parent = block.binding.eval('self') # set parent to make inherited values available end # during block execution instance_eval &block end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(name, *values, &block)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/iqeo/configuration.rb', line 112 def method_missing name, *values, &block return @_items.send( name, *values, &block ) if @_items.respond_to? name # @_items methods are highest priority name = name.to_s.chomp('=') if block_given? # block is a nested configuration if block.arity == 1 # yield DSL needs deferred block to set parent without binding return _set name, Configuration.new_defer_block_for_parent( self, @_options, &block ) else return _set name, Configuration.new( nil, @_options, &block ) # eval DSL can set parent from block binding in initialize end end return _get name if values.empty? # just get item return _set name, values if values.size > 1 # set item to multiple values return _set name, values.first # set item to single value end |
Instance Attribute Details
- (Object) _items
Returns the value of attribute _items
90 91 92 |
# File 'lib/iqeo/configuration.rb', line 90 def _items @_items end |
- (Object) _options
Returns the value of attribute _options
90 91 92 |
# File 'lib/iqeo/configuration.rb', line 90 def @_options end |
- (Object) _parent
Returns the value of attribute _parent
90 91 92 |
# File 'lib/iqeo/configuration.rb', line 90 def _parent @_parent end |
Class Method Details
+ (Object) load(file, options = {})
Creates a new Configuration instance from filename or File/IO object.
Content should be in eval DSL format.
72 73 74 |
# File 'lib/iqeo/configuration.rb', line 72 def self.load file, = {} return self.read file.respond_to?(:read) ? file.read : File.read(file) end |
+ (Object) new_defer_block_for_parent(parent, options = {}, &block)
76 77 78 79 80 81 82 83 |
# File 'lib/iqeo/configuration.rb', line 76 def self.new_defer_block_for_parent parent, = {}, &block conf = Configuration.new nil, conf._parent = parent if block_given? && block.arity > 0 block.call(conf) # this is 'yield self' from the outside end conf end |
+ (Object) read(string, options = {})
Creates a new Configuration instance from string.
Content should be in eval DSL format.
62 63 64 65 66 |
# File 'lib/iqeo/configuration.rb', line 62 def self.read string, = {} conf = self.new nil, conf.instance_eval string conf end |
+ (Object) version
Returns Configuration version number.
54 55 56 |
# File 'lib/iqeo/configuration.rb', line 54 def self.version Iqeo::CONFIGURATION_VERSION end |
Instance Method Details
- (Object) *
todo: method '*' for wildcard dir glob like selections eg top.*.bottom ?
184 185 186 |
# File 'lib/iqeo/configuration.rb', line 184 def * ConfigurationDelegator.new _configurations end |
- (Object) _configurations
173 174 175 |
# File 'lib/iqeo/configuration.rb', line 173 def _configurations @_items.values.select { |value| value.kind_of? Configuration } end |
- (Object) _get(key) Also known as: []
Retrieves value for key, indifferent storage permits key to be a string or symbol.
If configuration is nested, searches for key recursively up to root.
Returns nil if key does not exist.
142 143 144 145 146 |
# File 'lib/iqeo/configuration.rb', line 142 def _get key return @_items[key] unless @_items[key].nil? return @_items[key] if @_parent.nil? @_parent._get key end |
- (Object) _load(file)
153 154 155 |
# File 'lib/iqeo/configuration.rb', line 153 def _load file _read file.respond_to?(:read) ? file.read : File.read(file) end |
- (Object) _merge(other)
169 170 171 |
# File 'lib/iqeo/configuration.rb', line 169 def _merge other self.dup._merge! other end |
- (Object) _merge!(other)
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/iqeo/configuration.rb', line 157 def _merge! other @_items.merge!(other._items) do |key,this,other| if this.kind_of?( Configuration ) && other.kind_of?( Configuration ) this._merge! other else other end end @_items.values.each { |value| value._parent = self if value.kind_of?( Configuration ) } self end |
- (Object) _process_options(options)
177 178 179 180 |
# File 'lib/iqeo/configuration.rb', line 177 def @_options = OPTIONS.merge #_wipe if @_options[:blankslate] # todo: how to make blankslate optional ? end |
- (Object) _read(string)
149 150 151 |
# File 'lib/iqeo/configuration.rb', line 149 def _read string instance_eval string end |
- (Object) _set(key, value) Also known as: []=
130 131 132 133 |
# File 'lib/iqeo/configuration.rb', line 130 def _set key, value value._parent = self if value.kind_of?( Configuration ) @_items[key] = value end |