class KXI::Application::Config
Represents configuration file
Public Class Methods
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
Gets configuration file @return [String] Path to configuration file
# File lib/kxi/application/config.rb, line 9 def file @file end
Protected Instance Methods
Gets current context @return [Object] Current context
# File lib/kxi/application/config.rb, line 25 def context @obj end
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
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
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
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
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
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
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
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
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
# 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
# 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