classy_enum 1.3.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -1,3 +1,4 @@
1
1
  rvm:
2
2
  - 1.8.7
3
3
  - 1.9.2
4
+ - 1.9.3
data/README.textile CHANGED
@@ -149,6 +149,26 @@ In the above example, high priority alarms are only emailed if the owning alarm
149
149
  @alarm.send_email? # => false
150
150
  </pre>
151
151
 
152
+ h2. Serializing as JSON
153
+
154
+ By default, the enum will be serialized as a string representing the value:
155
+
156
+ <pre>
157
+ @alarm = Alarm.create(:priority => :high, :enabled => true)
158
+ @alarm.to_json.should == "{\"alarm\":{\"priority\":\"high\"}}"
159
+ </pre>
160
+
161
+ This behavior can be overridden by using the @:serialize_as_json => true@ option in your ActiveRecord model:
162
+
163
+ <pre>
164
+ class Alarm < ActiveRecord::Base
165
+ classy_enum_attr :priority, :serialize_as_json => true
166
+ end
167
+
168
+ @alarm = Alarm.create(:priority => :high, :enabled => true)
169
+ @alarm.to_json.should == "{\"alarm\":{\"priority\":{}}}"
170
+ </pre>
171
+
152
172
  h2. Special Cases
153
173
 
154
174
  What if your enum class name is not the same as your model's attribute name? No problem! Just use a second arugment in @classy_enum_attr@ to declare the attribute name. In this case, the model's attribute is called *alarm_priority*.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.1
1
+ 1.3.2
data/classy_enum.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{classy_enum}
8
- s.version = "1.3.1"
8
+ s.version = "1.3.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Peter Brown}]
12
- s.date = %q{2011-10-28}
12
+ s.date = %q{2011-11-12}
13
13
  s.description = %q{A utility that adds class based enum functionality to ActiveRecord attributes}
14
14
  s.email = %q{github@lette.us}
15
15
  s.extra_rdoc_files = [
@@ -25,6 +25,7 @@ module ClassyEnum
25
25
  enum = options[:enum] || attribute
26
26
  allow_blank = options[:allow_blank] || false
27
27
  allow_nil = options[:allow_nil] || false
28
+ serialize_as_json = options[:serialize_as_json] || false
28
29
 
29
30
  klass = enum.to_s.camelize.constantize
30
31
 
@@ -36,7 +37,7 @@ module ClassyEnum
36
37
 
37
38
  # Define getter method that returns a ClassyEnum instance
38
39
  define_method attribute do
39
- klass.build(super(), self)
40
+ klass.build(super(), :owner => self, :serialize_as_json => serialize_as_json)
40
41
  end
41
42
 
42
43
  # Define setter method that accepts either string or symbol for member
@@ -26,15 +26,12 @@ module ClassyEnum
26
26
  @index = index + 1
27
27
  @option = option
28
28
 
29
+ attr_accessor :owner, :serialize_as_json
30
+
29
31
  # Use ActiveModel::AttributeMethods to define attribute? methods
30
32
  attribute_method_suffix '?'
31
33
  define_attribute_methods enums
32
34
 
33
- def initialize
34
- @to_s = self.class.instance_variable_get('@option').to_s
35
- @index = self.class.instance_variable_get('@index')
36
- end
37
-
38
35
  end
39
36
 
40
37
  klass_name = "#{self}#{option.to_s.camelize}"
@@ -51,12 +48,13 @@ module ClassyEnum
51
48
  # end
52
49
  #
53
50
  # Priority.build(:low) # => PriorityLow.new
54
- def build(option, owner = nil)
55
- return option if option.blank?
56
- return TypeError.new("Valid #{self} options are #{self.valid_options}") unless self::OPTIONS.include? option.to_sym
51
+ def build(value, options={})
52
+ return value if value.blank?
53
+ return TypeError.new("Valid #{self} options are #{self.valid_options}") unless self::OPTIONS.include? value.to_sym
57
54
 
58
- object = Object.const_get("#{self}#{option.to_s.camelize}").new
59
- object.instance_variable_set(:@owner,owner) unless owner.nil?
55
+ object = Object.const_get("#{self}#{value.to_s.camelize}").new
56
+ object.owner = options[:owner]
57
+ object.serialize_as_json = options[:serialize_as_json]
60
58
  object
61
59
  end
62
60
 
@@ -12,7 +12,7 @@ module ClassyEnum
12
12
  # @priority = PriorityMedium.new
13
13
  # @priority.index # => 2
14
14
  def index
15
- @index
15
+ self.class.instance_variable_get('@index')
16
16
  end
17
17
 
18
18
  alias :to_i :index
@@ -28,7 +28,7 @@ module ClassyEnum
28
28
  # @priority = PriorityLow.new
29
29
  # @priority.to_s # => 'low'
30
30
  def to_s
31
- @to_s
31
+ self.class.instance_variable_get('@option').to_s
32
32
  end
33
33
 
34
34
  # Returns a Symbol corresponding to a string representation of element,
@@ -43,7 +43,7 @@ module ClassyEnum
43
43
  # @priority = PriorityLow.new
44
44
  # @priority.to_sym # => :low
45
45
  def to_sym
46
- @to_s.to_sym
46
+ to_s.to_sym
47
47
  end
48
48
 
49
49
  # Returns string representing enum in Rails titleize format
@@ -57,7 +57,7 @@ module ClassyEnum
57
57
  # @priority = Priority.build(:really_high)
58
58
  # @priority.name # => "Really High"
59
59
  def name
60
- @to_s.titleize
60
+ to_s.titleize
61
61
  end
62
62
 
63
63
  # Sort an array of elements based on the order they are defined
@@ -76,7 +76,7 @@ module ClassyEnum
76
76
  # priorities.max # => @high
77
77
  # priorities.min # => @low
78
78
  def <=> other
79
- @index <=> other.index
79
+ index <=> other.index
80
80
  end
81
81
 
82
82
  # Used by ActiveRecord::PredicateBuilder when building from a hash
@@ -92,8 +92,10 @@ module ClassyEnum
92
92
 
93
93
  # Overrides as_json to remove owner reference recursion issues
94
94
  def as_json(options=nil)
95
+ return to_s unless serialize_as_json
95
96
  json = super(options)
96
97
  json.delete('owner')
98
+ json.delete('serialize_as_json')
97
99
  json
98
100
  end
99
101
 
@@ -15,9 +15,15 @@ class Cat < ActiveRecord::Base
15
15
  delegate :breed_color, :to => :breed
16
16
  end
17
17
 
18
+ class OtherCat < ActiveRecord::Base
19
+ classy_enum_attr :breed, :enum => :cat_breed, :serialize_as_json => true
20
+ attr_accessor :color
21
+ delegate :breed_color, :to => :breed
22
+ end
23
+
18
24
  describe Cat do
19
25
  let(:abyssian) { Cat.new(:breed => :abyssian, :color => 'black') }
20
- let(:persian) { Cat.new(:breed => :persian, :color => 'white') }
26
+ let(:persian) { OtherCat.new(:breed => :persian, :color => 'white') }
21
27
 
22
28
  it 'should delegate breed color to breed with an ownership reference' do
23
29
  abyssian.breed_color { should eql('black Abyssian') }
@@ -25,8 +31,11 @@ describe Cat do
25
31
  end
26
32
 
27
33
  it 'should correctly serialize without the owner reference' do
28
- abyssian.to_json.should == "{\"cat\":{\"breed\":{\"to_s\":\"abyssian\",\"index\":1}}}"
29
- persian.to_json.should == "{\"cat\":{\"breed\":{\"to_s\":\"persian\",\"index\":4}}}"
34
+ abyssian.to_json.should == "{\"cat\":{\"breed\":\"abyssian\"}}"
35
+ end
36
+
37
+ it 'should convert the enum to a string when serializing' do
38
+ persian.to_json.should == "{\"other_cat\":{\"breed\":{}}}"
30
39
  end
31
40
 
32
41
  end
data/spec/spec_helper.rb CHANGED
@@ -43,6 +43,10 @@ ActiveRecord::Schema.define(:version => 1) do
43
43
  create_table :cats, :force => true do |t|
44
44
  t.string :breed
45
45
  end
46
+
47
+ create_table :other_cats, :force => true do |t|
48
+ t.string :breed
49
+ end
46
50
  end
47
51
 
48
52
  class Breed < ClassyEnum::Base
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: 1.3.1
4
+ version: 1.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-28 00:00:00.000000000Z
12
+ date: 2011-11-12 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70107427721740 !ruby/object:Gem::Requirement
16
+ requirement: &70109521646960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70107427721740
24
+ version_requirements: *70109521646960
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: jeweler
27
- requirement: &70107427721140 !ruby/object:Gem::Requirement
27
+ requirement: &70109521646460 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.6.2
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70107427721140
35
+ version_requirements: *70109521646460
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec-rails
38
- requirement: &70107427720540 !ruby/object:Gem::Requirement
38
+ requirement: &70109521645440 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 2.6.1
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70107427720540
46
+ version_requirements: *70109521645440
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: formtastic
49
- requirement: &70107427719940 !ruby/object:Gem::Requirement
49
+ requirement: &70109521644780 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.2.4
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70107427719940
57
+ version_requirements: *70109521644780
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: sqlite3-ruby
60
- requirement: &70107427719340 !ruby/object:Gem::Requirement
60
+ requirement: &70109521644180 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70107427719340
68
+ version_requirements: *70109521644180
69
69
  description: A utility that adds class based enum functionality to ActiveRecord attributes
70
70
  email: github@lette.us
71
71
  executables: []
@@ -114,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
114
114
  version: '0'
115
115
  segments:
116
116
  - 0
117
- hash: -3596092072518778020
117
+ hash: -4456031736000763211
118
118
  required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  none: false
120
120
  requirements: