simple_enum 1.0.0 → 1.0.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/.gitignore +0 -1
- data/Rakefile +8 -3
- data/lib/simple_enum.rb +14 -10
- data/test/class_methods_test.rb +12 -1
- data/test/simple_enum_test.rb +7 -1
- metadata +12 -7
- data/VERSION.yml +0 -5
- data/lib/simple_enum/version.rb +0 -14
data/.gitignore
CHANGED
data/Rakefile
CHANGED
@@ -26,15 +26,20 @@ end
|
|
26
26
|
begin
|
27
27
|
require 'jeweler'
|
28
28
|
Jeweler::Tasks.new do |gemspec|
|
29
|
+
require File.join(File.dirname(__FILE__), 'lib', 'simple_enum')
|
30
|
+
|
29
31
|
gemspec.name = "simple_enum"
|
32
|
+
gemspec.version = SimpleEnum::VERSION
|
30
33
|
gemspec.summary = "Simple enum-like field support for ActiveRecord (including validations and i18n)"
|
31
34
|
gemspec.email = "lukas.westermann@gmail.com"
|
32
35
|
gemspec.homepage = "http://github.com/lwe/simple_enum"
|
33
|
-
gemspec.authors = ["Lukas Westermann"]
|
36
|
+
gemspec.authors = ["Lukas Westermann"] # ask & add "Dmitry Polushkin"
|
37
|
+
|
38
|
+
gemspec.files.reject! { |file| file =~ /\.gemspec$/ } # kinda redundant
|
34
39
|
end
|
35
40
|
Jeweler::GemcutterTasks.new
|
36
41
|
rescue LoadError
|
37
|
-
puts "Jeweler not available. Install it with: sudo gem install
|
42
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler"
|
38
43
|
end
|
39
44
|
|
40
45
|
namespace :metrics do
|
@@ -67,4 +72,4 @@ desc 'Clean up generated files.'
|
|
67
72
|
task :clean do |t|
|
68
73
|
FileUtils.rm_rf "doc"
|
69
74
|
FileUtils.rm_rf "pkg"
|
70
|
-
end
|
75
|
+
end
|
data/lib/simple_enum.rb
CHANGED
@@ -12,12 +12,14 @@ require 'simple_enum/array_support'
|
|
12
12
|
require 'simple_enum/enum_hash'
|
13
13
|
require 'simple_enum/object_support'
|
14
14
|
require 'simple_enum/validation'
|
15
|
-
require 'simple_enum/version'
|
16
15
|
|
17
16
|
# Base module which gets included in <tt>ActiveRecord::Base</tt>. See documentation
|
18
17
|
# of +SimpleEnum::ClassMethods+ for more details.
|
19
18
|
module SimpleEnum
|
20
19
|
|
20
|
+
# +SimpleEnum+ version string.
|
21
|
+
VERSION = "1.0.1".freeze
|
22
|
+
|
21
23
|
class << self
|
22
24
|
|
23
25
|
# Provides configurability to SimpleEnum, allows to override some defaults which are
|
@@ -165,26 +167,28 @@ module SimpleEnum
|
|
165
167
|
|
166
168
|
# generate setter
|
167
169
|
define_method("#{enum_cd}=") do |new_value|
|
168
|
-
v = new_value.
|
169
|
-
raise(ArgumentError, "Invalid enumeration value: #{new_value}") if (options[:whiny] and v.nil? and !new_value.
|
170
|
+
v = new_value.blank? ? nil : values[new_value.to_sym]
|
171
|
+
raise(ArgumentError, "Invalid enumeration value: #{new_value}") if (options[:whiny] and v.nil? and !new_value.blank?)
|
170
172
|
write_attribute options[:column], v
|
171
173
|
end
|
172
174
|
|
173
175
|
# allow access to defined values hash, e.g. in a select helper or finder method.
|
174
176
|
self_name = enum_cd.to_s.pluralize
|
175
177
|
self_name.upcase! if options[:upcase]
|
176
|
-
|
178
|
+
|
177
179
|
class_eval(<<-EOM, __FILE__, __LINE__ + 1)
|
180
|
+
@#{self_name} = values
|
181
|
+
|
178
182
|
def self.#{self_name}(*args)
|
179
|
-
return
|
180
|
-
return
|
181
|
-
args.inject([]) { |ary, sym| ary <<
|
183
|
+
return @#{self_name} if args.first.nil?
|
184
|
+
return @#{self_name}[args.first] if args.size == 1
|
185
|
+
args.inject([]) { |ary, sym| ary << @#{self_name}[sym]; ary }
|
182
186
|
end
|
183
187
|
|
184
188
|
def self.#{self_name}_for_select(&block)
|
185
|
-
self.#{self_name}.map do |k,v|
|
186
|
-
[block_given? ? yield(k,v) : self.human_enum_name(#{self_name.inspect}, k),
|
187
|
-
end.sort
|
189
|
+
self.#{self_name}.map do |k,v|
|
190
|
+
[block_given? ? yield(k,v) : self.human_enum_name(#{self_name.inspect}, k), k]
|
191
|
+
end.sort
|
188
192
|
end
|
189
193
|
EOM
|
190
194
|
|
data/test/class_methods_test.rb
CHANGED
@@ -11,7 +11,12 @@ class ClassMethodsTest < ActiveSupport::TestCase
|
|
11
11
|
assert_nil Dummy.genders(:inexistent)
|
12
12
|
assert_nil Dummy.genders[:inexistent]
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
|
+
test "GENDERS constant created" do
|
16
|
+
assert_equal [0, 1], Dummy.genders.values.sort
|
17
|
+
assert_equal %w{female male}, Dummy.genders.keys.map(&:to_s).sort
|
18
|
+
end
|
19
|
+
|
15
20
|
test "that Klass.genders(:sym_a, :sym_b) returns an array of values, useful for IN clauses" do
|
16
21
|
assert_equal [0, 1], Dummy.genders(:male, :female)
|
17
22
|
assert_equal [1, 0], Dummy.genders(:female, :male)
|
@@ -103,4 +108,10 @@ class ClassMethodsTest < ActiveSupport::TestCase
|
|
103
108
|
assert_equal "Foo", Dummy.human_enum_name(:didums, :foo)
|
104
109
|
assert_equal "Foos", Dummy.human_enum_name(:didums, :foo, :count => 5)
|
105
110
|
end
|
111
|
+
|
112
|
+
test "enum_for_select class method" do
|
113
|
+
for_select = Dummy.genders_for_select
|
114
|
+
assert_equal ["Girl", :female], for_select.first
|
115
|
+
assert_equal ["Male", :male], for_select.last
|
116
|
+
end
|
106
117
|
end
|
data/test/simple_enum_test.rb
CHANGED
@@ -77,6 +77,8 @@ class SimpleEnumTest < ActiveSupport::TestCase
|
|
77
77
|
|
78
78
|
d = not_whiny.new :gender => :foo
|
79
79
|
assert_nil(d.gender)
|
80
|
+
d.gender = ''
|
81
|
+
assert_nil(d.gender)
|
80
82
|
end
|
81
83
|
|
82
84
|
test "ensure that setting to 'nil' works if :whiny => true and :whiny => false" do
|
@@ -84,6 +86,8 @@ class SimpleEnumTest < ActiveSupport::TestCase
|
|
84
86
|
assert_equal(:male, d.gender)
|
85
87
|
d.gender = nil
|
86
88
|
assert_nil(d.gender)
|
89
|
+
d.gender = ''
|
90
|
+
assert_nil(d.gender)
|
87
91
|
|
88
92
|
not_whiny_again = Class.new(Dummy) do
|
89
93
|
as_enum :gender, [:male, :female], :whiny => false
|
@@ -92,6 +96,8 @@ class SimpleEnumTest < ActiveSupport::TestCase
|
|
92
96
|
d = not_whiny_again.new :gender => :male
|
93
97
|
assert_equal(:male, d.gender)
|
94
98
|
d.gender = nil
|
95
|
-
assert_nil(d.gender)
|
99
|
+
assert_nil(d.gender)
|
100
|
+
d.gender = ''
|
101
|
+
assert_nil(d.gender)
|
96
102
|
end
|
97
103
|
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 1.0.1
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Lukas Westermann
|
@@ -9,7 +14,7 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-
|
17
|
+
date: 2010-04-06 00:00:00 +02:00
|
13
18
|
default_executable:
|
14
19
|
dependencies: []
|
15
20
|
|
@@ -26,14 +31,12 @@ files:
|
|
26
31
|
- LICENCE
|
27
32
|
- README.rdoc
|
28
33
|
- Rakefile
|
29
|
-
- VERSION.yml
|
30
34
|
- init.rb
|
31
35
|
- lib/simple_enum.rb
|
32
36
|
- lib/simple_enum/array_support.rb
|
33
37
|
- lib/simple_enum/enum_hash.rb
|
34
38
|
- lib/simple_enum/object_support.rb
|
35
39
|
- lib/simple_enum/validation.rb
|
36
|
-
- lib/simple_enum/version.rb
|
37
40
|
- locales/en.yml
|
38
41
|
- test/array_conversions_test.rb
|
39
42
|
- test/class_methods_test.rb
|
@@ -60,18 +63,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
60
63
|
requirements:
|
61
64
|
- - ">="
|
62
65
|
- !ruby/object:Gem::Version
|
66
|
+
segments:
|
67
|
+
- 0
|
63
68
|
version: "0"
|
64
|
-
version:
|
65
69
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
70
|
requirements:
|
67
71
|
- - ">="
|
68
72
|
- !ruby/object:Gem::Version
|
73
|
+
segments:
|
74
|
+
- 0
|
69
75
|
version: "0"
|
70
|
-
version:
|
71
76
|
requirements: []
|
72
77
|
|
73
78
|
rubyforge_project:
|
74
|
-
rubygems_version: 1.3.
|
79
|
+
rubygems_version: 1.3.6
|
75
80
|
signing_key:
|
76
81
|
specification_version: 3
|
77
82
|
summary: Simple enum-like field support for ActiveRecord (including validations and i18n)
|
data/VERSION.yml
DELETED
data/lib/simple_enum/version.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
module SimpleEnum
|
2
|
-
module VERSION #:nodoc:
|
3
|
-
def self.version
|
4
|
-
@VERSION_PARTS ||= YAML.load_file File.join(File.dirname(__FILE__), '..', '..', 'VERSION.yml')
|
5
|
-
end
|
6
|
-
|
7
|
-
def self.to_s
|
8
|
-
@VERSION ||= [version[:major], version[:minor], version[:patch]].join('.').freeze
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
NAME = "simple_enum".freeze
|
13
|
-
ABOUT = "#{NAME} #{VERSION}".freeze
|
14
|
-
end
|