Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to verity #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source 'https://rubygems.org'
source 'http://dist.dev.int.realestate.com.au/rubygems/'

# Specify your gem's dependencies in biq-rake.gemspec
gemspec

gem 'bundler', '~> 1.3.0'
gem "rake"
35 changes: 35 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
PATH
remote: .
specs:
verity (0.0.1)

GEM
remote: https://rubygems.org/
remote: http://dist.dev.int.realestate.com.au/rubygems/
specs:
backports (3.1.0)
diff-lcs (1.2.1)
rake (10.0.3)
rspec (2.13.0)
rspec-core (~> 2.13.0)
rspec-expectations (~> 2.13.0)
rspec-mocks (~> 2.13.0)
rspec-core (2.13.0)
rspec-expectations (2.13.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.13.0)
yard (0.8.5.2)
yardstick (0.9.4)
backports (~> 3.0, >= 3.0.3)
yard (~> 0.8.5)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 1.3.0)
rake
rspec (>= 2.0.0)
verity!
yard (>= 0.5.3)
yardstick (>= 0.1.0)
4 changes: 2 additions & 2 deletions lib/verity/core/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module ClassMethods

def self.extended(klass)
klass.class_eval do
@@validations = Hash.new { |hash, key| hash[key] = {} }
@validations = Hash.new { |hash, key| hash[key] = {} }
end
end

Expand All @@ -72,7 +72,7 @@ def self.extended(klass)
# @return [Hash] The validations defined on the class.
# @api public
def validations
@@validations
@validations
end

end
Expand Down
29 changes: 16 additions & 13 deletions lib/verity/core/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,23 @@ def method_missing(name, *args)
begin
super
rescue NameError
predicate = args.shift
predicate.configure(*args)
case name.to_s

when /^(.*)_must$/
attribute = $1.to_sym
(validations[attribute][:positive] ||= []) << predicate
true
when /^(.*)_must_not$/
attribute = $1.to_sym
(validations[attribute][:negative] ||= []) << predicate
true
else
raise
when /^(.*)_must$/
predicate = args.shift
predicate.configure(*args)
attribute = $1.to_sym
(validations[attribute][:positive] ||= []) << predicate
true
when /^(.*)_must_not$/
predicate = args.shift
predicate.configure(*args)
attribute = $1.to_sym
(validations[attribute][:negative] ||= []) << predicate
true
when /^be_(.*)/
Verity::Predicates::Be.new($1)
else
raise
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/verity/predicates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ module Verity::Predicates
require 'verity/predicates/blank'
require 'verity/predicates/length'
require 'verity/predicates/format'
require 'verity/predicates/be'
require 'verity/predicates/present'
4 changes: 2 additions & 2 deletions lib/verity/predicates/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def matches?(value)
end

def positive_error_for(value)
"is invalid."
"must be #{self.class.name.split('::').last.downcase}"
end

def negative_error_for(value)
"is invalid."
"must not be #{self.class.name.split('::').last.downcase}"
end

protected
Expand Down
24 changes: 24 additions & 0 deletions lib/verity/predicates/be.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Verity::Predicates
# @private
class Be < Base

def initialize(name, *args)
@predicate_name = name
@method_name = name + "?"
super(*args)
end

def matches?(value)
value.respond_to?(@method_name) && value.send(@method_name)
end

def positive_error_for(value)
"must be #{@predicate_name}"
end

def negative_error_for(value)
"must not be #{@predicate_name}"
end
end

end
15 changes: 15 additions & 0 deletions lib/verity/predicates/present.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Verity::Predicates
# @private
class Present < Base
def matches?(value)
!(value.nil? || (value.respond_to?(:empty?) && value.empty?))
end
end

# Is `true` when the attribute is `present`.
#
# @api public
def be_present
Present.new
end
end
59 changes: 59 additions & 0 deletions spec/predicates/be_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'spec_helper'

module Verity::Predicates
describe Be do

subject { Be.new('empty') }

it 'does not match nil' do
expect(subject.matches?(nil)).to be_false
end

it 'matches empty strings' do
expect(subject.matches?('')).to be_true
end

it 'matches empty collections' do
expect(subject.matches?([])).to be_true
end

it 'does not match other objects' do
expect(subject.matches?('non-empty string')).to be_false
end

end

end

describe 'empty predicate' do

before :all do
class User
verifiable

attr_accessor :data
data_must_not be_empty
end
end

subject { User.new }

context 'with empty data' do

before do
subject.data = []
end

it { should_not be_valid }
end

context 'with some data' do

before do
subject.data = ['stuff']
end

it { should be_valid }
end

end
78 changes: 78 additions & 0 deletions spec/predicates/present_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require 'spec_helper'

module Verity::Predicates
describe Present do

it 'does not match nil' do
expect(subject.matches?(nil)).to be_false
end

it 'does not match empty strings' do
expect(subject.matches?('')).to be_false
end

it 'matches non-empty strings' do
expect(subject.matches?('non-empty string')).to be_true
end

it 'does not match empty arrays' do
expect(subject.matches?([])).to be_false
end

it 'matches non-empty arrays' do
expect(subject.matches?([:stuff])).to be_true
end

it 'does not match empty hashes' do
expect(subject.matches?({})).to be_false
end

it 'matches non-empty hashes' do
expect(subject.matches?({stuff: 'things'})).to be_true
end

it 'matches other non-nil objects' do
expect(subject.matches?(22)).to be_true
end

end

end

describe 'present predicate' do

before :all do
class User
verifiable

attr_accessor :data
data_must be_present
end
end

subject { User.new }

context 'with empty data' do

before do
subject.data = []
end

it { should_not be_valid }

it "has the correct error message" do
subject.valid?
expect(subject.validation_errors).to include(:data => ['must be present'])
end
end

context 'with some data' do

before do
subject.data = ['stuff']
end

it { should be_valid }
end

end
21 changes: 3 additions & 18 deletions verity.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,8 @@ Gem::Specification.new do |s|
"spec/spec_helper.rb"
]

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
s.add_development_dependency(%q<yard>, [">= 0.5.3"])
s.add_development_dependency(%q<yardstick>, [">= 0.1.0"])
else
s.add_dependency(%q<rspec>, [">= 1.3.0"])
s.add_dependency(%q<yard>, [">= 0.5.3"])
s.add_dependency(%q<yardstick>, [">= 0.1.0"])
end
else
s.add_dependency(%q<rspec>, [">= 1.3.0"])
s.add_dependency(%q<yard>, [">= 0.5.3"])
s.add_dependency(%q<yardstick>, [">= 0.1.0"])
end
s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
s.add_development_dependency(%q<yard>, [">= 0.5.3"])
s.add_development_dependency(%q<yardstick>, [">= 0.1.0"])
end