Class: Errapi::Configuration
- Inherits:
-
Object
- Object
- Errapi::Configuration
- Defined in:
- lib/errapi/configuration.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#plugins ⇒ Object
readonly
Returns the value of attribute plugins.
Instance Method Summary collapse
- #build_error(error, context) ⇒ Object
- #configure {|_self| ... } ⇒ Object
- #extract_conditions!(source, options = {}) ⇒ Object
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #new_context ⇒ Object
- #new_error(options = {}) ⇒ Object
- #plugin(impl, options = {}) ⇒ Object
- #register_condition(factory) ⇒ Object
- #serialize_error(error, serialized) ⇒ Object
- #validation(name, options = {}) ⇒ Object
- #validation_factory(factory, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
9 10 11 12 13 14 15 |
# File 'lib/errapi/configuration.rb', line 9 def initialize @options = OpenStruct.new @plugins = OpenStruct.new @validation_factories = {} @condition_factories = {} @location_factories = {} end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/errapi/configuration.rb', line 6 def @options end |
#plugins ⇒ Object (readonly)
Returns the value of attribute plugins.
7 8 9 |
# File 'lib/errapi/configuration.rb', line 7 def plugins @plugins end |
Instance Method Details
#build_error(error, context) ⇒ Object
25 26 27 |
# File 'lib/errapi/configuration.rb', line 25 def build_error error, context apply_plugins :build_error, error, context end |
#configure {|_self| ... } ⇒ Object
17 18 19 |
# File 'lib/errapi/configuration.rb', line 17 def configure yield self end |
#extract_conditions!(source, options = {}) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/errapi/configuration.rb', line 62 def extract_conditions! source, = {} [].tap do |conditions| @condition_factories.each_pair do |conditional,factory| next unless source.key? conditional conditions << factory.new(conditional, source.delete(conditional), ) end end end |
#new_context ⇒ Object
33 34 35 |
# File 'lib/errapi/configuration.rb', line 33 def new_context Errapi::ValidationContext.new config: self end |
#new_error(options = {}) ⇒ Object
21 22 23 |
# File 'lib/errapi/configuration.rb', line 21 def new_error = {} Errapi::ValidationError.new end |
#plugin(impl, options = {}) ⇒ Object
37 38 39 40 41 |
# File 'lib/errapi/configuration.rb', line 37 def plugin impl, = {} name = [:name] || Utils.underscore(impl.to_s.sub(/.*::/, '')).to_sym impl.config = self if impl.respond_to? :config= @plugins[name] = impl end |
#register_condition(factory) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/errapi/configuration.rb', line 55 def register_condition factory factory.conditionals.each do |conditional| raise ArgumentError, "Conditional #{conditional} should start with 'if' or 'unless'." unless conditional.to_s.match /^(if|unless)/ @condition_factories[conditional] = factory end end |
#serialize_error(error, serialized) ⇒ Object
29 30 31 |
# File 'lib/errapi/configuration.rb', line 29 def serialize_error error, serialized apply_plugins :serialize_error, error, serialized end |
#validation(name, options = {}) ⇒ Object
49 50 51 52 53 |
# File 'lib/errapi/configuration.rb', line 49 def validation name, = {} raise ArgumentError, "No validation factory registered for name #{name.inspect}" unless @validation_factories.key? name factory = @validation_factories[name] factory.respond_to?(:validation) ? factory.validation() : factory.new() end |
#validation_factory(factory, options = {}) ⇒ Object
43 44 45 46 47 |
# File 'lib/errapi/configuration.rb', line 43 def validation_factory factory, = {} name = [:name] || Utils.underscore(factory.to_s.sub(/.*::/, '')).to_sym factory.config = self if factory.respond_to? :config= @validation_factories[name] = factory end |