simple_enum 1.6.2 → 1.6.3
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/.gitignore +3 -0
- data/.travis.yml +1 -0
- data/Gemfile.lock +26 -21
- data/README.rdoc +2 -0
- data/Rakefile +6 -0
- data/lib/simple_enum.rb +5 -3
- data/lib/simple_enum/enum_hash.rb +3 -4
- data/lib/simple_enum/version.rb +1 -1
- data/simple_enum.gemspec +1 -1
- data/test/enum_hash_test.rb +21 -15
- data/test/mongoid_test.rb +5 -3
- data/test/orm/active_record.rb +2 -0
- data/test/orm/mongoid.rb +1 -0
- data/test/simple_enum_test.rb +12 -0
- metadata +41 -28
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,41 +1,46 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
simple_enum (1.6.
|
4
|
+
simple_enum (1.6.3)
|
5
5
|
activesupport (>= 3.0.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activemodel (3.2.
|
11
|
-
activesupport (= 3.2.
|
10
|
+
activemodel (3.2.8)
|
11
|
+
activesupport (= 3.2.8)
|
12
12
|
builder (~> 3.0.0)
|
13
|
-
activerecord (3.2.
|
14
|
-
activemodel (= 3.2.
|
15
|
-
activesupport (= 3.2.
|
16
|
-
arel (~> 3.0.
|
13
|
+
activerecord (3.2.8)
|
14
|
+
activemodel (= 3.2.8)
|
15
|
+
activesupport (= 3.2.8)
|
16
|
+
arel (~> 3.0.2)
|
17
17
|
tzinfo (~> 0.3.29)
|
18
|
-
|
18
|
+
activerecord-jdbc-adapter (1.2.2)
|
19
|
+
activerecord-jdbcsqlite3-adapter (1.2.2)
|
20
|
+
activerecord-jdbc-adapter (~> 1.2.2)
|
21
|
+
jdbc-sqlite3 (~> 3.7.2)
|
22
|
+
activesupport (3.2.8)
|
19
23
|
i18n (~> 0.6)
|
20
24
|
multi_json (~> 1.0)
|
21
|
-
arel (3.0.
|
22
|
-
bson (1.
|
23
|
-
bson (1.
|
24
|
-
bson_ext (1.
|
25
|
-
bson (
|
25
|
+
arel (3.0.2)
|
26
|
+
bson (1.6.4)
|
27
|
+
bson (1.6.4-java)
|
28
|
+
bson_ext (1.6.4)
|
29
|
+
bson (~> 1.6.4)
|
26
30
|
builder (3.0.0)
|
27
31
|
i18n (0.6.0)
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
+
jdbc-sqlite3 (3.7.2)
|
33
|
+
minitest (3.3.0)
|
34
|
+
mongo (1.6.2)
|
35
|
+
bson (~> 1.6.2)
|
36
|
+
mongoid (2.4.12)
|
32
37
|
activemodel (~> 3.1)
|
33
|
-
mongo (
|
38
|
+
mongo (<= 1.6.2)
|
34
39
|
tzinfo (~> 0.3.22)
|
35
|
-
multi_json (1.
|
40
|
+
multi_json (1.3.6)
|
36
41
|
rake (0.9.2.2)
|
37
|
-
sqlite3 (1.3.
|
38
|
-
tzinfo (0.3.
|
42
|
+
sqlite3 (1.3.6)
|
43
|
+
tzinfo (0.3.33)
|
39
44
|
|
40
45
|
PLATFORMS
|
41
46
|
java
|
data/README.rdoc
CHANGED
@@ -276,6 +276,8 @@ or <em>b)</em> a custom method to convert an object to a symbolized form named +
|
|
276
276
|
* @dbalatero - rails 2.3.5 bugfix & validator fixes
|
277
277
|
* @johnthethird - feature for <tt>_for_select</tt> to return the values
|
278
278
|
* @sinsiliux - ruby 1.9 fixes and removed AR dependency
|
279
|
+
* @sled - mongoid support
|
280
|
+
* and all others: https://github.com/lwe/simple_enum/graphs/contributors thanks
|
279
281
|
|
280
282
|
== Licence & Copyright
|
281
283
|
Copyright (c) 2011 by Lukas Westermann, Licenced under MIT Licence (see LICENCE file)
|
data/Rakefile
CHANGED
data/lib/simple_enum.rb
CHANGED
@@ -37,6 +37,7 @@ module SimpleEnum
|
|
37
37
|
# <tt>false</tt> no exception is thrown and the internal value is set to <tt>nil</tt> (default is <tt>true</tt>)
|
38
38
|
# * <tt>:dirty</tt> - Boolean value which if set to <tt>true</tt> generates <tt>..._was</tt> and <tt>..._changed?</tt>
|
39
39
|
# methods for the enum, which delegate to the internal column.
|
40
|
+
# * <tt>:strings</tt> - Boolean value which if set to <tt>true</tt> defaults array values as strings instead of integers.
|
40
41
|
def default_options
|
41
42
|
@default_options ||= {
|
42
43
|
:whiny => true,
|
@@ -154,16 +155,17 @@ module SimpleEnum
|
|
154
155
|
# <tt>false</tt> no exception is thrown and the internal value is set to <tt>nil</tt> (default is <tt>true</tt>)
|
155
156
|
# * <tt>:dirty</tt> - Boolean value which if set to <tt>true</tt> generates <tt>..._was</tt> and <tt>..._changed?</tt>
|
156
157
|
# methods for the enum, which delegate to the internal column (default is <tt>false</tt>)
|
158
|
+
# * <tt>:strings</tt> - Boolean value which if set to <tt>true</tt> stores array values as strings instead of it's index.
|
157
159
|
# * <tt>:field</tt> - Also allowed as valid key, for Mongoid integration + default options, see simple_enum#27.
|
158
160
|
#
|
159
161
|
def as_enum(enum_cd, values, options = {})
|
160
162
|
options = SimpleEnum.default_options.merge({ :column => "#{enum_cd}_cd" }).merge(options)
|
161
|
-
options.assert_valid_keys(:column, :whiny, :prefix, :slim, :upcase, :dirty, :field)
|
163
|
+
options.assert_valid_keys(:column, :whiny, :prefix, :slim, :upcase, :dirty, :strings, :field)
|
162
164
|
|
163
165
|
metaclass = (class << self; self; end)
|
164
166
|
|
165
|
-
# convert array to hash
|
166
|
-
values = SimpleEnum::EnumHash.new(values)
|
167
|
+
# convert array to hash
|
168
|
+
values = SimpleEnum::EnumHash.new(values, options[:strings])
|
167
169
|
values_inverted = values.invert
|
168
170
|
|
169
171
|
# store info away
|
@@ -4,7 +4,6 @@ module SimpleEnum
|
|
4
4
|
# Works like to original +Hash+ class, but with some added value,
|
5
5
|
# like access to
|
6
6
|
#
|
7
|
-
#
|
8
7
|
class EnumHash < ::ActiveSupport::OrderedHash
|
9
8
|
|
10
9
|
# Converts an entity to a symbol, uses to_enum_sym, if possible.
|
@@ -16,7 +15,7 @@ module SimpleEnum
|
|
16
15
|
sym unless sym.blank?
|
17
16
|
end
|
18
17
|
|
19
|
-
def initialize(args = [])
|
18
|
+
def initialize(args = [], strings = false)
|
20
19
|
super()
|
21
20
|
|
22
21
|
@reverse_sym_lookup = {}
|
@@ -28,7 +27,7 @@ module SimpleEnum
|
|
28
27
|
ary = args.send(args.respond_to?(:enum_with_index) ? :enum_with_index : :each_with_index).to_a unless args.first.respond_to?(:map)
|
29
28
|
ary = args.map { |e| [e, e.id] } if args.first.respond_to?(:map) && !args.first.is_a?(Array)
|
30
29
|
ary ||= args
|
31
|
-
ary.each { |e| set_value_for_reverse_lookup(e[0], e[1]) }
|
30
|
+
ary.each { |e| set_value_for_reverse_lookup(e[0], strings ? e[0].to_s : e[1]) }
|
32
31
|
end
|
33
32
|
end
|
34
33
|
|
@@ -54,4 +53,4 @@ module SimpleEnum
|
|
54
53
|
@sym_value_lookup[sym] = value
|
55
54
|
end
|
56
55
|
end
|
57
|
-
end
|
56
|
+
end
|
data/lib/simple_enum/version.rb
CHANGED
data/simple_enum.gemspec
CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
|
|
14
14
|
|
15
15
|
s.authors = ["Lukas Westermann"]
|
16
16
|
s.email = ["lukas.westermann@gmail.com"]
|
17
|
-
s.homepage = "http://github.com/
|
17
|
+
s.homepage = "http://lwe.github.com/simple_enum/"
|
18
18
|
|
19
19
|
s.files = `git ls-files`.split("\n")
|
20
20
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
data/test/enum_hash_test.rb
CHANGED
@@ -2,10 +2,10 @@ require 'test_helper'
|
|
2
2
|
require 'simple_enum/enum_hash'
|
3
3
|
|
4
4
|
class EnumHashTest < MiniTest::Unit::TestCase
|
5
|
-
|
5
|
+
|
6
6
|
def test_create_new_enumhash_instance_from_array_of_symbols
|
7
7
|
genders = SimpleEnum::EnumHash.new [:male, :female]
|
8
|
-
|
8
|
+
|
9
9
|
assert_same 0, genders[:male]
|
10
10
|
assert_same 1, genders[:female]
|
11
11
|
assert_same 0, genders.male
|
@@ -14,54 +14,60 @@ class EnumHashTest < MiniTest::Unit::TestCase
|
|
14
14
|
|
15
15
|
def test_create_new_enumhash_instance_from_hash
|
16
16
|
status = SimpleEnum::EnumHash.new :inactive => 0, :active => 1, :archived => 99
|
17
|
-
|
17
|
+
|
18
18
|
assert_same 0, status.inactive
|
19
19
|
assert_same 1, status[:active]
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def test_create_new_enumhash_instance_from_query_results
|
23
23
|
reload_db :genders => true
|
24
24
|
genders = SimpleEnum::EnumHash.new Gender.all
|
25
|
-
|
25
|
+
|
26
26
|
assert_same 0, genders[@male]
|
27
27
|
assert_same genders[@male], genders[:male]
|
28
28
|
assert_same 1, genders.female
|
29
29
|
assert_equal @male, genders.send(:male, true)
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
def test_that_enumhash_keys_are_ordered
|
33
33
|
ordered = SimpleEnum::EnumHash.new [:alpha, :beta, :gamma, :delta, :epsilon, :zeta, :eta]
|
34
34
|
expected_keys = [:alpha, :beta, :gamma, :delta, :epsilon, :zeta, :eta]
|
35
35
|
assert_equal expected_keys, ordered.keys
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
def test_valid_key_value_association_when_simple_array_is_merged_into_enumhash
|
39
39
|
a = [:a, :b, :c, :d]
|
40
40
|
h = SimpleEnum::EnumHash.new(a)
|
41
|
-
|
41
|
+
|
42
42
|
assert_same 0, h[:a]
|
43
43
|
assert_same 1, h[:b]
|
44
44
|
assert_same 2, h[:c]
|
45
45
|
assert_same 3, h[:d]
|
46
46
|
assert_equal [:a, :b, :c, :d], h.keys
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
def test_that_an_already_correct_looking_array_is_converted_to_hash
|
50
50
|
a = [[:a, 5], [:b, 10]]
|
51
51
|
h = SimpleEnum::EnumHash.new(a)
|
52
|
-
|
52
|
+
|
53
53
|
assert_same 5, h[:a]
|
54
54
|
assert_same 10, h[:b]
|
55
55
|
assert_equal [:a, :b], h.keys
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
def test_that_an_array_of_query_results_are_converted_to_result_ids
|
59
59
|
reload_db :genders => true # reload db
|
60
60
|
a = Gender.all
|
61
|
-
|
61
|
+
|
62
62
|
h = SimpleEnum::EnumHash.new(a)
|
63
|
-
|
63
|
+
|
64
64
|
assert_same 0, h[@male]
|
65
65
|
assert_same 1, h[@female]
|
66
|
-
end
|
67
|
-
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_strings_option
|
69
|
+
h = SimpleEnum::EnumHash.new([:male, :female], true)
|
70
|
+
assert_equal "male", h[:male]
|
71
|
+
assert_equal "female", h[:female]
|
72
|
+
end
|
73
|
+
end
|
data/test/mongoid_test.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class MongoidTest < MiniTest::Unit::TestCase
|
4
|
-
@@default_options = SimpleEnum.default_options
|
5
|
-
|
6
4
|
def setup
|
7
|
-
SimpleEnum.default_options
|
5
|
+
@default_options = SimpleEnum.default_options
|
8
6
|
reload_db
|
9
7
|
end
|
10
8
|
|
9
|
+
def teardown
|
10
|
+
SimpleEnum.default_options.clear.update(@default_options)
|
11
|
+
end
|
12
|
+
|
11
13
|
def test_creates_a_field_per_default
|
12
14
|
skip('Only available in mongoid') unless mongoid?
|
13
15
|
klass = anonymous_dummy do
|
data/test/orm/active_record.rb
CHANGED
@@ -28,6 +28,7 @@ def reload_db(options = {})
|
|
28
28
|
t.column :name, :string
|
29
29
|
t.column :gender_cd, :integer
|
30
30
|
t.column :word_cd, :string, :limit => 5
|
31
|
+
t.column :role_cd, :string
|
31
32
|
t.column :other, :integer
|
32
33
|
end
|
33
34
|
|
@@ -81,6 +82,7 @@ class Dummy < ActiveRecord::Base
|
|
81
82
|
as_enum :gender, [:male, :female]
|
82
83
|
as_enum :word, { :alpha => 'alpha', :beta => 'beta', :gamma => 'gamma'}
|
83
84
|
as_enum :didum, [ :foo, :bar, :foobar ], :column => 'other'
|
85
|
+
as_enum :role, [:admin, :member, :anon], :strings => true
|
84
86
|
end
|
85
87
|
|
86
88
|
class Gender < ActiveRecord::Base
|
data/test/orm/mongoid.rb
CHANGED
@@ -71,6 +71,7 @@ class Dummy
|
|
71
71
|
as_enum :gender, [:male, :female]
|
72
72
|
as_enum :word, { :alpha => 'alpha', :beta => 'beta', :gamma => 'gamma'}
|
73
73
|
as_enum :didum, [ :foo, :bar, :foobar ], :column => 'other'
|
74
|
+
as_enum :role, [:admin, :member, :anon], :strings => true
|
74
75
|
|
75
76
|
before_save :check_typed
|
76
77
|
|
data/test/simple_enum_test.rb
CHANGED
@@ -254,4 +254,16 @@ class SimpleEnumTest < MiniTest::Unit::TestCase
|
|
254
254
|
d = Dummy.new
|
255
255
|
assert_nil(d.human_gender)
|
256
256
|
end
|
257
|
+
|
258
|
+
def test_strings_option
|
259
|
+
d = Dummy.new :role => :anon
|
260
|
+
|
261
|
+
assert_equal :anon, d.role
|
262
|
+
assert_equal 'anon', d.role_cd
|
263
|
+
|
264
|
+
d.role = :admin
|
265
|
+
|
266
|
+
assert_equal :admin, d.role
|
267
|
+
assert_equal 'admin', d.role_cd
|
268
|
+
end
|
257
269
|
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 9
|
5
|
+
prerelease:
|
5
6
|
segments:
|
6
7
|
- 1
|
7
8
|
- 6
|
8
|
-
-
|
9
|
-
version: 1.6.
|
9
|
+
- 3
|
10
|
+
version: 1.6.3
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Lukas Westermann
|
@@ -14,78 +15,87 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2012-08
|
18
|
-
default_executable:
|
18
|
+
date: 2012-09-08 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
|
22
|
-
|
21
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
22
|
+
none: false
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
+
hash: 7
|
26
27
|
segments:
|
27
28
|
- 3
|
28
29
|
- 0
|
29
30
|
- 0
|
30
31
|
version: 3.0.0
|
31
|
-
name: activesupport
|
32
|
-
requirement: *id001
|
33
32
|
prerelease: false
|
33
|
+
type: :runtime
|
34
|
+
name: activesupport
|
35
|
+
version_requirements: *id001
|
34
36
|
- !ruby/object:Gem::Dependency
|
35
|
-
|
36
|
-
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
37
39
|
requirements:
|
38
40
|
- - ">="
|
39
41
|
- !ruby/object:Gem::Version
|
42
|
+
hash: 63
|
40
43
|
segments:
|
41
44
|
- 0
|
42
45
|
- 9
|
43
46
|
- 2
|
44
47
|
version: 0.9.2
|
45
|
-
name: rake
|
46
|
-
requirement: *id002
|
47
48
|
prerelease: false
|
48
|
-
- !ruby/object:Gem::Dependency
|
49
49
|
type: :development
|
50
|
-
|
50
|
+
name: rake
|
51
|
+
version_requirements: *id002
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
51
55
|
requirements:
|
52
56
|
- - ">="
|
53
57
|
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
54
59
|
segments:
|
55
60
|
- 2
|
56
61
|
- 3
|
57
62
|
- 0
|
58
63
|
version: 2.3.0
|
59
|
-
name: minitest
|
60
|
-
requirement: *id003
|
61
64
|
prerelease: false
|
62
|
-
- !ruby/object:Gem::Dependency
|
63
65
|
type: :development
|
64
|
-
|
66
|
+
name: minitest
|
67
|
+
version_requirements: *id003
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
65
71
|
requirements:
|
66
72
|
- - ">="
|
67
73
|
- !ruby/object:Gem::Version
|
74
|
+
hash: 7
|
68
75
|
segments:
|
69
76
|
- 3
|
70
77
|
- 0
|
71
78
|
- 0
|
72
79
|
version: 3.0.0
|
73
|
-
name: activerecord
|
74
|
-
requirement: *id004
|
75
80
|
prerelease: false
|
76
|
-
- !ruby/object:Gem::Dependency
|
77
81
|
type: :development
|
78
|
-
|
82
|
+
name: activerecord
|
83
|
+
version_requirements: *id004
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
79
87
|
requirements:
|
80
88
|
- - ~>
|
81
89
|
- !ruby/object:Gem::Version
|
90
|
+
hash: 3
|
82
91
|
segments:
|
83
92
|
- 2
|
84
93
|
- 0
|
85
94
|
version: "2.0"
|
86
|
-
name: mongoid
|
87
|
-
requirement: *id005
|
88
95
|
prerelease: false
|
96
|
+
type: :development
|
97
|
+
name: mongoid
|
98
|
+
version_requirements: *id005
|
89
99
|
description: Provides enum-like fields for ActiveRecord, ActiveModel and Mongoid models.
|
90
100
|
email:
|
91
101
|
- lukas.westermann@gmail.com
|
@@ -126,8 +136,7 @@ files:
|
|
126
136
|
- test/simple_enum_test.rb
|
127
137
|
- test/test_helper.rb
|
128
138
|
- test/without_shortcuts_test.rb
|
129
|
-
|
130
|
-
homepage: http://github.com/lwe/simple_enum
|
139
|
+
homepage: http://lwe.github.com/simple_enum/
|
131
140
|
licenses:
|
132
141
|
- MIT
|
133
142
|
post_install_message:
|
@@ -136,18 +145,22 @@ rdoc_options: []
|
|
136
145
|
require_paths:
|
137
146
|
- lib
|
138
147
|
required_ruby_version: !ruby/object:Gem::Requirement
|
148
|
+
none: false
|
139
149
|
requirements:
|
140
150
|
- - ">="
|
141
151
|
- !ruby/object:Gem::Version
|
152
|
+
hash: 57
|
142
153
|
segments:
|
143
154
|
- 1
|
144
155
|
- 8
|
145
156
|
- 7
|
146
157
|
version: 1.8.7
|
147
158
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
|
+
none: false
|
148
160
|
requirements:
|
149
161
|
- - ">="
|
150
162
|
- !ruby/object:Gem::Version
|
163
|
+
hash: 23
|
151
164
|
segments:
|
152
165
|
- 1
|
153
166
|
- 3
|
@@ -156,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
156
169
|
requirements: []
|
157
170
|
|
158
171
|
rubyforge_project:
|
159
|
-
rubygems_version: 1.
|
172
|
+
rubygems_version: 1.8.24
|
160
173
|
signing_key:
|
161
174
|
specification_version: 3
|
162
175
|
summary: Simple enum-like field support for models.
|