enumerated_field 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +13 -0
- data/README.md +12 -4
- data/Rakefile +2 -0
- data/enumerated_field.gemspec +12 -4
- data/lib/enumerated_field.rb +7 -1
- data/lib/enumerated_field/version.rb +1 -1
- data/test/enumerated_field_test.rb +30 -42
- data/test/fixtures/apples.yml +12 -0
- data/test/test_helper.rb +46 -1
- metadata +108 -92
data/.travis.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
rvm:
|
2
|
+
- 1.8.7
|
3
|
+
- 1.9.2
|
4
|
+
- ree
|
5
|
+
- jruby
|
6
|
+
- ruby-head
|
7
|
+
- rbx-2.0
|
8
|
+
notifications:
|
9
|
+
# overriding this so billing@teamsporttech.com (who owns the repo) doesn't get spammed
|
10
|
+
# anyone, feel free to add yourselves
|
11
|
+
email:
|
12
|
+
- patrick.byrne@tstmedia.com
|
13
|
+
- jon.karna@tstmedia.com
|
data/README.md
CHANGED
@@ -1,4 +1,9 @@
|
|
1
|
-
EnumeratedField is a library that provides some nice methods when a
|
1
|
+
EnumeratedField is a library that provides some nice methods when a
|
2
|
+
string column is used like an enumeration, meaning there is a list of
|
3
|
+
allowable values for the string column. Typically you want the display
|
4
|
+
value as seen by the end user to differ from the stored value, allowing
|
5
|
+
you to easily change the display value at anytime without migrating
|
6
|
+
data, and this little gem helps you with that.
|
2
7
|
|
3
8
|
## Usage
|
4
9
|
|
@@ -13,8 +18,8 @@ Available options are:
|
|
13
18
|
* `:allow_blank`, whether a blank (nil, "") value passes validation.
|
14
19
|
Defaults to false.
|
15
20
|
|
16
|
-
The default validation uses ActiveModel's inclusion validations. If
|
17
|
-
class without ActiveModel use `:validate => false` to disable
|
21
|
+
The default validation uses ActiveModel's inclusion validations. If
|
22
|
+
using on a class without ActiveModel use `:validate => false` to disable
|
18
23
|
these.
|
19
24
|
|
20
25
|
## Example
|
@@ -82,7 +87,9 @@ these.
|
|
82
87
|
> Hike::TRAIL_SHT
|
83
88
|
=> :sht
|
84
89
|
|
85
|
-
These methods are all prefixed with the field name by design, which
|
90
|
+
These methods are all prefixed with the field name by design, which
|
91
|
+
allows multiple fields on a model to exist which potentially have the
|
92
|
+
same values.
|
86
93
|
|
87
94
|
## TESTS
|
88
95
|
|
@@ -92,3 +99,4 @@ daring).
|
|
92
99
|
## TODO
|
93
100
|
|
94
101
|
* Provide any support needed for defining columns on MySQL databases as enum columns instead of string columns.
|
102
|
+
|
data/Rakefile
CHANGED
data/enumerated_field.gemspec
CHANGED
@@ -2,12 +2,18 @@
|
|
2
2
|
$:.push File.expand_path("../lib", __FILE__)
|
3
3
|
require "enumerated_field/version"
|
4
4
|
|
5
|
+
AUTHORS = {
|
6
|
+
"Luke Ludwig" => "luke.ludwig@tstmedia.com",
|
7
|
+
"Patrick Byrne" => "patrick.byrne@tstmedia.com",
|
8
|
+
"Jon Karna" => "jon.karna@tstmedia.com",
|
9
|
+
}
|
10
|
+
|
5
11
|
Gem::Specification.new do |s|
|
6
12
|
s.name = "enumerated_field"
|
7
13
|
s.version = EnumeratedField::VERSION
|
8
14
|
s.platform = Gem::Platform::RUBY
|
9
|
-
s.authors =
|
10
|
-
s.email =
|
15
|
+
s.authors = AUTHORS.keys
|
16
|
+
s.email = AUTHORS.values
|
11
17
|
s.homepage = "https://github.com/tstmedia/enumerated_field"
|
12
18
|
s.summary = %q{EnumeratedField is a library that provides some nice methods when a string column is used like an enumeration.}
|
13
19
|
s.description = %q{EnumeratedField is a library that provides some nice methods when a string column is used like an enumeration, meaning there is a list of allowable values for the string column.}
|
@@ -19,10 +25,12 @@ Gem::Specification.new do |s|
|
|
19
25
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
26
|
s.require_paths = ["lib"]
|
21
27
|
|
22
|
-
s.add_dependency
|
23
|
-
|
28
|
+
s.add_dependency 'activemodel'
|
29
|
+
s.add_dependency 'activesupport'
|
24
30
|
s.add_development_dependency 'rake'
|
25
31
|
s.add_development_dependency 'bundler'
|
26
32
|
s.add_development_dependency 'turn'
|
27
33
|
s.add_development_dependency 'shoulda'
|
34
|
+
s.add_development_dependency 'activerecord'
|
35
|
+
s.add_development_dependency 'sqlite3'
|
28
36
|
end
|
data/lib/enumerated_field.rb
CHANGED
@@ -25,7 +25,7 @@ module EnumeratedField
|
|
25
25
|
# returns the values_array for this field, useful for providing to
|
26
26
|
# options_for_select when constructing forms
|
27
27
|
enumerated_class = class << self; self; end
|
28
|
-
enumerated_class.
|
28
|
+
enumerated_class.class_eval do
|
29
29
|
define_method("#{field_name}_values") do |*options|
|
30
30
|
options = options.first || {}
|
31
31
|
if options[:first_option]
|
@@ -81,6 +81,12 @@ module EnumeratedField
|
|
81
81
|
define_method("#{field_name}_#{key}?") { send(field_name).to_s == key.to_s }
|
82
82
|
end
|
83
83
|
|
84
|
+
if defined? ActiveRecord::Base and ancestors.include? ActiveRecord::Base
|
85
|
+
values_hash.keys.each do |key|
|
86
|
+
scope "#{field_name}_#{key}", where(field_name => key)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
84
90
|
end
|
85
91
|
end
|
86
92
|
|
@@ -1,43 +1,5 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/test_helper'
|
2
2
|
|
3
|
-
class Apple
|
4
|
-
include EnumeratedField
|
5
|
-
|
6
|
-
attr_accessor :color, :kind
|
7
|
-
|
8
|
-
enum_field :color, [['Red', :red], ['Green', :green]], :validate => false
|
9
|
-
enum_field :kind, [['Fuji Apple', :fuji], ['Delicious Red Apple', :delicious]], :validate => false
|
10
|
-
|
11
|
-
def initialize(color, kind)
|
12
|
-
self.color = color
|
13
|
-
self.kind = kind
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
class Banana
|
19
|
-
include EnumeratedField
|
20
|
-
include ActiveModel::Validations
|
21
|
-
|
22
|
-
attr_accessor :brand
|
23
|
-
attr_accessor :color
|
24
|
-
attr_accessor :tastiness
|
25
|
-
|
26
|
-
enum_field :brand, [["Chiquita", :chiquita], ["Del Monte", :delmonte]]
|
27
|
-
enum_field :color, [["Awesome Yellow", :yellow], ["Icky Green", :green]], :allow_nil => true
|
28
|
-
# stressing the constantizing of the keys
|
29
|
-
enum_field :tastiness, [
|
30
|
-
["Great", "great!"],
|
31
|
-
["Good", "it's good"],
|
32
|
-
["Bad", "hate-hate"],
|
33
|
-
], :validate => false
|
34
|
-
|
35
|
-
def initialize(brand, color)
|
36
|
-
self.brand = brand
|
37
|
-
self.color = color
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
3
|
class EnumeratedFieldTest < Test::Unit::TestCase
|
42
4
|
|
43
5
|
context 'EnumeratedField class' do
|
@@ -64,13 +26,14 @@ class EnumeratedFieldTest < Test::Unit::TestCase
|
|
64
26
|
assert_equal "it's good", Banana::TASTINESS_IT_S_GOOD
|
65
27
|
assert_equal "hate-hate", Banana::TASTINESS_HATE_HATE
|
66
28
|
end
|
29
|
+
|
67
30
|
end
|
68
31
|
|
69
32
|
context 'EnumeratedField instance' do
|
70
33
|
|
71
34
|
setup do
|
72
|
-
@red_apple = Apple.new(:red, :fuji)
|
73
|
-
@green_apple = Apple.new(:green, :delicious)
|
35
|
+
@red_apple = Apple.new(:color => :red, :kind => :fuji)
|
36
|
+
@green_apple = Apple.new(:color => :green, :kind => :delicious)
|
74
37
|
end
|
75
38
|
|
76
39
|
should 'have color_display method' do
|
@@ -125,13 +88,38 @@ class EnumeratedFieldTest < Test::Unit::TestCase
|
|
125
88
|
|
126
89
|
should 'not occur if passed :validate => false' do
|
127
90
|
# no validations, accepts any choice
|
128
|
-
apple = Apple.new(:orange, :macintosh)
|
91
|
+
apple = Apple.new(:color => :orange, :kind => :macintosh)
|
129
92
|
assert !apple.respond_to?(:valid)
|
130
93
|
end
|
131
94
|
|
132
95
|
should 'accept valid string equivalent to symbol in list' do
|
133
96
|
banana = Banana.new('chiquita', :green)
|
134
|
-
assert banana.valid?, banana.errors[:brand][0]
|
97
|
+
assert banana.valid?, banana.errors[:brand][0].to_s
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'ActiveRecord' do
|
102
|
+
context 'instance' do
|
103
|
+
subject { Apple.new }
|
104
|
+
|
105
|
+
should have_db_column :color
|
106
|
+
should have_db_column :kind
|
107
|
+
end
|
108
|
+
|
109
|
+
context 'class' do
|
110
|
+
subject { Apple }
|
111
|
+
|
112
|
+
should 'have scopes for each enumerated value' do
|
113
|
+
assert_equal 4, subject.count
|
114
|
+
['color', 'kind'].each do |column|
|
115
|
+
subject.send("#{column}_values").each do |a,b|
|
116
|
+
assert subject.respond_to? "#{column}_#{b}"
|
117
|
+
assert subject.send("#{column}_#{b}").any?
|
118
|
+
assert_equal 2, subject.send("#{column}_#{b}").size
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
135
122
|
end
|
136
123
|
end
|
124
|
+
|
137
125
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,4 +1,49 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'turn'
|
3
|
+
require 'active_record'
|
4
|
+
require 'active_record/fixtures'
|
5
|
+
require 'yaml'
|
6
|
+
require 'sqlite3'
|
7
|
+
require 'enumerated_field'
|
3
8
|
require 'shoulda'
|
4
|
-
require '
|
9
|
+
require 'shoulda/active_record'
|
10
|
+
|
11
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
|
12
|
+
ActiveRecord::Schema.define do
|
13
|
+
create_table :apples, :force => true do |t|
|
14
|
+
t.string :color
|
15
|
+
t.string :kind
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Apple < ActiveRecord::Base
|
20
|
+
include EnumeratedField
|
21
|
+
|
22
|
+
enum_field :color, [['Red', :red], ['Green', :green]], :validate => false
|
23
|
+
enum_field :kind, [['Fuji Apple', :fuji], ['Delicious Red Apple', :delicious]], :validate => false
|
24
|
+
end
|
25
|
+
|
26
|
+
class Banana
|
27
|
+
include EnumeratedField
|
28
|
+
include ActiveModel::Validations
|
29
|
+
|
30
|
+
attr_accessor :brand
|
31
|
+
attr_accessor :color
|
32
|
+
attr_accessor :tastiness
|
33
|
+
|
34
|
+
enum_field :brand, [["Chiquita", :chiquita], ["Del Monte", :delmonte]]
|
35
|
+
enum_field :color, [["Awesome Yellow", :yellow], ["Icky Green", :green]], :allow_nil => true
|
36
|
+
# stressing the constantizing of the keys
|
37
|
+
enum_field :tastiness, [
|
38
|
+
["Great", "great!"],
|
39
|
+
["Good", "it's good"],
|
40
|
+
["Bad", "hate-hate"],
|
41
|
+
], :validate => false
|
42
|
+
|
43
|
+
def initialize(brand, color)
|
44
|
+
self.brand = brand
|
45
|
+
self.color = color
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
Fixtures.create_fixtures 'test/fixtures', :apples
|
metadata
CHANGED
@@ -1,104 +1,120 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerated_field
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 0
|
10
|
-
version: 0.1.0
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Luke Ludwig
|
9
|
+
- Patrick Byrne
|
10
|
+
- Jon Karna
|
14
11
|
autorequire:
|
15
12
|
bindir: bin
|
16
13
|
cert_chain: []
|
17
|
-
|
18
|
-
date: 2011-07-27 00:00:00 -05:00
|
14
|
+
date: 2011-08-30 00:00:00.000000000 -05:00
|
19
15
|
default_executable:
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
22
|
-
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
-
none: false
|
24
|
-
requirements:
|
25
|
-
- - ">="
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
hash: 3
|
28
|
-
segments:
|
29
|
-
- 0
|
30
|
-
version: "0"
|
16
|
+
dependencies:
|
17
|
+
- !ruby/object:Gem::Dependency
|
31
18
|
name: activemodel
|
19
|
+
requirement: &2157839460 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ! '>='
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: '0'
|
32
25
|
type: :runtime
|
33
26
|
prerelease: false
|
34
|
-
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
|
27
|
+
version_requirements: *2157839460
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: activesupport
|
30
|
+
requirement: &2157838800 !ruby/object:Gem::Requirement
|
37
31
|
none: false
|
38
|
-
requirements:
|
39
|
-
- -
|
40
|
-
- !ruby/object:Gem::Version
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
32
|
+
requirements:
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
type: :runtime
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: *2157838800
|
39
|
+
- !ruby/object:Gem::Dependency
|
45
40
|
name: rake
|
41
|
+
requirement: &2157838060 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
46
47
|
type: :development
|
47
48
|
prerelease: false
|
48
|
-
|
49
|
-
- !ruby/object:Gem::Dependency
|
50
|
-
version_requirements: &id003 !ruby/object:Gem::Requirement
|
51
|
-
none: false
|
52
|
-
requirements:
|
53
|
-
- - ">="
|
54
|
-
- !ruby/object:Gem::Version
|
55
|
-
hash: 3
|
56
|
-
segments:
|
57
|
-
- 0
|
58
|
-
version: "0"
|
49
|
+
version_requirements: *2157838060
|
50
|
+
- !ruby/object:Gem::Dependency
|
59
51
|
name: bundler
|
52
|
+
requirement: &2157837460 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
60
58
|
type: :development
|
61
59
|
prerelease: false
|
62
|
-
|
63
|
-
- !ruby/object:Gem::Dependency
|
64
|
-
version_requirements: &id004 !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
|
-
requirements:
|
67
|
-
- - ">="
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
hash: 3
|
70
|
-
segments:
|
71
|
-
- 0
|
72
|
-
version: "0"
|
60
|
+
version_requirements: *2157837460
|
61
|
+
- !ruby/object:Gem::Dependency
|
73
62
|
name: turn
|
63
|
+
requirement: &2157836860 !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
74
69
|
type: :development
|
75
70
|
prerelease: false
|
76
|
-
|
77
|
-
- !ruby/object:Gem::Dependency
|
78
|
-
version_requirements: &id005 !ruby/object:Gem::Requirement
|
79
|
-
none: false
|
80
|
-
requirements:
|
81
|
-
- - ">="
|
82
|
-
- !ruby/object:Gem::Version
|
83
|
-
hash: 3
|
84
|
-
segments:
|
85
|
-
- 0
|
86
|
-
version: "0"
|
71
|
+
version_requirements: *2157836860
|
72
|
+
- !ruby/object:Gem::Dependency
|
87
73
|
name: shoulda
|
74
|
+
requirement: &2157836200 !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
type: :development
|
81
|
+
prerelease: false
|
82
|
+
version_requirements: *2157836200
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activerecord
|
85
|
+
requirement: &2157835540 !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: *2157835540
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: sqlite3
|
96
|
+
requirement: &2157834860 !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
88
102
|
type: :development
|
89
103
|
prerelease: false
|
90
|
-
|
91
|
-
description: EnumeratedField is a library that provides some nice methods when a string
|
92
|
-
|
104
|
+
version_requirements: *2157834860
|
105
|
+
description: EnumeratedField is a library that provides some nice methods when a string
|
106
|
+
column is used like an enumeration, meaning there is a list of allowable values
|
107
|
+
for the string column.
|
108
|
+
email:
|
93
109
|
- luke.ludwig@tstmedia.com
|
110
|
+
- patrick.byrne@tstmedia.com
|
111
|
+
- jon.karna@tstmedia.com
|
94
112
|
executables: []
|
95
|
-
|
96
113
|
extensions: []
|
97
|
-
|
98
114
|
extra_rdoc_files: []
|
99
|
-
|
100
|
-
files:
|
115
|
+
files:
|
101
116
|
- .gitignore
|
117
|
+
- .travis.yml
|
102
118
|
- Gemfile
|
103
119
|
- README.md
|
104
120
|
- Rakefile
|
@@ -106,41 +122,41 @@ files:
|
|
106
122
|
- lib/enumerated_field.rb
|
107
123
|
- lib/enumerated_field/version.rb
|
108
124
|
- test/enumerated_field_test.rb
|
125
|
+
- test/fixtures/apples.yml
|
109
126
|
- test/test_helper.rb
|
110
127
|
has_rdoc: true
|
111
128
|
homepage: https://github.com/tstmedia/enumerated_field
|
112
129
|
licenses: []
|
113
|
-
|
114
130
|
post_install_message:
|
115
131
|
rdoc_options: []
|
116
|
-
|
117
|
-
require_paths:
|
132
|
+
require_paths:
|
118
133
|
- lib
|
119
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
135
|
none: false
|
121
|
-
requirements:
|
122
|
-
- -
|
123
|
-
- !ruby/object:Gem::Version
|
124
|
-
|
125
|
-
segments:
|
136
|
+
requirements:
|
137
|
+
- - ! '>='
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
segments:
|
126
141
|
- 0
|
127
|
-
|
128
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
|
+
hash: 1613582628289302536
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
144
|
none: false
|
130
|
-
requirements:
|
131
|
-
- -
|
132
|
-
- !ruby/object:Gem::Version
|
133
|
-
|
134
|
-
segments:
|
145
|
+
requirements:
|
146
|
+
- - ! '>='
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
segments:
|
135
150
|
- 0
|
136
|
-
|
151
|
+
hash: 1613582628289302536
|
137
152
|
requirements: []
|
138
|
-
|
139
153
|
rubyforge_project: enumerated_field
|
140
154
|
rubygems_version: 1.6.2
|
141
155
|
signing_key:
|
142
156
|
specification_version: 3
|
143
|
-
summary: EnumeratedField is a library that provides some nice methods when a string
|
144
|
-
|
157
|
+
summary: EnumeratedField is a library that provides some nice methods when a string
|
158
|
+
column is used like an enumeration.
|
159
|
+
test_files:
|
145
160
|
- test/enumerated_field_test.rb
|
161
|
+
- test/fixtures/apples.yml
|
146
162
|
- test/test_helper.rb
|