Class: Iqeo::Configuration

Inherits:
BlankSlate show all
Defined in:
lib/iqeo/configuration.rb

Overview

Configuration class.

A DSL representing configuration files.

Constant Summary collapse

VERSION =

Returns Configuration version number.

'1.1.1'
OPTIONS =
{
  :blankslate => true,
  :case_sensitive => true
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default = nil, options = {}, &block) ⇒ Configuration

todo: why can’t :_parent= be protected ? protected :_parent, :_items, :_items= #, :_get, :[], :_set, :[]=



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/iqeo/configuration.rb', line 96

def initialize default = nil, options = {}, &block
  _process_options options
  @_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

#method_missing(name, *values, &block) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/iqeo/configuration.rb', line 113

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

#_itemsObject

Returns the value of attribute _items.



91
92
93
# File 'lib/iqeo/configuration.rb', line 91

def _items
  @_items
end

#_optionsObject

Returns the value of attribute _options.



91
92
93
# File 'lib/iqeo/configuration.rb', line 91

def _options
  @_options
end

#_parentObject

Returns the value of attribute _parent.



91
92
93
# File 'lib/iqeo/configuration.rb', line 91

def _parent
  @_parent
end

Class Method Details

.load(file, options = {}) ⇒ Object

Creates a new Configuration instance from filename or File/IO object.

Content should be in eval DSL format.



73
74
75
# File 'lib/iqeo/configuration.rb', line 73

def self.load file, options = {}
  return self.read file.respond_to?(:read) ? file.read : File.read(file)
end

.new_defer_block_for_parent(parent, options = {}, &block) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/iqeo/configuration.rb', line 77

def self.new_defer_block_for_parent parent, options = {}, &block
  conf = Configuration.new nil, options
  conf._parent = parent
  if block_given? && block.arity > 0
    block.call(conf)                                              # this is 'yield self' from the outside
  end
  conf
end

.read(string, options = {}) ⇒ Object

Creates a new Configuration instance from string.

Content should be in eval DSL format.



63
64
65
66
67
# File 'lib/iqeo/configuration.rb', line 63

def self.read string, options = {}
  conf = self.new nil, options
  conf.instance_eval string
  conf
end

.versionObject



55
56
57
# File 'lib/iqeo/configuration.rb', line 55

def self.version
  VERSION
end

Instance Method Details

#*Object

todo: method ‘*’ for wildcard dir glob like selections eg top.*.bottom ?



185
186
187
# File 'lib/iqeo/configuration.rb', line 185

def *
  ConfigurationDelegator.new _configurations
end

#_configurationsObject



174
175
176
# File 'lib/iqeo/configuration.rb', line 174

def _configurations
  @_items.values.select { |value| value.kind_of? Configuration }
end

#_get(key) ⇒ Object 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.



143
144
145
146
147
# File 'lib/iqeo/configuration.rb', line 143

def _get key
  return @_items[key] unless @_items[key].nil?
  return @_items[key] if @_parent.nil?
  @_parent._get key
end

#_load(file) ⇒ Object



154
155
156
# File 'lib/iqeo/configuration.rb', line 154

def _load file
  _read file.respond_to?(:read) ? file.read : File.read(file)
end

#_merge(other) ⇒ Object



170
171
172
# File 'lib/iqeo/configuration.rb', line 170

def _merge other
  self.dup._merge! other
end

#_merge!(other) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/iqeo/configuration.rb', line 158

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

#_process_options(options) ⇒ Object



178
179
180
181
# File 'lib/iqeo/configuration.rb', line 178

def _process_options options
  @_options = OPTIONS.merge options
  #_wipe if @_options[:blankslate]         # todo: how to make blankslate optional ?
end

#_read(string) ⇒ Object



150
151
152
# File 'lib/iqeo/configuration.rb', line 150

def _read string
  instance_eval string
end

#_set(key, value) ⇒ Object Also known as: []=



131
132
133
134
# File 'lib/iqeo/configuration.rb', line 131

def _set key, value
  value._parent = self if value.kind_of?( Configuration )
  @_items[key] = value
end