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