class Praxis::Config
Attributes
attribute[R]
Public Class Methods
new()
click to toggle source
# File lib/praxis/config.rb, line 9 def initialize @attribute = Attributor::Attribute.new(Attributor::Struct) {} @value = nil end
Public Instance Methods
define(key = nil, type = Attributor::Struct, **opts, &block)
click to toggle source
…or using this way too (equivalent) define(:app) do
attribute :two, String
end You can also define a key to be a non-Struct type define :app, Attributor::Hash
# File lib/praxis/config.rb, line 41 def define(key = nil, type = Attributor::Struct, **opts, &block) raise Exceptions::InvalidConfiguration, "You cannot define the top level configuration with a non-Struct type. Got: #{type.inspect}" if key.nil? && type != Attributor::Struct case key when String, Symbol, NilClass top = key.nil? ? @attribute : @attribute.attributes[key] if top # key defined...redefine raise Exceptions::InvalidConfiguration, "Incompatible type received for extending configuration key [#{key}]. Got type #{type.name}" unless type == Attributor::Struct && top.type < Attributor::Struct top.options.merge!(opts) top.type.attributes(**opts, &block) else @attribute.attributes[key] = Attributor::Attribute.new(type, opts, &block) end else raise Exceptions::InvalidConfiguration, "Defining a configuration key requires a String or a Symbol key. Got: #{key.inspect}" end end
get()
click to toggle source
# File lib/praxis/config.rb, line 75 def get @value ||= begin # rubocop:disable Naming/MemoizedInstanceVariableName context = %w[Application config].freeze @attribute.load({}, context, recurse: true) end end
set(config)
click to toggle source
# File lib/praxis/config.rb, line 61 def set(config) context = %w[Application config] begin @value = @attribute.load(config, context, recurse: true) rescue Attributor::AttributorException => e raise Exceptions::ConfigLoad.new(exception: e) end errors = @attribute.validate(@value, context) raise Exceptions::ConfigValidation.new(errors: errors) unless errors.empty? end