-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed registered validators to validator factories. Extracted condi…
…tions into classes.
- Loading branch information
Simon Oulevay
committed
Dec 19, 2014
1 parent
27d314b
commit 0db6ef4
Showing
6 changed files
with
118 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
class Errapi::Condition | ||
ALLOWED_CONDITIONALS = %i(if unless).freeze | ||
|
||
def self.conditionals | ||
h = const_get('CONDITIONALS') | ||
raise LoadError, "The CONDITIONALS constant in class #{self} is of the wrong type (#{h.class}). Either make it a Hash or override #{self}.conditionals to return a list of symbols." unless h.kind_of? Hash | ||
h.keys | ||
end | ||
|
||
def initialize conditional, predicate, options = {} | ||
|
||
@conditional = resolve_conditional conditional | ||
raise ArgumentError, "Conditional must be either :if or :unless" unless ALLOWED_CONDITIONALS.include? @conditional | ||
|
||
@predicate = predicate | ||
end | ||
|
||
def fulfilled? *args | ||
result = check @predicate, *args | ||
result = !result if @conditional == :unless | ||
result | ||
end | ||
|
||
def resolve_conditional conditional | ||
conditional | ||
end | ||
|
||
def check predicate, value, context, options = {} | ||
raise NotImplementedError, "Subclasses should implement the #check method to check whether the value matches the predicate of the condition" | ||
end | ||
|
||
class SimpleCheck < Errapi::Condition | ||
|
||
CONDITIONALS = { | ||
if: :if, | ||
unless: :unless | ||
}.freeze | ||
|
||
def check predicate, value, context, options = {} | ||
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, context, options | ||
else | ||
@predicate | ||
end | ||
end | ||
end | ||
|
||
class ErrorCheck < Errapi::Condition | ||
|
||
CONDITIONALS = { | ||
if_error: :if, | ||
unless_error: :unless | ||
}.freeze | ||
|
||
def resolve_conditional conditional | ||
CONDITIONALS[conditional] | ||
end | ||
|
||
def check predicate, value, context, options = {} | ||
if @predicate.respond_to? :call | ||
context.errors? &@predicate | ||
elsif @predicate.kind_of? Hash | ||
context.errors? @predicate | ||
else | ||
@predicate ? context.errors? : !context.errors? | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters