simple_enum 1.0.1 → 1.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  = SimpleEnum - unobtrusive enum-like fields for ActiveRecord
2
2
 
3
3
  A Rails plugin which brings easy-to-use enum-like functionality to
4
- ActiveRecord models.
4
+ ActiveRecord models (now compatible with rails 3, ruby 1.9 and jruby).
5
5
 
6
6
  *Note*: a recent search on github for `enum` turned out, that there are many, many similar solutions.
7
7
  Yet, none seem to provide so many options, but I may be biased ;)
@@ -180,6 +180,12 @@ or <em>b)</em> a custom method to convert an object to a symbolized form named +
180
180
 
181
181
  * Maybe the <tt>:whiny</tt> option should default to <tt>false</tt>, so that generally no exceptions are thrown if a user fakes a request?
182
182
  * Clean-up code and tests -> bring down LOC ;) (but maintain Code LOC vs. Test LOC ratio which is currently 1:2.9)
183
+ * Make `:slim => true` the default option...?
184
+
185
+ == Contributors
186
+
187
+ * dmitry - bugfixes and other improvements
188
+ * tarsolya - implemented all the ruby 1.9 and rails 3 goodness!
183
189
 
184
190
  == Licence & Copyright
185
- Copyright (c) 2009 by Lukas Westermann, Licenced under MIT Licence (see LICENCE file)
191
+ Copyright (c) 2009 by Lukas Westermann, Licenced under MIT Licence (see LICENCE file)
data/lib/simple_enum.rb CHANGED
@@ -8,7 +8,6 @@
8
8
  #
9
9
  # See the +as_enum+ documentation for more details.
10
10
 
11
- require 'simple_enum/array_support'
12
11
  require 'simple_enum/enum_hash'
13
12
  require 'simple_enum/object_support'
14
13
  require 'simple_enum/validation'
@@ -18,7 +17,7 @@ require 'simple_enum/validation'
18
17
  module SimpleEnum
19
18
 
20
19
  # +SimpleEnum+ version string.
21
- VERSION = "1.0.1".freeze
20
+ VERSION = "1.1.0".freeze
22
21
 
23
22
  class << self
24
23
 
@@ -211,7 +210,11 @@ module SimpleEnum
211
210
 
212
211
  # allow class access to each value
213
212
  unless options[:slim] === :class
214
- metaclass.send(:define_method, "#{prefix}#{sym}", Proc.new { |*args| args.first ? k : code })
213
+ if self.respond_to? :singleton_class
214
+ singleton_class.send(:define_method, "#{prefix}#{sym}", Proc.new { |*args| args.first ? k : code })
215
+ else
216
+ metaclass.send(:define_method, "#{prefix}#{sym}", Proc.new { |*args| args.first ? k : code })
217
+ end
215
218
  end
216
219
  end
217
220
  end
@@ -220,7 +223,7 @@ module SimpleEnum
220
223
  include Validation
221
224
 
222
225
  def human_enum_name(enum, key, options = {})
223
- defaults = self_and_descendants_from_active_record.map { |klass| :"#{klass.name.underscore}.#{enum}.#{key}" }
226
+ defaults = ([self] + subclasses).map { |klass| :"#{klass.name.underscore}.#{enum}.#{key}" }
224
227
  defaults << :"#{enum}.#{key}"
225
228
  defaults << options.delete(:default) if options[:default]
226
229
  defaults << "#{key}".humanize
@@ -240,8 +243,7 @@ end
240
243
  # Tie stuff together and load translations if ActiveRecord is defined
241
244
  if Object.const_defined?('ActiveRecord')
242
245
  Object.send(:include, SimpleEnum::ObjectSupport)
243
- Array.send(:include, SimpleEnum::ArraySupport)
244
246
 
245
247
  ActiveRecord::Base.send(:include, SimpleEnum)
246
248
  I18n.load_path << File.join(File.dirname(__FILE__), '..', 'locales', 'en.yml')
247
- end
249
+ end
@@ -5,21 +5,23 @@ module SimpleEnum
5
5
  # like access to
6
6
  #
7
7
  #
8
- class EnumHash < ::Hash
9
- def initialize(hsh)
10
- hsh = hsh.to_hash_magic unless hsh.is_a?(Hash)
8
+ class EnumHash < ::ActiveSupport::OrderedHash
9
+ def initialize(args = [])
10
+ super()
11
11
 
12
12
  @reverse_sym_lookup = {}
13
13
  @sym_value_lookup = {}
14
-
15
- hsh.each do |k,v|
16
- sym = k.to_enum_sym
17
- self[k] = v
18
- @reverse_sym_lookup[sym] = k
19
- @sym_value_lookup[sym] = v
14
+
15
+ if args.is_a?(Hash)
16
+ args.each { |k,v| set_value_for_reverse_lookup(k, v) }
17
+ else
18
+ ary = args.send(args.respond_to?(:enum_with_index) ? :enum_with_index : :each_with_index).to_a unless args.first.is_a?(ActiveRecord::Base) or args.first.is_a?(Array)
19
+ ary = args.map { |e| [e, e.id] } if args.first.is_a?(ActiveRecord::Base)
20
+ ary ||= args
21
+ ary.each { |e| set_value_for_reverse_lookup(e[0], e[1]) }
20
22
  end
21
23
  end
22
-
24
+
23
25
  def default(k = nil)
24
26
  @sym_value_lookup[k.to_enum_sym] if k
25
27
  end
@@ -32,5 +34,13 @@ module SimpleEnum
32
34
  super
33
35
  end
34
36
  end
37
+
38
+ private
39
+ def set_value_for_reverse_lookup(key, value)
40
+ sym = key.to_enum_sym
41
+ self[key] = value
42
+ @reverse_sym_lookup[sym] = key
43
+ @sym_value_lookup[sym] = value
44
+ end
35
45
  end
36
46
  end
@@ -22,16 +22,15 @@ module SimpleEnum
22
22
  # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
23
23
  # method, proc or string should return or evaluate to a true or false value.
24
24
  def validates_as_enum(*attr_names)
25
- configuration = { :on => :save }
26
- configuration.update(attr_names.extract_options!)
25
+ @configuration = { :on => :save }
26
+ @configuration.update(attr_names.extract_options!)
27
27
  attr_names.map! { |e| enum_definitions[e][:column] } # map to column name
28
-
29
- validates_each(attr_names, configuration) do |record, attr_name, value|
28
+ validates_each(attr_names) do |record, attr_name, value|
30
29
  enum_def = enum_definitions[attr_name]
31
30
  unless send(enum_def[:name].to_s.pluralize).values.include?(value)
32
- record.errors.add(enum_def[:name], :invalid_enum, :default => configuration[:message], :value => value)
31
+ record.errors.add(enum_def[:name], :invalid_enum, :default => @configuration[:message], :value => value)
33
32
  end
34
33
  end
35
- end
34
+ end
36
35
  end
37
36
  end
@@ -30,4 +30,43 @@ class EnumHashTest < ActiveSupport::TestCase
30
30
  assert_same 1, genders.female
31
31
  assert_equal male, genders.send(:male, true)
32
32
  end
33
+
34
+ test "that EnumHash keys are ordered" do
35
+ ordered = SimpleEnum::EnumHash.new [:alpha, :beta, :gamma, :delta, :epsilon, :zeta, :eta]
36
+ expected_keys = [:alpha, :beta, :gamma, :delta, :epsilon, :zeta, :eta]
37
+ assert_equal expected_keys, ordered.keys
38
+ end
39
+
40
+ test "that when simple array is merge into a EnumHash, array values are the keys and indicies are values" do
41
+ a = [:a, :b, :c, :d]
42
+ h = SimpleEnum::EnumHash.new(a)
43
+
44
+ assert_same 0, h[:a]
45
+ assert_same 1, h[:b]
46
+ assert_same 2, h[:c]
47
+ assert_same 3, h[:d]
48
+ assert_equal [:a, :b, :c, :d], h.keys
49
+ end
50
+
51
+ test "that an already 'correct' looking array is converted to a hash" do
52
+ a = [[:a, 5], [:b, 10]]
53
+ h = SimpleEnum::EnumHash.new(a)
54
+
55
+ assert_same 5, h[:a]
56
+ assert_same 10, h[:b]
57
+ assert_equal [:a, :b], h.keys
58
+ end
59
+
60
+ test "that an array of ActiveRecords are converted to <obj> => obj.id" do
61
+ reload_db :genders => true # reload db
62
+ a = Gender.find(:all, :order => :id)
63
+
64
+ male = Gender.find(0)
65
+ female = Gender.find(1)
66
+
67
+ h = SimpleEnum::EnumHash.new(a)
68
+
69
+ assert_same 0, h[male]
70
+ assert_same 1, h[female]
71
+ end
33
72
  end
@@ -52,10 +52,7 @@ class SimpleEnumTest < ActiveSupport::TestCase
52
52
  end
53
53
 
54
54
  test "add validation and test validations" do
55
- Dummy.class_eval do; validates_as_enum :gender; end
56
- #with_validation = Class.new(Dummy) do
57
- # validates_as_enum :gender
58
- #end
55
+ Dummy.class_eval { validates_as_enum :gender }
59
56
 
60
57
  d = Dummy.new :gender_cd => 5 # invalid number :)
61
58
  assert_equal(false, d.save)
data/test/test_helper.rb CHANGED
@@ -1,12 +1,10 @@
1
1
  # Setup environment for both tests and IRB interactive console
2
2
  #
3
3
 
4
- $KCODE = 'u' # to make parameterize work...
4
+ $KCODE = 'u' unless RUBY_VERSION =~ /^1\.9/ # to make parameterize work...
5
5
 
6
6
  require 'rubygems'
7
7
 
8
- gem 'sqlite3-ruby'
9
-
10
8
  require 'active_support'
11
9
  require 'active_record'
12
10
 
@@ -16,7 +14,9 @@ RAILS_ROOT = ROOT
16
14
  RAILS_ENV = 'test'
17
15
 
18
16
  # create database connection (in memory db!)
19
- ActiveRecord::Base.establish_connection({ :adapter => 'sqlite3', :database => ':memory:'})
17
+ ActiveRecord::Base.establish_connection({
18
+ :adapter => RUBY_PLATFORM =~ /java/ ? 'jdbcsqlite3' : 'sqlite3',
19
+ :database => ':memory:'})
20
20
 
21
21
  # load simple_enum
22
22
  require File.join(ROOT, 'init')
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
- - 0
8
7
  - 1
9
- version: 1.0.1
8
+ - 0
9
+ version: 1.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Lukas Westermann
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-06 00:00:00 +02:00
17
+ date: 2010-05-10 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -33,7 +33,6 @@ files:
33
33
  - Rakefile
34
34
  - init.rb
35
35
  - lib/simple_enum.rb
36
- - lib/simple_enum/array_support.rb
37
36
  - lib/simple_enum/enum_hash.rb
38
37
  - lib/simple_enum/object_support.rb
39
38
  - lib/simple_enum/validation.rb
@@ -46,7 +45,6 @@ files:
46
45
  - test/object_backed_test.rb
47
46
  - test/object_support_test.rb
48
47
  - test/prefixes_test.rb
49
- - test/se_array_support_test.rb
50
48
  - test/simple_enum_test.rb
51
49
  - test/test_helper.rb
52
50
  - test/without_shortcuts_test.rb
@@ -89,7 +87,6 @@ test_files:
89
87
  - test/object_backed_test.rb
90
88
  - test/object_support_test.rb
91
89
  - test/prefixes_test.rb
92
- - test/se_array_support_test.rb
93
90
  - test/simple_enum_test.rb
94
91
  - test/test_helper.rb
95
92
  - test/without_shortcuts_test.rb
@@ -1,15 +0,0 @@
1
- module SimpleEnum
2
- module ArraySupport
3
-
4
- # Magically convert an array to a hash, has some neat features
5
- # for active record models and similar.
6
- #
7
- # TODO: add more documentation; allow block to be passed to customize key/value pairs
8
- def to_hash_magic
9
- v = enum_with_index.to_a unless first.is_a?(ActiveRecord::Base) or first.is_a?(Array)
10
- v = map { |e| [e, e.id] } if first.is_a?(ActiveRecord::Base)
11
- v ||= self
12
- Hash[*v.flatten]
13
- end
14
- end
15
- end
@@ -1,32 +0,0 @@
1
- class SeArraySupportTest < ActiveSupport::TestCase
2
- test "that when simple array is merge to hash, array values are the keys and indicies are values" do
3
- a = [:a, :b, :c, :d]
4
- h = a.to_hash_magic
5
-
6
- assert_same 0, h[:a]
7
- assert_same 1, h[:b]
8
- assert_same 2, h[:c]
9
- assert_same 3, h[:d]
10
- end
11
-
12
- test "that an already correct looking array is converted to a hash" do
13
- a = [[:a, 5], [:b, 10]]
14
- h = a.to_hash_magic
15
-
16
- assert_same 5, h[:a]
17
- assert_same 10, h[:b]
18
- end
19
-
20
- test "that an array of ActiveRecords are converted to <obj> => obj.id" do
21
- reload_db :genders => true # reload db
22
- a = Gender.find(:all, :order => :id)
23
-
24
- male = Gender.find(0)
25
- female = Gender.find(1)
26
-
27
- h = a.to_hash_magic
28
-
29
- assert_same 0, h[male]
30
- assert_same 1, h[female]
31
- end
32
- end