attr_enumerator 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +2 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +89 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/attr_enumerator.gemspec +77 -0
- data/lib/attr_enumerator.rb +73 -0
- data/spec/attr_enumerator_spec.rb +229 -0
- data/spec/spec_helper.rb +14 -0
- data/watchr.rb +13 -0
- metadata +199 -0
data/.document
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Chris Baker
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
= attr_enumerator
|
2
|
+
|
3
|
+
A customizable method for restricting an attribute to a set of choices. By default, <tt>attr_enumerator</tt> will create:
|
4
|
+
|
5
|
+
* A <b>validation</b> requiring the attribute value to be one of the choices
|
6
|
+
* A <b>class constant</b> containing all choices
|
7
|
+
* <b>Instance methods</b> for each choice, returning <tt>true</tt> if the attribute is that value and <tt>false</tt> otherwise
|
8
|
+
* <b>Scopes</b> for each choice that retrieve records where the attribute is that value (only applicable when used with <tt>ActiveRecord</tt>)
|
9
|
+
|
10
|
+
<tt>AttrEnumerator</tt> is automatically included in <tt>ActiveRecord::Base</tt> and is available for use in <tt>ActiveRecord</tt> models.
|
11
|
+
|
12
|
+
class Car < ActiveRecord::Base
|
13
|
+
attr_enumerator :color, ["red", "blue"]
|
14
|
+
end
|
15
|
+
|
16
|
+
To use <tt>attr_enumerator</tt> on a class that does not descend from <tt>ActiveRecord::Base</tt> it must include <tt>ActiveModel::Validations</tt> and provide an accessor to the attribute.
|
17
|
+
|
18
|
+
class Car
|
19
|
+
include ActiveModel::Validations
|
20
|
+
include AttrEnumerator
|
21
|
+
|
22
|
+
attr_accessor :color
|
23
|
+
attr_enumerator :color, ["red", "blue"]
|
24
|
+
end
|
25
|
+
|
26
|
+
== Configuration
|
27
|
+
|
28
|
+
Options may be passed to <tt>attr_enumerator</tt> to configure its behavior. For instance, use <tt>:create_constant => false</tt> to prevent a class constant from being created.
|
29
|
+
|
30
|
+
class Car < ActiveRecord::Base
|
31
|
+
attr_enumerator :color, ["red", "blue"], :create_constant => false
|
32
|
+
end
|
33
|
+
|
34
|
+
Options that may be passed directly to <tt>attr_enumerator</tt> may also be configured globally. Options passed directly to <tt>attr_enumerator</tt> will override global options.
|
35
|
+
|
36
|
+
AttrEnumerator::Options.configure do |config|
|
37
|
+
config.create_constant = false
|
38
|
+
end
|
39
|
+
|
40
|
+
=== Options
|
41
|
+
* <tt>create_constant</tt> (default: <tt>true</tt>)
|
42
|
+
|
43
|
+
When <tt>true</tt>, a class constant containing an array of choices is created using the pluralized, uppercased attribute name. Set to a string or symbol to change the constant name or <tt>false</tt> to not create the constant.
|
44
|
+
|
45
|
+
* <tt>create_methods</tt> (default: <tt>true</tt>)
|
46
|
+
|
47
|
+
When <tt>true</tt>, create a method for each choice that returns <tt>true</tt> if the attribute is that value and <tt>false</tt> otherwise. Use the <tt>prefix</tt> and <tt>suffix</tt> options to customize the method names. Set to <tt>false</tt> to not create methods.
|
48
|
+
|
49
|
+
* <tt>create_scopes</tt> (default: <tt>true</tt>, ignored when not using <tt>ActiveRecord</tt>)
|
50
|
+
|
51
|
+
When <tt>true</tt>, create a scope for each choice that retrieves records where the attribute is that value. Use the <tt>prefix</tt> and <tt>suffix</tt> options to customize the scope names. Set to <tt>false</tt> to not create scopes.
|
52
|
+
|
53
|
+
* <tt>prefix</tt> (default: <tt>true</tt>)
|
54
|
+
|
55
|
+
When <tt>true</tt>, prefixes created methods and scopes with the attribute name. Set to a string or symbol to change the prefix or <tt>false</tt> to disable.
|
56
|
+
|
57
|
+
* <tt>suffix</tt> (default: <tt>false</tt>)
|
58
|
+
|
59
|
+
See <tt>prefix</tt>.
|
60
|
+
|
61
|
+
* <tt>message</tt> (default: <tt>"is invalid"</tt>)
|
62
|
+
|
63
|
+
Specifies the error message when the attribute is not a valid value. Use <tt>%{value}</tt> to refer to the value of the attribute, e.g. <tt>"%{value} is not a valid choice"</tt>.
|
64
|
+
|
65
|
+
* <tt>allow_nil</tt> (default: <tt>false</tt>)
|
66
|
+
|
67
|
+
Allow the attribute to be <tt>nil</tt>.
|
68
|
+
|
69
|
+
* <tt>allow_blank</tt> (default: <tt>false</tt>)
|
70
|
+
|
71
|
+
Allow the attribute to be blank.
|
72
|
+
|
73
|
+
* <tt>if</tt> (default: unused)
|
74
|
+
|
75
|
+
Specifies a method, proc or string to call to determine if the validation should occur. See <tt>ActiveModel::Validations</tt> for more details.
|
76
|
+
|
77
|
+
* <tt>unless</tt>
|
78
|
+
|
79
|
+
Opposite of <tt>if</tt>.
|
80
|
+
|
81
|
+
== Contributing to attr_enumerator
|
82
|
+
|
83
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
84
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
85
|
+
* Fork the project
|
86
|
+
* Start a feature/bugfix branch
|
87
|
+
* Commit and push until you are happy with your contribution
|
88
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
89
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "attr_enumerator"
|
16
|
+
gem.homepage = "http://github.com/chrisb87/attr_enumerator"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{Enumerated attributes for ActiveModel}
|
19
|
+
gem.description = %Q{Enumerated attributes for ActiveModel}
|
20
|
+
gem.email = "baker@socialvibe.com"
|
21
|
+
gem.authors = ["Chris Baker"]
|
22
|
+
end
|
23
|
+
Jeweler::RubygemsDotOrgTasks.new
|
24
|
+
|
25
|
+
require 'rspec/core'
|
26
|
+
require 'rspec/core/rake_task'
|
27
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
28
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
29
|
+
end
|
30
|
+
|
31
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
32
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
33
|
+
spec.rcov = true
|
34
|
+
end
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "attr_enumerator #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.3.0
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{attr_enumerator}
|
8
|
+
s.version = "0.3.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Chris Baker"]
|
12
|
+
s.date = %q{2011-03-25}
|
13
|
+
s.description = %q{Enumerated attributes for ActiveModel}
|
14
|
+
s.email = %q{baker@socialvibe.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"Gemfile",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"attr_enumerator.gemspec",
|
28
|
+
"lib/attr_enumerator.rb",
|
29
|
+
"spec/attr_enumerator_spec.rb",
|
30
|
+
"spec/spec_helper.rb",
|
31
|
+
"watchr.rb"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://github.com/chrisb87/attr_enumerator}
|
34
|
+
s.licenses = ["MIT"]
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
s.rubygems_version = %q{1.3.7}
|
37
|
+
s.summary = %q{Enumerated attributes for ActiveModel}
|
38
|
+
s.test_files = [
|
39
|
+
"spec/attr_enumerator_spec.rb",
|
40
|
+
"spec/spec_helper.rb"
|
41
|
+
]
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_runtime_dependency(%q<activemodel>, [">= 3.0.0"])
|
49
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0"])
|
50
|
+
s.add_development_dependency(%q<activerecord>, [">= 3.0.0"])
|
51
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
52
|
+
s.add_development_dependency(%q<rspec>, [">= 2.5.0"])
|
53
|
+
s.add_development_dependency(%q<ruby-debug>, [">= 0"])
|
54
|
+
s.add_development_dependency(%q<sqlite3>, [">= 0"])
|
55
|
+
s.add_development_dependency(%q<watchr>, [">= 0"])
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<activemodel>, [">= 3.0.0"])
|
58
|
+
s.add_dependency(%q<activesupport>, [">= 3.0.0"])
|
59
|
+
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
|
60
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
61
|
+
s.add_dependency(%q<rspec>, [">= 2.5.0"])
|
62
|
+
s.add_dependency(%q<ruby-debug>, [">= 0"])
|
63
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
64
|
+
s.add_dependency(%q<watchr>, [">= 0"])
|
65
|
+
end
|
66
|
+
else
|
67
|
+
s.add_dependency(%q<activemodel>, [">= 3.0.0"])
|
68
|
+
s.add_dependency(%q<activesupport>, [">= 3.0.0"])
|
69
|
+
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
|
70
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
71
|
+
s.add_dependency(%q<rspec>, [">= 2.5.0"])
|
72
|
+
s.add_dependency(%q<ruby-debug>, [">= 0"])
|
73
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
74
|
+
s.add_dependency(%q<watchr>, [">= 0"])
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'active_support'
|
3
|
+
|
4
|
+
module AttrEnumerator
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def attr_enumerator(field, choices, opts={})
|
9
|
+
options = Options.parse(field, choices, opts, self)
|
10
|
+
|
11
|
+
validates field, :inclusion => options[:validation_options]
|
12
|
+
|
13
|
+
if options[:create_constant]
|
14
|
+
const_set(options[:constant], choices).freeze
|
15
|
+
end
|
16
|
+
|
17
|
+
options[:formatted_choices].each do |formatted_choice, choice|
|
18
|
+
if options[:create_methods]
|
19
|
+
define_method(formatted_choice + '?'){ self.send(field) == choice }
|
20
|
+
end
|
21
|
+
|
22
|
+
if options[:create_scopes]
|
23
|
+
scope formatted_choice, where(field => choice)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Options
|
30
|
+
include ActiveSupport::Configurable
|
31
|
+
|
32
|
+
config.create_constant = true
|
33
|
+
config.create_methods = true
|
34
|
+
config.create_scopes = true
|
35
|
+
config.prefix = true
|
36
|
+
config.suffix = false
|
37
|
+
config.message = :invalid
|
38
|
+
|
39
|
+
VALIDATION_OPTIONS = [:in, :message, :allow_nil, :allow_blank, :if, :unless].freeze
|
40
|
+
|
41
|
+
def self.parse(field, choices, opts, klass)
|
42
|
+
{}.merge!(config).merge!(opts).merge!(:in => choices).tap do |options|
|
43
|
+
options[:validation_options] = options.slice(*VALIDATION_OPTIONS)
|
44
|
+
options.reject!{|k,v| VALIDATION_OPTIONS.include?(k)}
|
45
|
+
|
46
|
+
options[:create_scopes] &= klass.respond_to?(:scope)
|
47
|
+
|
48
|
+
[:prefix, :suffix].each do |affix|
|
49
|
+
options[affix] = case options[affix]
|
50
|
+
when true then field.to_s
|
51
|
+
when false then nil
|
52
|
+
else options[affix].to_s
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
options[:constant] = case options[:create_constant]
|
57
|
+
when true then field.to_s.pluralize.upcase
|
58
|
+
when false then nil
|
59
|
+
else options[:create_constant].to_s
|
60
|
+
end
|
61
|
+
|
62
|
+
options[:formatted_choices] = choices.map do |choice|
|
63
|
+
formatted_choice = choice.to_s.underscore.parameterize('_')
|
64
|
+
formatted_choice.insert(0, options[:prefix] + '_') unless options[:prefix].blank?
|
65
|
+
formatted_choice << '_' + options[:suffix] unless options[:suffix].blank?
|
66
|
+
[formatted_choice, choice]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
ActiveRecord::Base.class_eval { include AttrEnumerator } if defined? ActiveRecord
|
@@ -0,0 +1,229 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "AttrEnumerator" do
|
4
|
+
subject { TestModel }
|
5
|
+
|
6
|
+
def instance
|
7
|
+
@instance ||= subject.new
|
8
|
+
end
|
9
|
+
|
10
|
+
after(:each) do
|
11
|
+
begin
|
12
|
+
Object.send(:remove_const, :TestModel)
|
13
|
+
rescue NameError
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "without ActiveRecord" do
|
18
|
+
before(:each) do
|
19
|
+
class TestModel
|
20
|
+
include ActiveModel::Validations
|
21
|
+
include AttrEnumerator
|
22
|
+
attr_accessor :choice
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "validation" do
|
27
|
+
it "should pass when the value is one of the choices" do
|
28
|
+
subject.attr_enumerator :choice, ['red', 'blue']
|
29
|
+
instance.choice = 'blue'
|
30
|
+
instance.should be_valid
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should fail when the value is not one of the choices" do
|
34
|
+
subject.attr_enumerator :choice, ['red', 'blue']
|
35
|
+
instance.choice = 'green'
|
36
|
+
instance.should_not be_valid
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should have a default message" do
|
40
|
+
subject.attr_enumerator :choice, ['red', 'blue']
|
41
|
+
instance.valid?
|
42
|
+
instance.errors.should == {:choice => ['is invalid']}
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should allow for a custom message" do
|
46
|
+
subject.attr_enumerator :choice, ['red', 'blue'], :message => '%{value} is not a valid color'
|
47
|
+
instance.choice = 'green'
|
48
|
+
instance.valid?
|
49
|
+
instance.errors.should == {:choice => ['green is not a valid color']}
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should handle allow_blank" do
|
53
|
+
subject.attr_enumerator :choice, ['red', 'blue'], :allow_blank => true
|
54
|
+
|
55
|
+
instance.choice = nil
|
56
|
+
instance.should be_valid
|
57
|
+
|
58
|
+
instance.choice = ''
|
59
|
+
instance.should be_valid
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should handle allow_nil" do
|
63
|
+
subject.attr_enumerator :choice, ['red', 'blue'], :allow_nil => true
|
64
|
+
|
65
|
+
instance.choice = nil
|
66
|
+
instance.should be_valid
|
67
|
+
|
68
|
+
instance.choice = ''
|
69
|
+
instance.should_not be_valid
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should handle symbols as enumerations" do
|
73
|
+
subject.attr_enumerator :choice, [:red, :blue]
|
74
|
+
|
75
|
+
instance.choice = :red
|
76
|
+
instance.should be_valid
|
77
|
+
instance.should be_choice_red
|
78
|
+
|
79
|
+
instance.choice = 'red'
|
80
|
+
instance.should_not be_valid
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context "class constant" do
|
85
|
+
it "should create a constant by default" do
|
86
|
+
subject.attr_enumerator :choice, ['red', 'blue']
|
87
|
+
subject::CHOICES.should == ['red', 'blue']
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should freeze the constant to prevent editing" do
|
91
|
+
subject.attr_enumerator :choice, ['red', 'blue']
|
92
|
+
subject::CHOICES.should be_frozen
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should allow for explicitly creating the default constant" do
|
96
|
+
subject.attr_enumerator :choice, ['red', 'blue'], :create_constant => true
|
97
|
+
subject::CHOICES.should == ['red', 'blue']
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should allow for a custom constant name using a symbol" do
|
101
|
+
subject.attr_enumerator :choice, ['red', 'blue'], :create_constant => :POSSIBLE_COLORS
|
102
|
+
subject::POSSIBLE_COLORS.should == ['red', 'blue']
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should allow for a custom constant name using a string" do
|
106
|
+
subject.attr_enumerator :choice, ['red', 'blue'], :create_constant => 'POSSIBLE_COLORS'
|
107
|
+
subject::POSSIBLE_COLORS.should == ['red', 'blue']
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should allow for not creating a constant" do
|
111
|
+
subject.attr_enumerator :choice, ['red', 'blue'], :create_constant => false
|
112
|
+
subject.constants.should_not include('COLORS')
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
context "methods" do
|
117
|
+
it "should create methods for each enumeration by default" do
|
118
|
+
subject.attr_enumerator :choice, ['red', 'blue']
|
119
|
+
instance.choice = 'red'
|
120
|
+
|
121
|
+
instance.should respond_to :choice_red?
|
122
|
+
instance.should be_choice_red
|
123
|
+
|
124
|
+
instance.should respond_to :choice_blue?
|
125
|
+
instance.should_not be_choice_blue
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should create methods with friendly names" do
|
129
|
+
enumerations = {
|
130
|
+
:has_space? => 'has space',
|
131
|
+
:has_dash? => 'has-dash',
|
132
|
+
:other_characters? => 'other%*characters',
|
133
|
+
:uppercase? => 'UPPERCASE',
|
134
|
+
:camel_case? => 'CamelCase',
|
135
|
+
:ends_with_dot? => 'ends.with.dot.'
|
136
|
+
}
|
137
|
+
|
138
|
+
subject.attr_enumerator :choice, enumerations.values, :prefix => false, :suffix => false
|
139
|
+
|
140
|
+
enumerations.keys.each do |method_name|
|
141
|
+
instance.should respond_to method_name
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should allow for explicitly creating the default methods" do
|
146
|
+
subject.attr_enumerator :choice, ['red', 'blue'], :create_methods => true
|
147
|
+
instance.choice = 'red'
|
148
|
+
|
149
|
+
instance.should respond_to :choice_red?
|
150
|
+
instance.should be_choice_red
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should allow for not creating methods" do
|
154
|
+
subject.attr_enumerator :choice, ['red', 'blue'], :create_methods => false
|
155
|
+
instance.should_not respond_to :choice_red?
|
156
|
+
end
|
157
|
+
|
158
|
+
it "should allow for a custom prefix" do
|
159
|
+
subject.attr_enumerator :choice, ['red', 'blue'], :prefix => 'colored'
|
160
|
+
instance.choice = 'red'
|
161
|
+
|
162
|
+
instance.should respond_to :colored_red?
|
163
|
+
instance.should be_colored_red
|
164
|
+
|
165
|
+
instance.should respond_to :colored_blue?
|
166
|
+
instance.should_not be_colored_blue
|
167
|
+
end
|
168
|
+
|
169
|
+
it "should allow for explicitly using the default prefix" do
|
170
|
+
subject.attr_enumerator :choice, ['red', 'blue'], :prefix => true
|
171
|
+
instance.should respond_to :choice_red?
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should allow for no prefix" do
|
175
|
+
subject.attr_enumerator :choice, ['red', 'blue'], :prefix => false
|
176
|
+
instance.should respond_to :red?
|
177
|
+
end
|
178
|
+
|
179
|
+
it "should not have a suffix by default" do
|
180
|
+
subject.attr_enumerator :choice, ['red', 'blue']
|
181
|
+
instance.should respond_to :choice_red?
|
182
|
+
end
|
183
|
+
|
184
|
+
it "should allow for explicitly having no suffix" do
|
185
|
+
subject.attr_enumerator :choice, ['red', 'blue'], :suffix => false
|
186
|
+
instance.should respond_to :choice_red?
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should use the field as a suffix when :suffix is true" do
|
190
|
+
subject.attr_enumerator :choice, ['red', 'blue'], :suffix => true
|
191
|
+
instance.should respond_to :choice_red_choice?
|
192
|
+
end
|
193
|
+
|
194
|
+
it "should allow for a custom suffix" do
|
195
|
+
subject.attr_enumerator :choice, ['red', 'blue'], :suffix => 'custom'
|
196
|
+
instance.should respond_to :choice_red_custom?
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
context "with ActiveRecord" do
|
202
|
+
before(:each) do
|
203
|
+
class TestModel < ActiveRecordModel
|
204
|
+
attr_accessor :choice
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should automatically be included in ActiveRecord::Base" do
|
209
|
+
ActiveRecord::Base.should respond_to :attr_enumerator
|
210
|
+
end
|
211
|
+
|
212
|
+
describe "scopes" do
|
213
|
+
it "should create a scope for each enumeration by default" do
|
214
|
+
subject.attr_enumerator :choice, ['red', 'blue']
|
215
|
+
subject.choice_red.should be_a ActiveRecord::Relation
|
216
|
+
end
|
217
|
+
|
218
|
+
it "should allow for explicitly creating scopes" do
|
219
|
+
subject.attr_enumerator :choice, ['red', 'blue'], :create_scopes => true
|
220
|
+
subject.choice_red.should be_a ActiveRecord::Relation
|
221
|
+
end
|
222
|
+
|
223
|
+
it "should allow for not creating scopes" do
|
224
|
+
subject.attr_enumerator :choice, ['red', 'blue'], :create_scopes => false
|
225
|
+
subject.should_not respond_to :choice_red
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'active_record'
|
5
|
+
require 'attr_enumerator'
|
6
|
+
|
7
|
+
# Requires supporting files with custom matchers and macros, etc,
|
8
|
+
# in ./support/ and its subdirectories.
|
9
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
10
|
+
|
11
|
+
class ActiveRecordModel < ActiveRecord::Base
|
12
|
+
establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
13
|
+
connection.execute("CREATE TABLE #{table_name} (#{primary_key} integer PRIMARY KEY AUTOINCREMENT)")
|
14
|
+
end
|
data/watchr.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
def run(cmd)
|
2
|
+
full_command = "bundle exec rspec #{cmd}"
|
3
|
+
puts(full_command)
|
4
|
+
system(full_command)
|
5
|
+
end
|
6
|
+
|
7
|
+
def run_all
|
8
|
+
run("spec")
|
9
|
+
end
|
10
|
+
|
11
|
+
watch( '^lib/(.*)\.rb' ) { |m| run("spec/%s_spec.rb" % m[1]) }
|
12
|
+
watch( '^spec/spec_helper\.rb' ) { run_all }
|
13
|
+
watch( '^spec.*/.*_spec\.rb' ) { |m| run(m[0]) }
|
metadata
ADDED
@@ -0,0 +1,199 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_enumerator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Chris Baker
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-03-25 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
name: activemodel
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 7
|
31
|
+
segments:
|
32
|
+
- 3
|
33
|
+
- 0
|
34
|
+
- 0
|
35
|
+
version: 3.0.0
|
36
|
+
requirement: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
name: activesupport
|
41
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 7
|
47
|
+
segments:
|
48
|
+
- 3
|
49
|
+
- 0
|
50
|
+
- 0
|
51
|
+
version: 3.0.0
|
52
|
+
requirement: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
name: activerecord
|
57
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 7
|
63
|
+
segments:
|
64
|
+
- 3
|
65
|
+
- 0
|
66
|
+
- 0
|
67
|
+
version: 3.0.0
|
68
|
+
requirement: *id003
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
name: jeweler
|
73
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 3
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
version: "0"
|
82
|
+
requirement: *id004
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
type: :development
|
85
|
+
prerelease: false
|
86
|
+
name: rspec
|
87
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
hash: 27
|
93
|
+
segments:
|
94
|
+
- 2
|
95
|
+
- 5
|
96
|
+
- 0
|
97
|
+
version: 2.5.0
|
98
|
+
requirement: *id005
|
99
|
+
- !ruby/object:Gem::Dependency
|
100
|
+
type: :development
|
101
|
+
prerelease: false
|
102
|
+
name: ruby-debug
|
103
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
hash: 3
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
version: "0"
|
112
|
+
requirement: *id006
|
113
|
+
- !ruby/object:Gem::Dependency
|
114
|
+
type: :development
|
115
|
+
prerelease: false
|
116
|
+
name: sqlite3
|
117
|
+
version_requirements: &id007 !ruby/object:Gem::Requirement
|
118
|
+
none: false
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
hash: 3
|
123
|
+
segments:
|
124
|
+
- 0
|
125
|
+
version: "0"
|
126
|
+
requirement: *id007
|
127
|
+
- !ruby/object:Gem::Dependency
|
128
|
+
type: :development
|
129
|
+
prerelease: false
|
130
|
+
name: watchr
|
131
|
+
version_requirements: &id008 !ruby/object:Gem::Requirement
|
132
|
+
none: false
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
hash: 3
|
137
|
+
segments:
|
138
|
+
- 0
|
139
|
+
version: "0"
|
140
|
+
requirement: *id008
|
141
|
+
description: Enumerated attributes for ActiveModel
|
142
|
+
email: baker@socialvibe.com
|
143
|
+
executables: []
|
144
|
+
|
145
|
+
extensions: []
|
146
|
+
|
147
|
+
extra_rdoc_files:
|
148
|
+
- LICENSE.txt
|
149
|
+
- README.rdoc
|
150
|
+
files:
|
151
|
+
- .document
|
152
|
+
- .rspec
|
153
|
+
- Gemfile
|
154
|
+
- LICENSE.txt
|
155
|
+
- README.rdoc
|
156
|
+
- Rakefile
|
157
|
+
- VERSION
|
158
|
+
- attr_enumerator.gemspec
|
159
|
+
- lib/attr_enumerator.rb
|
160
|
+
- spec/attr_enumerator_spec.rb
|
161
|
+
- spec/spec_helper.rb
|
162
|
+
- watchr.rb
|
163
|
+
has_rdoc: true
|
164
|
+
homepage: http://github.com/chrisb87/attr_enumerator
|
165
|
+
licenses:
|
166
|
+
- MIT
|
167
|
+
post_install_message:
|
168
|
+
rdoc_options: []
|
169
|
+
|
170
|
+
require_paths:
|
171
|
+
- lib
|
172
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
173
|
+
none: false
|
174
|
+
requirements:
|
175
|
+
- - ">="
|
176
|
+
- !ruby/object:Gem::Version
|
177
|
+
hash: 3
|
178
|
+
segments:
|
179
|
+
- 0
|
180
|
+
version: "0"
|
181
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
182
|
+
none: false
|
183
|
+
requirements:
|
184
|
+
- - ">="
|
185
|
+
- !ruby/object:Gem::Version
|
186
|
+
hash: 3
|
187
|
+
segments:
|
188
|
+
- 0
|
189
|
+
version: "0"
|
190
|
+
requirements: []
|
191
|
+
|
192
|
+
rubyforge_project:
|
193
|
+
rubygems_version: 1.3.7
|
194
|
+
signing_key:
|
195
|
+
specification_version: 3
|
196
|
+
summary: Enumerated attributes for ActiveModel
|
197
|
+
test_files:
|
198
|
+
- spec/attr_enumerator_spec.rb
|
199
|
+
- spec/spec_helper.rb
|