Class: Praxis::Config
- Inherits:
-
Object
- Object
- Praxis::Config
- Includes:
- Attributor::Type
- Defined in:
- lib/praxis/config.rb
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
Instance Method Summary collapse
-
#define(key = nil, type = Attributor::Struct, **opts, &block) ⇒ Object
…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.
- #get ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #set(config) ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
7 8 9 10 |
# File 'lib/praxis/config.rb', line 7 def initialize @attribute = Attributor::Attribute.new(Attributor::Struct) {} @value = nil end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
5 6 7 |
# File 'lib/praxis/config.rb', line 5 def attribute @attribute end |
Instance Method Details
#define(key = nil, type = Attributor::Struct, **opts, &block) ⇒ Object
…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
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/praxis/config.rb', line 39 def define(key=nil, type=Attributor::Struct, **opts, &block) if key.nil? && type != Attributor::Struct raise Exceptions::InvalidConfiguration.new( "You cannot define the top level configuration with a non-Struct type. Got: #{type.inspect}" ) end case key when String, Symbol, NilClass top = key.nil? ? @attribute : @attribute.attributes[key] if top #key defined...redefine unless type == Attributor::Struct && top.type < Attributor::Struct raise Exceptions::InvalidConfiguration.new( "Incompatible type received for extending configuration key [#{key}]. Got type #{type.name}" ) end top..merge!(opts) top.type.attributes(opts, &block) else @attribute.attributes[key] = Attributor::Attribute.new(type, opts, &block) end else raise Exceptions::InvalidConfiguration.new( "Defining a configuration key requires a String or a Symbol key. Got: #{key.inspect}" ) end end |
#get ⇒ Object
85 86 87 88 89 90 |
# File 'lib/praxis/config.rb', line 85 def get @value ||= begin context = ['Application','config'].freeze @attribute.load({},context, recurse: true) end end |
#set(config) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/praxis/config.rb', line 69 def set(config) context = ['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) unless errors.empty? raise Exceptions::ConfigValidation.new(errors: errors) end end |