Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
114 additions
and
0 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,14 +1,68 @@ | ||
module Perpetuity | ||
class ValidationSet | ||
include Enumerable | ||
|
||
def initialize | ||
@validations = [] | ||
end | ||
|
||
def validations | ||
@validations | ||
end | ||
|
||
def << object | ||
@validations << object | ||
end | ||
|
||
def each &block | ||
validations.each &block | ||
end | ||
|
||
def valid? object | ||
each do |validation| | ||
return false unless validation.pass?(object) | ||
end | ||
|
||
true | ||
end | ||
|
||
def present attribute | ||
self << Perpetuity::Validations::Presence.new(attribute) | ||
end | ||
|
||
def length attribute, options = {} | ||
self << Perpetuity::Validations::Length.new(attribute, options) | ||
end | ||
end | ||
|
||
class Validation | ||
end | ||
|
||
module Validations | ||
class Presence | ||
def initialize attribute | ||
@attribute = attribute | ||
end | ||
|
||
def pass? object | ||
!object.send(@attribute).nil? | ||
end | ||
end | ||
|
||
class Length | ||
def initialize attribute, options | ||
@attribute = attribute | ||
@options = options | ||
end | ||
|
||
def pass? object | ||
length = object.send(@attribute).length | ||
|
||
return false unless @options[:at_least].nil? or @options[:at_least] <= length | ||
return false unless @options[:at_most].nil? or @options[:at_most] >= length | ||
|
||
true | ||
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