Class: Iqeo::Configuration

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

Overview

Configuration class.

A DSL representing configuration files.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Configuration.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/iqeo/configuration.rb', line 87

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

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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/iqeo/configuration.rb', line 103

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, &block )
    else
      return _set name, Configuration.new( &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

#_parent=(value) ⇒ Object

Sets the attribute _parent

Parameters:

  • value

    the value to set the attribute _parent to.



85
86
87
# File 'lib/iqeo/configuration.rb', line 85

def _parent=(value)
  @_parent = value
end

Class Method Details

.load(file) ⇒ Object

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

.new_defer_block_for_parent(parent, &block) ⇒ Object



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
  conf._parent = parent
  if block_given? && block.arity > 0
    block.call(conf)                                              # this is 'yield self' from the outside
  end
  conf
end

.read(string) ⇒ Object

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
  conf.instance_eval string
  conf
end

.versionObject

Returns Configuration version number.



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

def self.version
  Iqeo::CONFIGURATION_VERSION
end

Instance Method Details

#_load(file) ⇒ Object



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

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

#_merge(other) ⇒ Object



160
161
162
# File 'lib/iqeo/configuration.rb', line 160

def _merge other
  self.dup._merge! other
end

#_merge!(other) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/iqeo/configuration.rb', line 148

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

#_read(string) ⇒ Object



140
141
142
# File 'lib/iqeo/configuration.rb', line 140

def _read string
  instance_eval string
end