-
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.
Split context into context and state.
- Loading branch information
Simon Oulevay
committed
Nov 25, 2014
1 parent
18a5052
commit 23237f4
Showing
3 changed files
with
62 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,15 @@ | ||
module Errapi | ||
|
||
class ValidationContext | ||
attr_reader :errors | ||
attr_reader :state | ||
|
||
def initialize | ||
@errors = [] | ||
def initialize state | ||
@state = state | ||
end | ||
|
||
def add_error options = {}, &block | ||
@errors << ValidationError.new(options, &block) | ||
@state.add_error options, &block | ||
self | ||
end | ||
|
||
def validate value, options = {} | ||
if yield value, options | ||
true | ||
else | ||
add_error options[:error] | ||
false | ||
end | ||
end | ||
|
||
def error? criteria = {} | ||
return !@errors.empty? if criteria.empty? | ||
@errors.any?{ |err| err.matches? criteria } | ||
end | ||
|
||
def valid? | ||
!error? | ||
end | ||
|
||
def clear | ||
@errors.clear | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Errapi | ||
|
||
class ValidationState | ||
attr_reader :errors | ||
|
||
def initialize | ||
@errors = [] | ||
end | ||
|
||
def add_error options = {}, &block | ||
@errors << ValidationError.new(options, &block) | ||
self | ||
end | ||
|
||
def validate value, options = {} | ||
if yield value, options | ||
true | ||
else | ||
add_error options[:error] | ||
false | ||
end | ||
end | ||
|
||
def error? criteria = {} | ||
return !@errors.empty? if criteria.empty? | ||
@errors.any?{ |err| err.matches? criteria } | ||
end | ||
|
||
def valid? | ||
!error? | ||
end | ||
|
||
def clear | ||
@errors.clear | ||
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