Class: Errapi::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/errapi/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

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

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/errapi/configuration.rb', line 6

def options
  @options
end

#pluginsObject (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

Yields:

  • (_self)

Yield Parameters:



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, options = {}
  [].tap do |conditions|
    @condition_factories.each_pair do |conditional,factory|
      next unless source.key? conditional
      conditions << factory.new(conditional, source.delete(conditional), options)
    end
  end
end

#new_contextObject



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 options = {}
  Errapi::ValidationError.new options
end

#plugin(impl, options = {}) ⇒ Object



37
38
39
40
41
# File 'lib/errapi/configuration.rb', line 37

def plugin impl, options = {}
  name = options[: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

Raises:

  • (ArgumentError)


49
50
51
52
53
# File 'lib/errapi/configuration.rb', line 49

def validation name, options = {}
  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(options) : factory.new(options)
end

#validation_factory(factory, options = {}) ⇒ Object



43
44
45
46
47
# File 'lib/errapi/configuration.rb', line 43

def validation_factory factory, options = {}
  name = options[:name] || Utils.underscore(factory.to_s.sub(/.*::/, '')).to_sym
  factory.config = self if factory.respond_to? :config=
  @validation_factories[name] = factory
end