classy_enum 3.1.0 → 3.1.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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # ClassyEnum Changelog
2
2
 
3
+ ## 3.1.1
4
+
5
+ * Fixes a regression with Formtastic support. ClassyEnumm::Base.build now
6
+ returns a null object that decends from the base_class when the argument is
7
+ blank (nil, empty string, etc). This allows the ActiveRecord model's enum
8
+ attribute to respond to enum methods even if it is blank.
9
+
3
10
  ## 3.1.0
4
11
 
5
12
  * ClassyEnum::Base now extends Enumerable to provide enum collection
@@ -13,6 +13,8 @@ module ClassyEnum
13
13
 
14
14
  class << self
15
15
  def inherited(klass)
16
+ return if klass.anonymous?
17
+
16
18
  if self == ClassyEnum::Base
17
19
  klass.base_class = klass
18
20
  else
@@ -59,7 +61,15 @@ module ClassyEnum
59
61
  def build(value, options={})
60
62
  object = find(value)
61
63
 
62
- return value if object.nil? || (options[:allow_blank] && object.nil?)
64
+ if object.nil? || (options[:allow_blank] && object.nil?)
65
+ return value unless value.blank?
66
+
67
+ # Subclass the base class and make it behave like the value that it is
68
+ object = Class.new(base_class) {
69
+ instance_variable_set(:@option, value)
70
+ delegate :blank?, :nil?, :to => :option
71
+ }.new
72
+ end
63
73
 
64
74
  object.owner = options[:owner]
65
75
  object.serialize_as_json = options[:serialize_as_json]
@@ -35,7 +35,7 @@ module ClassyEnum
35
35
  # @priority = Priority::Low.new
36
36
  # @priority.to_s # => 'low'
37
37
  def to_s
38
- self.class.instance_variable_get('@option').to_s
38
+ option.to_s
39
39
  end
40
40
 
41
41
  # Returns a Symbol corresponding to a string representation of element,
@@ -65,5 +65,11 @@ module ClassyEnum
65
65
  json
66
66
  end
67
67
 
68
+ private
69
+
70
+ def option
71
+ self.class.instance_variable_get(:@option)
72
+ end
73
+
68
74
  end
69
75
  end
@@ -1,3 +1,3 @@
1
1
  module ClassyEnum
2
- VERSION = "3.1.0"
2
+ VERSION = "3.1.1"
3
3
  end
@@ -26,6 +26,20 @@ describe ClassyEnum::Base do
26
26
  subject { ClassyEnumBase.build(:two) }
27
27
  it { should be_a(ClassyEnumBase::Two) }
28
28
  end
29
+
30
+ context 'nil' do
31
+ subject { ClassyEnumBase.build(nil) }
32
+ it { should be_a(ClassyEnumBase) }
33
+ it { should be_nil }
34
+ it { should be_blank }
35
+ end
36
+
37
+ context 'empty string' do
38
+ subject { ClassyEnumBase.build('') }
39
+ it { should be_a(ClassyEnumBase) }
40
+ it { should_not be_nil }
41
+ it { should be_blank }
42
+ end
29
43
  end
30
44
 
31
45
  context '#new' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: classy_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-12 00:00:00.000000000 Z
12
+ date: 2012-08-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -126,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
126
126
  version: '0'
127
127
  segments:
128
128
  - 0
129
- hash: 4485240533001760978
129
+ hash: 2229881947389413323
130
130
  required_rubygems_version: !ruby/object:Gem::Requirement
131
131
  none: false
132
132
  requirements:
@@ -135,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
135
  version: '0'
136
136
  segments:
137
137
  - 0
138
- hash: 4485240533001760978
138
+ hash: 2229881947389413323
139
139
  requirements: []
140
140
  rubyforge_project:
141
141
  rubygems_version: 1.8.24