markcatley-enum_field 0.1.1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +32 -0
- data/VERSION.yml +4 -0
- data/lib/enum_field.rb +47 -0
- data/test/enum_field_test.rb +104 -0
- metadata +57 -0
data/README.rdoc
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
= EnumField
|
2
|
+
|
3
|
+
Macro to emulate a MySQL enum_field type thing.
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
This plugin encapsulates a validates_inclusion_of and automatically gives you a
|
8
|
+
few more goodies automatically. That's it!
|
9
|
+
|
10
|
+
class Computer < ActiveRecord:Base
|
11
|
+
enum_field :status, ['on', 'off', 'standby', 'sleep', 'out of this world']
|
12
|
+
|
13
|
+
# Optionally with a message to replace the default one
|
14
|
+
# enum_field :status, ['on', 'off', 'standby', 'sleep', 'out of this world'], :message => "incorrect status"
|
15
|
+
|
16
|
+
#...
|
17
|
+
end
|
18
|
+
|
19
|
+
This will give you a few things:
|
20
|
+
|
21
|
+
- add a validates_inclusion_of with a simple error message ("invalid #{field}") or your custom message
|
22
|
+
- define the following query methods, in the name of expressive code:
|
23
|
+
- on?
|
24
|
+
- off?
|
25
|
+
- standby?
|
26
|
+
- sleep?
|
27
|
+
- out_of_this_world?
|
28
|
+
- define the STATUSES constant, which contains the acceptable values
|
29
|
+
|
30
|
+
= License
|
31
|
+
|
32
|
+
Copyright (c) 2008 James Golick, released under the MIT license
|
data/VERSION.yml
ADDED
data/lib/enum_field.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
module EnumField
|
2
|
+
def self.included(klass)
|
3
|
+
klass.class_eval { extend EnumField::ClassMethods }
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
# enum_field encapsulates a validates_inclusion_of and automatically gives you a
|
8
|
+
# few more goodies automatically.
|
9
|
+
#
|
10
|
+
# class Computer < ActiveRecord:Base
|
11
|
+
# enum_field :status, ['on', 'off', 'standby', 'sleep', 'out of this world']
|
12
|
+
#
|
13
|
+
# # Optionally with a message to replace the default one
|
14
|
+
# # enum_field :status, ['on', 'off', 'standby', 'sleep'], :message => "incorrect status"
|
15
|
+
#
|
16
|
+
# #...
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# This will give you a few things:
|
20
|
+
#
|
21
|
+
# - add a validates_inclusion_of with a simple error message ("invalid #{field}") or your custom message
|
22
|
+
# - define the following query methods, in the name of expressive code:
|
23
|
+
# - on?
|
24
|
+
# - off?
|
25
|
+
# - standby?
|
26
|
+
# - sleep?
|
27
|
+
# - out_of_this_world?
|
28
|
+
# - define the STATUSES constant, which contains the acceptable values
|
29
|
+
def enum_field(field, possible_values, options={})
|
30
|
+
message = options[:message] || "invalid #{field}"
|
31
|
+
const_set field.to_s.pluralize.upcase, possible_values unless const_defined?(field.to_s.pluralize.upcase)
|
32
|
+
|
33
|
+
possible_values.each do |current_value|
|
34
|
+
method_name = current_value.downcase.gsub(/[-\s]/, '_')
|
35
|
+
define_method("#{method_name}?") do
|
36
|
+
self.send(field) == current_value
|
37
|
+
end
|
38
|
+
|
39
|
+
if respond_to? :named_scope
|
40
|
+
named_scope method_name, :conditions => {field => current_value}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
validates_inclusion_of field, :in => possible_values, :message => message
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
$LOAD_PATH.reject! { |path| path.include?('TextMate') }
|
2
|
+
require 'test/unit'
|
3
|
+
require 'rubygems'
|
4
|
+
require 'active_support'
|
5
|
+
require 'mocha'
|
6
|
+
require 'shoulda'
|
7
|
+
require 'active_record'
|
8
|
+
require File.dirname(__FILE__)+'/../lib/enum_field'
|
9
|
+
require File.dirname(__FILE__)+'/../init'
|
10
|
+
|
11
|
+
class MockedModel; include EnumField; end;
|
12
|
+
|
13
|
+
class EnumFieldTest < Test::Unit::TestCase
|
14
|
+
context "with a simple gender enum" do
|
15
|
+
setup do
|
16
|
+
@possible_values = %w( male female )
|
17
|
+
MockedModel.expects(:validates_inclusion_of).with(:gender, :in => @possible_values, :message => "invalid gender")
|
18
|
+
MockedModel.send(:enum_field, :gender, @possible_values)
|
19
|
+
end
|
20
|
+
|
21
|
+
should "create constant with possible values named as pluralized field" do
|
22
|
+
assert_equal @possible_values, MockedModel::GENDERS
|
23
|
+
end
|
24
|
+
|
25
|
+
should "create query methods for each enum type" do
|
26
|
+
model = MockedModel.new
|
27
|
+
|
28
|
+
model.stubs(:gender).returns("male")
|
29
|
+
assert model.male?
|
30
|
+
assert !model.female?
|
31
|
+
model.stubs(:gender).returns("female")
|
32
|
+
assert !model.male?
|
33
|
+
assert model.female?
|
34
|
+
end
|
35
|
+
|
36
|
+
should "extend active record base with method" do
|
37
|
+
assert_respond_to ActiveRecord::Base, :enum_field
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "Specifying a message" do
|
42
|
+
setup do
|
43
|
+
@possible_values = %w(on off)
|
44
|
+
MockedModel.expects(:validates_inclusion_of).with(:status, :in => @possible_values, :message => "custom insult")
|
45
|
+
end
|
46
|
+
|
47
|
+
should "override the default message" do
|
48
|
+
MockedModel.send(:enum_field, :status, @possible_values, :message => 'custom insult')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "With an enum containing multiple word choices" do
|
53
|
+
setup do
|
54
|
+
MockedModel.stubs(:validates_inclusion_of)
|
55
|
+
MockedModel.send :enum_field, :field, ['choice one', 'choice-two', 'other']
|
56
|
+
@model = MockedModel.new
|
57
|
+
end
|
58
|
+
|
59
|
+
should "define an underscored query method for the multiple word choice" do
|
60
|
+
assert_respond_to @model, :choice_one?
|
61
|
+
end
|
62
|
+
|
63
|
+
should "define an underscored query method for the dasherized choice" do
|
64
|
+
assert_respond_to @model, :choice_two?
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "With an enum containing mixed case choices" do
|
69
|
+
setup do
|
70
|
+
MockedModel.stubs(:validates_inclusion_of)
|
71
|
+
MockedModel.send :enum_field, :field, ['Choice One', 'ChoiceTwo', 'Other']
|
72
|
+
@model = MockedModel.new
|
73
|
+
end
|
74
|
+
|
75
|
+
should "define a lowercase, underscored query method for the multiple word choice" do
|
76
|
+
assert_respond_to @model, :choice_one?
|
77
|
+
end
|
78
|
+
|
79
|
+
should "define a lowercase query method for the camelcase choice" do
|
80
|
+
assert_respond_to @model, :choicetwo?
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context "With an enum containing strange characters" do
|
85
|
+
setup do
|
86
|
+
MockedModel.stubs(:validates_inclusion_of)
|
87
|
+
MockedModel.send :enum_field, :field, ['choice%one', 'choice☺two', 'other.']
|
88
|
+
@model = MockedModel.new
|
89
|
+
end
|
90
|
+
|
91
|
+
should "define a normal query method for the unicode choice" do
|
92
|
+
assert_respond_to @model, :choice_two?
|
93
|
+
end
|
94
|
+
|
95
|
+
should "define a normal query method for the % choice" do
|
96
|
+
assert_respond_to @model, :choice_one?
|
97
|
+
end
|
98
|
+
|
99
|
+
should "define a query method without the trailing punctuation for the other choice" do
|
100
|
+
assert_respond_to @model, :other?
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: markcatley-enum_field
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Golick
|
8
|
+
- Mathieu Martin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-03-01 00:00:00 -08:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: enum_field encapsulates a bunch of common idioms around ActiveRecord validates_inclusion_of
|
18
|
+
email: james@giraffesoft.ca
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- README.rdoc
|
27
|
+
- VERSION.yml
|
28
|
+
- lib/enum_field.rb
|
29
|
+
- test/enum_field_test.rb
|
30
|
+
has_rdoc: false
|
31
|
+
homepage: http://github.com/giraffesoft/enum_field
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: "0"
|
42
|
+
version:
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
requirements: []
|
50
|
+
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.2.0
|
53
|
+
signing_key:
|
54
|
+
specification_version: 2
|
55
|
+
summary: ActiveRecord enum fields on steroid
|
56
|
+
test_files: []
|
57
|
+
|