class KXI::Application::Config

Represents configuration file

Public Class Methods

new(file, reader) click to toggle source

Instantiates the {KXI::Application::Config} file @param file [String] Configuration file to read @param reader [KXI::Application::ConfigReader] Readers used to parse configuration

# File lib/kxi/application/config.rb, line 16
def initialize(file, reader)
        @file = file
        @obj  = reader.parse(File.read(file))
        @pth  = ''
        @def  = false
end

Public Instance Methods

file() click to toggle source

Gets configuration file @return [String] Path to configuration file

# File lib/kxi/application/config.rb, line 9
def file
        @file
end

Protected Instance Methods

context() click to toggle source

Gets current context @return [Object] Current context

# File lib/kxi/application/config.rb, line 25
def context
        @obj
end
each_field(key, man = false, &block) click to toggle source

Iterates through fields of object @param key [String] Name of object @param man [Bool] Determines whether this section is mandatory @yield [idx] Block of actions for entered context @yieldparam idx [String] Name of field

# File lib/kxi/application/config.rb, line 130
def each_field(key, man = false, &block)
        if not @def and @obj.key?(key)
                ctx = @obj[key]
                raise(KXI::Exceptions::ConfigurationException.new(@file, "#{@pth}/#{key}", 'Field must be object!')) unless ctx.is_a?(Hash)
                ctx.each_pair do |idx, itm|
                        enter("#{key}/#{idx}", itm) do
                                block.call(idx)
                        end
                end
        elsif man
                raise(KXI::Exceptions::ConfigurationException.new(@file, "#{@pth}/#{key}", 'Object is mandatory!'))
        end
end
each_field!(key, &block) click to toggle source

Iterates through fields of mandatory object @param key [String] Name of object @yield [idx] Block of actions for entered context @yieldparam idx [String] Name of field

# File lib/kxi/application/config.rb, line 148
def each_field!(key, &block)
        each_field(key, true) { |itm| block.call(itm) }
end
each_index(key, man = false, &block) click to toggle source

Iterates through collection @param key [String] Name of collection @param man [Bool] Determines whether this section is mandatory @yield [] Block of actions for entered context

# File lib/kxi/application/config.rb, line 104
def each_index(key, man = false, &block)
        if not @def and @obj.key?(key)
                ctx = @obj[key]
                raise(KXI::Exceptions::ConfigurationException.new(@file, "#{@pth}/#{key}", 'Field must be array!')) unless ctx.is_a?(Array)
                ctx.each_with_index do |itm, idx|
                        enter("#{key}[#{idx}]", itm) do
                                block.call
                        end
                end
        elsif man
                raise(KXI::Exceptions::ConfigurationException.new(@file, "#{@pth}/#{key}", 'Array is mandatory!'))
        end
end
each_index!(key, &block) click to toggle source

Iterates through mandatory collection @param key [String] Name of collection @yield [] Block of actions for entered context

# File lib/kxi/application/config.rb, line 121
def each_index!(key, &block)
        each_index(key, true) { block.call }
end
get(key, default = nil, &validator) click to toggle source

Gets value or default from current context @param key [String] Name of value @param default [Object, nil] Default value @yield [val] Validator function @yieldparam val [Object, nil] Value to validate @return [Object, nil] Value or default

# File lib/kxi/application/config.rb, line 41
def get(key, default = nil, &validator)
        if not @def and @obj.key?(key)
                ctx = @obj[key]
                return ctx if validator == nil
                return validate(key, ctx, &validator)
        else
                return default
        end
end
get!(key, &validator) click to toggle source

Gets value from current context @param key [String] Name of value @yield [val] Validator function @yieldparam val [Object, nil] Value to validate @return [Object, nil] Value @raise [KXI::Exceptions::ConfigurationException] Raised when field is not found

# File lib/kxi/application/config.rb, line 57
def get!(key, &validator)
        if not @def and @obj.key?(key)
                ctx = @obj[key]
                return ctx if validator == nil
                return validate(key, ctx, &validator)
        else
                raise(KXI::Exceptions::ConfigurationException.new(@file, "#{@pth}/#{key}", 'Field is mandatory!'))
        end
end
inside(key, man = false, &block) click to toggle source

Enters named section of configuration file @param key [String] Name of section @param man [Bool] Determines whether this section is mandatory @yield [] Block of actions for entered context

# File lib/kxi/application/config.rb, line 71
def inside(key, man = false, &block)
        if not @def and @obj.key?(key)
                ctx = @obj[key]
                raise(KXI::Exceptions::ConfigurationException.new(@file, "#{@pth}/#{key}", 'Field must be object!')) unless ctx.is_a?(Hash)
                enter(key, ctx) do
                        block.call
                end
        elsif man
                raise(KXI::Exceptions::ConfigurationException.new(@file, "#{@pth}/#{key}", 'Object is mandatory!'))
        else
                enter(key, nil) do
                        if @def
                                block.call
                        else
                                @def = true
                                block.call
                                @def = false
                        end
                end
        end
end
inside!(key, &block) click to toggle source

Enters mandatory named section of configuration file @param key [String] Name of section @yield [] Block of actions for entered context

# File lib/kxi/application/config.rb, line 96
def inside!(key, &block)
        inside(key, true) { block.call }
end
path() click to toggle source

Gets path of current context @return [String] Path of current context

# File lib/kxi/application/config.rb, line 31
def path
        @pth
end

Private Instance Methods

enter(key, ctx, &block) click to toggle source
# File lib/kxi/application/config.rb, line 152
def enter(key, ctx, &block)
        obj  = @obj
        pth  = @pth
        @obj = ctx
        @pth = "#{@pth}/#{key}"
        block.call
ensure
        @pth = pth
        @obj = obj
end
validate(key, ctx, &validator) click to toggle source
# File lib/kxi/application/config.rb, line 163
def validate(key, ctx, &validator)
        begin
                enter(key, ctx) do
                        validator.call(ctx)
                end
                return ctx
        rescue Exception => ex
                raise(KXI::Exceptions::ConfigurationException.new(@file, "#{@pth}/#{key}", ex.message))
        end
end