enumerated_field 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README +37 -0
- data/Rakefile +2 -0
- data/enumerated_field.gemspec +21 -0
- data/lib/enumerated_field.rb +56 -0
- data/lib/enumerated_field/version.rb +3 -0
- data/test/enumerated_field_test.rb +54 -0
- data/test/test_helper.rb +2 -0
- metadata +77 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
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. Typically you want the display value as seen by the end user to differ from the stored value, allowing you to easily change the display value at anytime without migrating data, and this little gem helps you with that.
|
2
|
+
|
3
|
+
Example Usage:
|
4
|
+
This example demonstrates usage within an ActiveRecord::Base object, however this gem does not depend on ActiveRecord.
|
5
|
+
|
6
|
+
class Hike < ActiveRecord::Base
|
7
|
+
|
8
|
+
include EnumeratedField
|
9
|
+
|
10
|
+
enum_field :trail, [['Pacific Crest Trail', 'pct'],
|
11
|
+
['Continental Divide Trail', 'cdt'],
|
12
|
+
['Superior Hiking Trail', 'sht']]
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
> hike = Hike.create(:trail => 'pct')
|
17
|
+
|
18
|
+
> hike.trail_sht?
|
19
|
+
=> false
|
20
|
+
|
21
|
+
> hike.trail_pct?
|
22
|
+
=> true
|
23
|
+
|
24
|
+
> hike.trail_display
|
25
|
+
=> "Pacific Crest Trail"
|
26
|
+
|
27
|
+
> hike.trail_values # useful to provide to options_for_select when constructing forms
|
28
|
+
=> [['Pacific Crest Trail', 'pct'], ['Continental Divide Trail', 'cdt'], ['Superior Hiking Trail', 'sht']]
|
29
|
+
|
30
|
+
|
31
|
+
These methods are all prefixed with the field name by design, which allows multiple fields on a model to exist which potentially have the same values.
|
32
|
+
|
33
|
+
Run tests by: ruby test/enumerated_field_test.rb
|
34
|
+
|
35
|
+
TODO:
|
36
|
+
* Provide option to enum_field to setup validation methods that validate the value of the field against the allowed values.
|
37
|
+
* Provide any support needed for defining columns on MySQL databases as enum columns instead of string columns.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "enumerated_field/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "enumerated_field"
|
7
|
+
s.version = EnumeratedField::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Luke Ludwig"]
|
10
|
+
s.email = ["luke.ludwig@tstmedia.com"]
|
11
|
+
s.homepage = "https://github.com/tstmedia/enumerated_field"
|
12
|
+
s.summary = %q{EnumeratedField is a library that provides some nice methods when a string column is used like an enumeration.}
|
13
|
+
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.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "enumerated_field"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module EnumeratedField
|
2
|
+
|
3
|
+
def self.included(klass)
|
4
|
+
klass.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
# ex. enum_field(:league, [['National Football League', :nfl], ['Major League Baseball', :mlb]])
|
10
|
+
# field_name typically corresponds to the database column name
|
11
|
+
# values_array is a double array (not a hash to preserve order for when order matters.. ie select options)
|
12
|
+
def enum_field(field_name, values_array)
|
13
|
+
values_hash = {}
|
14
|
+
values_array.each { |value, key| values_hash[key] = value }
|
15
|
+
|
16
|
+
class_eval do
|
17
|
+
|
18
|
+
# returns the values_array for this field, useful for providing to
|
19
|
+
# options_for_select when constructing forms
|
20
|
+
define_method("#{field_name}_values") do |*options|
|
21
|
+
options = options.first || {}
|
22
|
+
if options[:first_option]
|
23
|
+
[[options[:first_option], '']] + values_array
|
24
|
+
else
|
25
|
+
values_array
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# returns display value for the current value of the field
|
30
|
+
define_method("#{field_name}_display") do
|
31
|
+
values_hash[send(field_name)]
|
32
|
+
end
|
33
|
+
|
34
|
+
# returns display value for the given value of the field
|
35
|
+
define_method("#{field_name}_display_for") do |key|
|
36
|
+
values_hash[key]
|
37
|
+
end
|
38
|
+
|
39
|
+
define_method("#{field_name}_value_for") do |key|
|
40
|
+
values_hash.invert[key]
|
41
|
+
end
|
42
|
+
|
43
|
+
# defines question methods for each possible value of the field
|
44
|
+
# ex. object.league_nfl? which returns true if the objects league
|
45
|
+
# field is currently set to nfl otherwise false
|
46
|
+
values_hash.keys.each do |key|
|
47
|
+
define_method("#{field_name}_#{key}?") { send(field_name) == key }
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class Apple
|
4
|
+
include EnumeratedField
|
5
|
+
|
6
|
+
attr_accessor :color, :kind
|
7
|
+
|
8
|
+
enum_field :color, [['Red', :red], ['Green', :green]]
|
9
|
+
enum_field :kind, [['Fuji Apple', :fuji], ['Delicious Red Apple', :delicious]]
|
10
|
+
|
11
|
+
def initialize(color, kind)
|
12
|
+
self.color = color
|
13
|
+
self.kind = kind
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
class EnumeratedFieldTest < Test::Unit::TestCase
|
19
|
+
|
20
|
+
def test_color_display
|
21
|
+
apple = Apple.new(:red, :fuji)
|
22
|
+
assert apple.color_display, 'Red'
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_color_display_for
|
26
|
+
apple = Apple.new(:red, :fuji)
|
27
|
+
assert apple.color_display_for(:green), 'Green'
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_two_enum_fields_in_one_class
|
31
|
+
apple = Apple.new(:green, :delicious)
|
32
|
+
assert apple.color_display, 'Green'
|
33
|
+
assert apple.kind_display, 'Delicious Red Apple'
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_question_methods
|
37
|
+
apple = Apple.new(:green, :delicious)
|
38
|
+
assert apple.color_green?
|
39
|
+
assert !apple.color_red?
|
40
|
+
assert apple.kind_delicious?
|
41
|
+
assert !apple.kind_fuji?
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_values_without_first_option
|
45
|
+
apple = Apple.new(:red, :fuji)
|
46
|
+
assert apple.color_values.length, 2
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_values_with_first_option
|
50
|
+
apple = Apple.new(:red, :fuji)
|
51
|
+
assert apple.color_values(:first_option => "Select Color").length, 3
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enumerated_field
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Luke Ludwig
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-21 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: 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.
|
23
|
+
email:
|
24
|
+
- luke.ludwig@tstmedia.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- README
|
35
|
+
- Rakefile
|
36
|
+
- enumerated_field.gemspec
|
37
|
+
- lib/enumerated_field.rb
|
38
|
+
- lib/enumerated_field/version.rb
|
39
|
+
- test/enumerated_field_test.rb
|
40
|
+
- test/test_helper.rb
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: https://github.com/tstmedia/enumerated_field
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !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"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project: enumerated_field
|
71
|
+
rubygems_version: 1.6.2
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: EnumeratedField is a library that provides some nice methods when a string column is used like an enumeration.
|
75
|
+
test_files:
|
76
|
+
- test/enumerated_field_test.rb
|
77
|
+
- test/test_helper.rb
|