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

- (Configuration) initialize(default = nil, &block)

A new instance of Configuration



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

def initialize default = nil, &block
  # todo: default immediate child Configurations should have _parent updated
  @_items = case
              when default.kind_of?( HashWithIndifferentAccess ) then default
              when default.kind_of?( Configuration ) then default._items
              else HashWithIndifferentAccess.new
            end
  @_parent = nil
  if block_given?
    if block.arity > 0                                           # cannot set parent for yield blocks here as self is wrong !?
      yield self
    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)



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/iqeo/configuration.rb', line 107

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

- (Object) _parent=(value)

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

+ (Object) load(file)

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, &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
  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)

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

+ (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) _load(file)



152
153
154
# File 'lib/iqeo/configuration.rb', line 152

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

- (Object) _merge(other)



162
163
164
165
# File 'lib/iqeo/configuration.rb', line 162

def _merge other
  # todo: merges should update _parent of any immediate child Configurations
  self.dup._merge! other
end

- (Object) _merge!(other)



156
157
158
159
160
# File 'lib/iqeo/configuration.rb', line 156

def _merge! other
  # todo: merges should update _parent of any immediate child Configurations
  @_items.merge! other._items
  self
end

- (Object) _read(string)



148
149
150
# File 'lib/iqeo/configuration.rb', line 148

def _read string
  instance_eval string
end