accept_values_for 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +6 -0
- data/Rakefile +26 -0
- data/Readme.rdoc +23 -0
- data/VERSION +1 -0
- data/lib/accept_values_for.rb +67 -0
- data/spec/accept_values_for_spec.rb +8 -0
- data/spec/spec_helper.rb +28 -0
- metadata +73 -0
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'spec/rake/spectask'
|
4
|
+
|
5
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
6
|
+
spec.libs << 'lib' << 'spec'
|
7
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
8
|
+
end
|
9
|
+
|
10
|
+
begin
|
11
|
+
require 'jeweler'
|
12
|
+
Jeweler::Tasks.new do |gemspec|
|
13
|
+
gemspec.name = "accept_values_for"
|
14
|
+
gemspec.summary = "In order to test a complex validation for ActiveRecord models Implemented accept_values_for custom rspec matcher"
|
15
|
+
gemspec.description = <<-EOI
|
16
|
+
When you have a complex validation(e.g. regexp or custom method) on ActiveRecord model
|
17
|
+
you have to write annoying easy specs on which values should be accepted by your validation method and which should not.
|
18
|
+
accepts_values_for rspec matcher simplify the code. See example for more information.
|
19
|
+
EOI
|
20
|
+
gemspec.email = "agresso@gmail.com"
|
21
|
+
gemspec.homepage = "http://github.com/bogdan/accept_values_for"
|
22
|
+
gemspec.authors = ["Bogdan Gusiev"]
|
23
|
+
end
|
24
|
+
rescue LoadError
|
25
|
+
puts "Jeweler not available. Install it with: [sudo] gem install jeweler"
|
26
|
+
end
|
data/Readme.rdoc
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
= Accept values for
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
== DESCRIPTION
|
6
|
+
|
7
|
+
In order to spec a complex validation for ActiveRecord models
|
8
|
+
Implemented accept_values_for custom matcher
|
9
|
+
|
10
|
+
== SYNOPSIS:
|
11
|
+
|
12
|
+
describe User do
|
13
|
+
|
14
|
+
subject { User.new(@valid_attributes)}
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
it { should accept_values_for(:email, "john@example.com", "lambda@gusiev.com") }
|
19
|
+
it { should_not accept_values_for(:email, "invalid", nil, "a@b", "john@.com") }
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,67 @@
|
|
1
|
+
if defined?(ActiveRecord)
|
2
|
+
|
3
|
+
# In order to spec a complex validation for ActiveRecord models
|
4
|
+
# Implemented accept_values_for custom matcher
|
5
|
+
#
|
6
|
+
# :call-seq:
|
7
|
+
# model.should accept_values_for(attribute, value1, value2 ...)
|
8
|
+
# model.should_not accept_values_for(attribute, value1, value2 ...)
|
9
|
+
#
|
10
|
+
# model should be a valid ActiveRecord model
|
11
|
+
# attribute should be the model attribute name
|
12
|
+
#
|
13
|
+
# Use this if you want to check that model will be valid
|
14
|
+
# with the given values for the given attribute
|
15
|
+
#
|
16
|
+
# == Examples
|
17
|
+
#
|
18
|
+
# user.should accept_values_for(:email, "john@example.com", "lambda@gusiev.com")
|
19
|
+
# user.should_not accept_values_for(:email, "invalid", nil, "a@b", "john@.com")
|
20
|
+
#
|
21
|
+
# IMPORTANT: passed model should be valid. Use fixtures or factories to create one.
|
22
|
+
#
|
23
|
+
def accept_values_for(attribute, *values)
|
24
|
+
AcceptValuesFor.new(attribute, *values)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
class AcceptValuesFor #:nodoc:
|
30
|
+
|
31
|
+
def initialize(attribute, *values)
|
32
|
+
@attribute = attribute
|
33
|
+
@values = values
|
34
|
+
end
|
35
|
+
|
36
|
+
def matches?(model)
|
37
|
+
@model = model
|
38
|
+
return false unless model.is_a?(ActiveRecord::Base) && model.has_attribute?(@attribute)
|
39
|
+
@values.each do |value|
|
40
|
+
model[@attribute] = value
|
41
|
+
unless model.valid?
|
42
|
+
@failed_value = value
|
43
|
+
return false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
return true
|
47
|
+
end
|
48
|
+
|
49
|
+
def failure_message_for_should
|
50
|
+
result = "expected #{@model.inspect} to accept value #{@failed_value.inspect} for #{@attribute.inspect}, but it was not\n"
|
51
|
+
if @model.respond_to?(:errors) && ActiveRecord::Errors === @model.errors
|
52
|
+
result += "Errors: " + @model.errors.full_messages.join(", ")
|
53
|
+
end
|
54
|
+
result
|
55
|
+
end
|
56
|
+
|
57
|
+
def failure_message_for_should_not
|
58
|
+
"expected #{@model.inspect} to not accept value #{@failed_value.inspect} for #{@attribute.inspect} attribute, but was"
|
59
|
+
end
|
60
|
+
|
61
|
+
def description
|
62
|
+
"accept values #{@values.map(&:inspect).join(', ')} for #{@attribute.inspect} attribute"
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
end
|
67
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
require 'spec/autorun'
|
4
|
+
require 'active_record'
|
5
|
+
require 'lib/accept_values_for'
|
6
|
+
|
7
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
8
|
+
ActiveRecord::Base.configurations = true
|
9
|
+
|
10
|
+
ActiveRecord::Schema.verbose = false
|
11
|
+
ActiveRecord::Schema.define(:version => 1) do
|
12
|
+
|
13
|
+
create_table :people do |t|
|
14
|
+
t.string :gender
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
Spec::Runner.configure do |config|
|
19
|
+
config.before(:each) do
|
20
|
+
class ::Person < ActiveRecord::Base
|
21
|
+
validates_inclusion_of :gender, :in => ["MALE", "FEMALE"]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
config.after(:each) do
|
26
|
+
Object.send(:remove_const, :Person)
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: accept_values_for
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Bogdan Gusiev
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-04 00:00:00 +03:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: |
|
22
|
+
When you have a complex validation(e.g. regexp or custom method) on ActiveRecord model
|
23
|
+
you have to write annoying easy specs on which values should be accepted by your validation method and which should not.
|
24
|
+
accepts_values_for rspec matcher simplify the code. See example for more information.
|
25
|
+
|
26
|
+
email: agresso@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- Gemfile
|
35
|
+
- Rakefile
|
36
|
+
- Readme.rdoc
|
37
|
+
- VERSION
|
38
|
+
- lib/accept_values_for.rb
|
39
|
+
- spec/accept_values_for_spec.rb
|
40
|
+
- spec/spec_helper.rb
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://github.com/bogdan/accept_values_for
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --charset=UTF-8
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.3.6
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: In order to test a complex validation for ActiveRecord models Implemented accept_values_for custom rspec matcher
|
71
|
+
test_files:
|
72
|
+
- spec/accept_values_for_spec.rb
|
73
|
+
- spec/spec_helper.rb
|