validate_block 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +3 -0
- data/Manifest.txt +12 -0
- data/README.rdoc +57 -0
- data/Rakefile +20 -0
- data/lib/validate_block.rb +23 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/validate_block_spec.rb +5 -0
- data/tasks/rspec.rake +21 -0
- metadata +135 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
= validate_block
|
2
|
+
|
3
|
+
* http://github.com/xunker/validate_block
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
This gem allows similar ActiveRecord validates_* commands to be grouped together in blocks and pruned of repeated parameters.
|
8
|
+
|
9
|
+
How often have you had a block of validation commands in an ActiveRecord object that are repeated, especially :id or :unless options? Does this look familiar?
|
10
|
+
|
11
|
+
validates_presence_of :hair, :unless => :bald?
|
12
|
+
validates_length_of :hair, :within => 3..15, :unless => :bald?
|
13
|
+
validates_inclusion_of :hair_color, :in => HAIR_COLORS, :unless => bald?
|
14
|
+
|
15
|
+
Instead, this gem will allow you to replace the above code with:
|
16
|
+
|
17
|
+
validate_block :unless => :bald? do
|
18
|
+
presence_of :hair
|
19
|
+
length_of :hair, :within => 3..15
|
20
|
+
inclusion_of :hair_color, :in => HAIR_COLORS
|
21
|
+
end
|
22
|
+
|
23
|
+
..which is a great way to DRY your :hair, don't you think?
|
24
|
+
|
25
|
+
Basically, this gem 1) removes the requirement to have 'validates_' on the front of the commands and 2) passes the options on the validate_block command to each validation command inside the block.
|
26
|
+
|
27
|
+
The syntax of the validation commands remains the same. Keeping the 'validate_*' prefix on the commands inside the block _will_ work but it is not required.
|
28
|
+
|
29
|
+
WARNING: There are no specs for this yet because I suck. Actually, it's because I don't properly understand how to spec a module like this so any help on that front would be appreciated.
|
30
|
+
|
31
|
+
== SYNOPSIS:
|
32
|
+
|
33
|
+
require 'rubygems'
|
34
|
+
require 'activerecord'
|
35
|
+
require 'validate_block'
|
36
|
+
|
37
|
+
class SomeObject < ActiveRecord::Base
|
38
|
+
|
39
|
+
validate_block :if => :some_method? do
|
40
|
+
presence_of :some_field
|
41
|
+
inclusion_of :some_other_field, :in => SOME_VALUES
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
== REQUIREMENTS:
|
47
|
+
|
48
|
+
ActiveRecord >= 2.1.0
|
49
|
+
Rspec (for specs)
|
50
|
+
|
51
|
+
== INSTALL:
|
52
|
+
|
53
|
+
sudo gem install validate_block
|
54
|
+
|
55
|
+
== LICENSE:
|
56
|
+
|
57
|
+
http://sam.zoy.org/wtfpl/COPYING
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'hoe', '>= 2.1.0'
|
3
|
+
gem 'activerecord', '>= 2.1.0'
|
4
|
+
gem 'rspec', '>= 1.3.0'
|
5
|
+
require 'hoe'
|
6
|
+
require 'fileutils'
|
7
|
+
require './lib/validate_block'
|
8
|
+
|
9
|
+
VERSION = "0.1.0"
|
10
|
+
|
11
|
+
Hoe.plugin :newgem
|
12
|
+
|
13
|
+
$hoe = Hoe.spec 'validate_block' do
|
14
|
+
self.developer 'Matthew Nielsen', 'xunker@pyxidis.org'
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'newgem/tasks'
|
18
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
19
|
+
|
20
|
+
task :default => :spec
|
@@ -0,0 +1,23 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
module Validations
|
6
|
+
module ClassMethods
|
7
|
+
def validate_block(opts = {}, &block)
|
8
|
+
@block_opts = opts || {}
|
9
|
+
yield
|
10
|
+
end
|
11
|
+
|
12
|
+
def method_missing(name, attrib, opts = {}, &block)
|
13
|
+
if name.to_s =~ /^validates_/
|
14
|
+
send(name, attrib, opts.merge(@block_opts))
|
15
|
+
elsif respond_to?(("validates_"+name.to_s).to_sym)
|
16
|
+
send(("validates_"+name.to_s).to_sym, attrib, opts.merge(@block_opts))
|
17
|
+
else
|
18
|
+
super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/validate_block.rb'}"
|
9
|
+
puts "Loading validate_block gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/spec/spec_helper.rb
ADDED
data/tasks/rspec.rake
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
begin
|
2
|
+
require 'spec'
|
3
|
+
rescue LoadError
|
4
|
+
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
5
|
+
require 'spec'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'spec/rake/spectask'
|
9
|
+
rescue LoadError
|
10
|
+
puts <<-EOS
|
11
|
+
To use rspec for testing you must install rspec gem:
|
12
|
+
gem install rspec
|
13
|
+
EOS
|
14
|
+
exit(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Run the specs under spec/models"
|
18
|
+
Spec::Rake::SpecTask.new do |t|
|
19
|
+
t.spec_opts = ['--options', "spec/spec.opts"]
|
20
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validate_block
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Matthew Nielsen
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-08-20 00:00:00 -06:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rubyforge
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 0
|
33
|
+
- 4
|
34
|
+
version: 2.0.4
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: hoe
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 21
|
46
|
+
segments:
|
47
|
+
- 2
|
48
|
+
- 6
|
49
|
+
- 1
|
50
|
+
version: 2.6.1
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
53
|
+
description: |-
|
54
|
+
This gem allows similar ActiveRecord validates_* commands to be grouped together in blocks and pruned of repeated parameters.
|
55
|
+
|
56
|
+
How often have you had a block of validation commands in an ActiveRecord object that are repeated, especially :id or :unless options? Does this look familiar?
|
57
|
+
|
58
|
+
validates_presence_of :hair, :unless => :bald?
|
59
|
+
validates_length_of :hair, :within => 3..15, :unless => :bald?
|
60
|
+
validates_inclusion_of :hair_color, :in => HAIR_COLORS, :unless => bald?
|
61
|
+
|
62
|
+
Instead, this gem will allow you to replace the above code with:
|
63
|
+
|
64
|
+
validate_block :unless => :bald? do
|
65
|
+
presence_of :hair
|
66
|
+
length_of :hair, :within => 3..15
|
67
|
+
inclusion_of :hair_color, :in => HAIR_COLORS
|
68
|
+
end
|
69
|
+
|
70
|
+
..which is a great way to DRY your :hair, don't you think?
|
71
|
+
|
72
|
+
Basically, this gem 1) removes the requirement to have 'validates_' on the front of the commands and 2) passes the options on the validate_block command to each validation command inside the block.
|
73
|
+
|
74
|
+
The syntax of the validation commands remains the same. Keeping the 'validate_*' prefix on the commands inside the block _will_ work but it is not required.
|
75
|
+
|
76
|
+
WARNING: There are no specs for this yet because I suck. Actually, it's because I don't properly understand how to spec a module like this so any help on that front would be appreciated.
|
77
|
+
email:
|
78
|
+
- xunker@pyxidis.org
|
79
|
+
executables: []
|
80
|
+
|
81
|
+
extensions: []
|
82
|
+
|
83
|
+
extra_rdoc_files:
|
84
|
+
- History.txt
|
85
|
+
- Manifest.txt
|
86
|
+
files:
|
87
|
+
- History.txt
|
88
|
+
- Manifest.txt
|
89
|
+
- README.rdoc
|
90
|
+
- Rakefile
|
91
|
+
- lib/validate_block.rb
|
92
|
+
- script/console
|
93
|
+
- script/destroy
|
94
|
+
- script/generate
|
95
|
+
- spec/spec.opts
|
96
|
+
- spec/spec_helper.rb
|
97
|
+
- spec/validate_block_spec.rb
|
98
|
+
- tasks/rspec.rake
|
99
|
+
has_rdoc: true
|
100
|
+
homepage: http://github.com/xunker/validate_block
|
101
|
+
licenses: []
|
102
|
+
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options:
|
105
|
+
- --main
|
106
|
+
- README.rdoc
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
hash: 3
|
115
|
+
segments:
|
116
|
+
- 0
|
117
|
+
version: "0"
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
hash: 3
|
124
|
+
segments:
|
125
|
+
- 0
|
126
|
+
version: "0"
|
127
|
+
requirements: []
|
128
|
+
|
129
|
+
rubyforge_project: validate_block
|
130
|
+
rubygems_version: 1.3.7
|
131
|
+
signing_key:
|
132
|
+
specification_version: 3
|
133
|
+
summary: This gem allows similar ActiveRecord validates_* commands to be grouped together in blocks and pruned of repeated parameters
|
134
|
+
test_files: []
|
135
|
+
|