-
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.
Merge pull request #13 from mcorp/validate_length_of
Matcher to validate size of attribute (resolves #11)
- Loading branch information
Showing
7 changed files
with
122 additions
and
2 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
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,68 @@ | ||
module Shoulda | ||
module Lotus | ||
module Matchers | ||
def validate_length_of(attribute) | ||
ValidateLengthOfMatcher.new(attribute) | ||
end | ||
|
||
class ValidateLengthOfMatcher | ||
def initialize(attribute) | ||
@attribute = attribute | ||
end | ||
|
||
def matches?(target) | ||
errors = [] | ||
|
||
@target = target | ||
|
||
@target.send("#{@attribute}=", '*' * (minimum - 1)) | ||
@target.valid? | ||
errors << (@target.errors.for(@attribute).select { |error| error.attribute == @attribute.to_s && error.validation == :size }.size > 0) | ||
|
||
@target.send("#{@attribute}=", '*' * (maximum + 1)) | ||
@target.valid? | ||
errors << (@target.errors.for(@attribute).select { |error| error.attribute == @attribute.to_s && error.validation == :size }.size > 0) | ||
|
||
errors.all? { |error| error } | ||
end | ||
|
||
def description | ||
"'#{@attribute}' size between '#{@minimum}..#{@maximum}'" | ||
end | ||
|
||
def failure_message | ||
"'#{@attribute}' is not between '#{@minimum}..#{@maximum}'" | ||
end | ||
|
||
def failure_message_when_negated | ||
"'#{@attribute}' is between '#{@minimum}..#{@maximum}'" | ||
end | ||
|
||
def is_at_least(minimum) | ||
@minimum = minimum | ||
self | ||
end | ||
|
||
def is_at_most(maximum) | ||
@maximum = maximum | ||
self | ||
end | ||
|
||
def is_equal_to(limit) | ||
@minimum, @maximum = limit | ||
self | ||
end | ||
|
||
private | ||
|
||
def minimum | ||
@minimum ||= 1 | ||
end | ||
|
||
def maximum | ||
@maximum ||= 1 | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module Shoulda | ||
module Lotus | ||
VERSION = '0.0.2'.freeze | ||
VERSION = '0.0.3'.freeze | ||
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,6 @@ | ||
class WithValidationLengthModel | ||
include Lotus::Validations | ||
|
||
attribute :name_equal_10, size: 10 | ||
attribute :name_between_1_10, size: 1..10 | ||
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,35 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe Shoulda::Lotus::Matchers::ValidateLengthOfMatcher do | ||
include Shoulda::Lotus::Matchers | ||
|
||
it '#description' do | ||
matcher = validate_length_of(:bar).is_at_least(1).is_at_most(10) | ||
|
||
expect(matcher.description).to eq "'bar' size between '1..10'" | ||
end | ||
|
||
context 'an attribute with a validation' do | ||
let(:model) { WithValidationLengthModel.new } | ||
|
||
it 'accepts an exact' do | ||
expect(model).to validate_length_of(:name_equal_10).is_equal_to(10) | ||
end | ||
|
||
it 'accepts an interval' do | ||
expect(model).to validate_length_of(:name_between_1_10).is_at_least(1).is_at_most(10) | ||
end | ||
|
||
it 'provides correct failure message' | ||
end | ||
|
||
context 'an attribute without validation' do | ||
let(:model) { WithoutValidationModel.new } | ||
|
||
it 'does not validate' do | ||
expect(model).to_not validate_length_of(:email) | ||
end | ||
|
||
it 'provides correct failure message' | ||
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