Class: Errapi::ValidationContext
- Inherits:
-
Object
- Object
- Errapi::ValidationContext
- Defined in:
- lib/errapi/validation_context.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Instance Method Summary collapse
- #add_error(options = {}) {|error| ... } ⇒ Object
- #clear ⇒ Object
- #errors?(criteria = {}, &block) ⇒ Boolean
-
#initialize(options = {}) ⇒ ValidationContext
constructor
A new instance of ValidationContext.
-
#serialize ⇒ Object
TODO: add custom serialization options.
- #valid? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ ValidationContext
Returns a new instance of ValidationContext.
8 9 10 11 12 |
# File 'lib/errapi/validation_context.rb', line 8 def initialize = {} @errors = [] @data = OpenStruct.new [:data] || {} @config = [:config] end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'lib/errapi/validation_context.rb', line 6 def config @config end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
4 5 6 |
# File 'lib/errapi/validation_context.rb', line 4 def data @data end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
5 6 7 |
# File 'lib/errapi/validation_context.rb', line 5 def errors @errors end |
Instance Method Details
#add_error(options = {}) {|error| ... } ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/errapi/validation_context.rb', line 14 def add_error = {}, &block error = .kind_of?(Errapi::ValidationError) ? : @config.new_error() yield error if block_given? @config.build_error error, self @errors << error self end |
#clear ⇒ Object
33 34 35 36 |
# File 'lib/errapi/validation_context.rb', line 33 def clear @errors.clear @data = OpenStruct.new end |
#errors?(criteria = {}, &block) ⇒ Boolean
24 25 26 27 |
# File 'lib/errapi/validation_context.rb', line 24 def errors? criteria = {}, &block return !@errors.empty? if criteria.empty? && !block block ? @errors.any?{ |err| err.matches?(criteria) && block.call(err) } : @errors.any?{ |err| err.matches?(criteria) } end |
#serialize ⇒ Object
TODO: add custom serialization options
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/errapi/validation_context.rb', line 39 def serialize # TODO: add hook for plugins to serialize context { errors: [] }.tap do |h| @errors.each do |error| serialized = {} @config.serialize_error error, serialized h[:errors] << serialized end end end |
#valid? ⇒ Boolean
29 30 31 |
# File 'lib/errapi/validation_context.rb', line 29 def valid? !errors? end |