Skip to content

Commit

Permalink
Removed with_context options wrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Oulevay committed Jan 6, 2015
1 parent 14e24ba commit f67d92e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
50 changes: 32 additions & 18 deletions lib/errapi/object_validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def validates_each *args, &block

options = args.last.kind_of?(Hash) ? args.pop : {}
options[:each] = args.shift
options[:each_with_context] = options.delete :each_with_context if options.key? :each_with_context
options[:each_options] = options.delete :each_options if options.key? :each_options
args << options

validates *args, &block
Expand All @@ -32,7 +32,7 @@ def validate value, context, options = {}

context_options = validation[:context_options]
each = validation[:each]
each_with_context = validation[:each_with_context] || {}
each_options = validation[:each_options] || {}

values = if each
extract(value, each, options)[:value] || []
Expand All @@ -47,7 +47,7 @@ def validate value, context, options = {}
end

each_options = {}
each_options[:location] = actual_location relative_location: each if each
each_options[:location] = actual_location at_relative_location: each if each

with_options each_options do

Expand All @@ -56,14 +56,14 @@ def validate value, context, options = {}
next if validation[:conditions].any?{ |condition| !condition.fulfilled?(value, context) }

iteration_options = {}
iteration_options = { location: actual_location(relative_location: i), value_set: values_set[i] } if each
iteration_options = { location: actual_location(at_relative_location: i), value_set: values_set[i] } if each

with_options iteration_options do

target = validation[:target]

value_context_options = context_options.dup
value_context_options[:location] = actual_location(extract_location(value_context_options) || { relative_location: target })
value_context_options[:location] = actual_location(extract_location(value_context_options) || { at_relative_location: target })

value_data = extract value, target, value_set: values_set[i]
current_value = value_data[:value]
Expand Down Expand Up @@ -107,8 +107,14 @@ def build_error error, context
end

def build_error_criteria criteria, context
criteria[:location] = actual_location criteria if %i(location relative_location).any?{ |k| criteria.key? k }
criteria.delete :relative_location

if criteria[:at_absolute_location]
criteria[:location] = criteria.delete :at_absolute_location
elsif %i(at at_location at_relative_location at_absolute_location).any?{ |k| criteria.key? k }
criteria[:location] = actual_location criteria
end

%i(at at_location at_relative_location at_absolute_location).each{ |key| criteria.delete key }
end

private
Expand Down Expand Up @@ -138,27 +144,35 @@ def extract value, target, options = {}
end

def extract_location options = {}
if options[:location]
{ location: options[:location] }
elsif options[:relative_location]
{ relative_location: options[:relative_location] }
if options[:at_absolute_location]
{ at_absolute_location: options[:at_absolute_location] }
elsif key = %i(at at_location at_relative_location).find{ |key| options.key? key }
{ key => options[key] }
end
end

def actual_location options = {}
if options[:location]
options[:location]
elsif options[:relative_location]
@current_options[:location] ? "#{@current_options[:location]}.#{options[:relative_location]}" : options[:relative_location]
if options[:at_absolute_location]
options[:at_absolute_location]
elsif key = %i(at at_location at_relative_location).find{ |key| options.key? key }
@current_options[:location] ? "#{@current_options[:location]}.#{options[key]}" : options[key]
else
@current_options[:location]
end
end

def extract_context_options! options = {}
{}.tap do |h|
%i(at at_location at_relative_location at_absolute_location).each do |key|
h[key] = options.delete key if options.key? key
end
end
end

def register_validations *args, &block

options = args.last.kind_of?(Hash) ? args.pop : {}
context_options = options.delete(:with_context) || {}
context_options = extract_context_options! options

custom_validators = []
custom_validators << options.delete(:with) if options[:with] # TODO: allow array
Expand All @@ -168,7 +182,7 @@ def register_validations *args, &block

each_options = {}
each_options[:each] = options.delete :each if options[:each]
each_options[:each_with_context] = options.delete :each_with_context if options[:each_with_context]
each_options[:each_options] = options.delete :each_options if options[:each_options]

# TODO: fail if there are no validations declared
args = [ nil ] if args.empty?
Expand Down Expand Up @@ -200,7 +214,7 @@ def register_validations *args, &block

validation.merge!({
validator_options: validator_options,
context_options: validator_options.delete(:with_context) || context_options,
context_options: context_options.merge(extract_context_options!(validator_options)),
conditions: conditions + @config.extract_conditions!(validator_options)
})

Expand Down
12 changes: 6 additions & 6 deletions spec/example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

errapi :with_age do
validates :name, presence: true
validates Proc.new(&:age), presence: true, with_context: { location: 'age' }
validates Proc.new(&:age), presence: true, at: 'age'
end
end

Expand Down Expand Up @@ -99,8 +99,8 @@
validates :baz, presence: true
end

validates :bar, with_context: { location: 'corge' } do
validates :qux, presence: true, with_context: { relative_location: 'grault' }
validates :bar, at_absolute_location: 'corge' do
validates :qux, presence: true, at_relative_location: 'grault'
end
end

Expand Down Expand Up @@ -167,10 +167,10 @@
validations = Errapi::ObjectValidations.new do
validates :foo, presence: true
validates :bar, presence: true, if_error: { location: 'foo' }
validates :baz, presence: { unless_error: { location: 'foo' } }
validates :baz, presence: { unless_error: { at_location: 'foo' } }
validates :qux do
validates :corge, presence: { unless_error: { location: 'foo' } }
validates :grault, presence: { if_error: { relative_location: 'corge' } }
validates :corge, presence: { unless_error: { at_absolute_location: 'foo' } }
validates :grault, presence: { if_error: { at_relative_location: 'corge' } }
end
end

Expand Down

0 comments on commit f67d92e

Please sign in to comment.