Skip to content

Commit

Permalink
Moved conditions back to validations.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaHydrae committed Nov 28, 2014
1 parent 8e49b06 commit ce1f932
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
34 changes: 0 additions & 34 deletions lib/errapi/validation_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ def with options = {}, &block
end

options = options.dup

conditions = options[:conditions] || []
return if conditions.any?{ |condition| !evaluate_condition(condition) }

original_properties = current_properties

if options[:each]
Expand Down Expand Up @@ -155,35 +151,5 @@ def relative_location base, relative
def location_to_string location
location.kind_of?(Hash) && location.key?(:value) ? location[:value].to_s : location.to_s
end

def evaluate_condition condition

value = @current_value

conditional, condition_type, predicate = if condition.key? :if
[ lambda{ |x| !!x }, :custom, condition[:if] ]
elsif condition.key? :unless
[ lambda{ |x| !x }, :custom, condition[:unless] ]
elsif condition.key? :if_error
[ lambda{ |x| !!x }, :error, condition[:if_error] ]
elsif condition.key? :unless_error
[ lambda{ |x| !x }, :error, condition[:unless_error] ]
end

result = case condition_type
when :custom
if predicate.kind_of?(Symbol) || predicate.kind_of?(String)
value.respond_to?(:[]) ? value[predicate] : value.send(predicate)
elsif predicate.respond_to? :call
predicate.call value, self
else
predicate
end
when :error
error? predicate
end

conditional.call result
end
end
end
33 changes: 33 additions & 0 deletions lib/errapi/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def validate context, options = {}
config = options.delete(:config) || Errapi.config

@validations.each do |validation|

next if validation[:conditions].any?{ |condition| !evaluate_condition(condition, context) }

context.with validation do

# TODO: store validation options separately to override
Expand Down Expand Up @@ -95,5 +98,35 @@ def extract_conditions! options = {}
conditions << { unless_error: options.delete(:unless_error) } if options[:unless_error]
end
end

def evaluate_condition condition, context

value = context.current_value

conditional, condition_type, predicate = if condition.key? :if
[ lambda{ |x| !!x }, :custom, condition[:if] ]
elsif condition.key? :unless
[ lambda{ |x| !x }, :custom, condition[:unless] ]
elsif condition.key? :if_error
[ lambda{ |x| !!x }, :error, condition[:if_error] ]
elsif condition.key? :unless_error
[ lambda{ |x| !x }, :error, condition[:unless_error] ]
end

result = case condition_type
when :custom
if predicate.kind_of?(Symbol) || predicate.kind_of?(String)
value.respond_to?(:[]) ? value[predicate] : value.send(predicate)
elsif predicate.respond_to? :call
predicate.call value, self
else
predicate
end
when :error
context.error? predicate
end

conditional.call result
end
end
end

0 comments on commit ce1f932

Please sign in to comment.