active_record_enumerated_type 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MzgwY2UwNzY2OTIxZDhiNGI1MjQwNzkwYTI5Mjk1ZTM3YWM4YzFjMQ==
4
+ NWZmNmIwOTc0ZjIyZTM5ODM0Yjg1ZmQ0Y2M1ZDVhOTViMTQ2NzYzMQ==
5
5
  data.tar.gz: !binary |-
6
- OGE0NzFjYzBhNzAyZWQ0YzJlNjA3NTdlNzkwMzJkYmViOWY1Y2Y5Zg==
6
+ OWQ2MTJjZjIxZGQ4MTM2MDkzNGFjMmFjN2M1MjFjMThiM2I3NGQyMg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YjhkODUwMTAyZjg4NWZjMDZkMzAzODUyMzUyOGJhNTJhYzNhNjJkYmE0YmNk
10
- ZTU0ZjM5MGU4ZTkzNzNjNTZiOGQ1NmZhZjA1ZjQ4M2JkMGIyYmE1YmMwMGUw
11
- OWJmZDFiYWY2YjEwNjZkYmMxYjU0ZTBmMzAxOGViZTg2YmVkNzY=
9
+ YWQ3Zjg2MWY0MjRjM2ZkNzEzMmQ2YTVlZDMwNmZmYWRjZmY0M2Y5NzBhYjA1
10
+ YzM1MzI3ZWE2MTZhOTY4Y2EwNDNiZjE1NjljMTMwOWUwNGRmMThhMzVmOWUw
11
+ N2YzMGQxY2U4MjQyMjU1M2U5N2IwNjBiNmRlYTk1YTAxN2FmNmY=
12
12
  data.tar.gz: !binary |-
13
- NWJhZGNkZGNjZGNiNmQ5ZmYwNTJhYjI1Yzk5NGEwZWVkMmZjZDU5MWI4MDJk
14
- NGUwMmI5OGUwZjdmMzcwNjU5YTljYjA5OTZmMmFlNDNkZmRhNjgxMGY4NTEz
15
- ODg1Njk5NmNmYWU0NzhiZjNiMDFkNDNhMGNlMmQ4YzJmYzIyOGQ=
13
+ MjM3ZGEzODIwMGUzNDU4MjJmNDA3NTE2ZTM5ZTExNjk0NDMxMmRlZDZiYjBl
14
+ MWY0Y2NlN2YzNjgxMzYyMDk2NjQwNzMyZGEwMzg0ODI4ZTI0ZWUzMmUxZTM4
15
+ N2VhNWQyYTIxNTNiODRhOWRhYWU5ZThjYTliNWMwOGFjZGFhODk=
data/README.md CHANGED
@@ -81,6 +81,32 @@ job.status.human
81
81
  # => 'started'
82
82
  ```
83
83
 
84
+ ### Custom serialization
85
+ For performance reasons, it is sometimes advantageous to store enum values as integers. This can be achieved via custom serialization.
86
+
87
+ ```Ruby
88
+ class JobStatus
89
+ include EnumeratedType
90
+
91
+ declare :started, id: 1
92
+ declare :finished, id: 2
93
+
94
+ def self.deserialize(value)
95
+ detect { |type| type.id == value.to_i }
96
+ end
97
+
98
+ def serialize
99
+ id
100
+ end
101
+ end
102
+
103
+ job = Job.new(status: :finished)
104
+ # => #<Job status: 2>
105
+
106
+ job.status
107
+ #<JobStatus:finished>
108
+ ```
109
+
84
110
  ## Contributing
85
111
 
86
112
  1. Fork it
@@ -9,12 +9,12 @@ module ActiveRecord
9
9
  type_class = options.fetch(:to)
10
10
 
11
11
  define_method(attribute) do
12
- super() && type_class[super().to_sym]
12
+ read_attribute(attribute) && type_class.deserialize(read_attribute(attribute))
13
13
  end
14
14
 
15
15
  define_method(:"#{attribute}=") do |value|
16
16
  begin
17
- super(value.presence && type_class.coerce(value).serialize)
17
+ write_attribute(attribute, value.presence && type_class.coerce(value).serialize)
18
18
  rescue ArgumentError
19
19
  valid_types = type_class.map { |type| "'#{type}'" }.to_sentence
20
20
  raise TypeError, "'#{value}' is not a valid type for #{attribute}. Valid types include #{valid_types}."
@@ -8,4 +8,10 @@ module EnumeratedType
8
8
  rescue I18n::MissingTranslationData
9
9
  to_s.titleize
10
10
  end
11
+
12
+ module ClassMethods
13
+ def deserialize(value)
14
+ self[value.to_sym]
15
+ end
16
+ end
11
17
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordEnumeratedType
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -10,7 +10,7 @@ describe EnumeratedType do
10
10
  expect(Status[:finished].human).to eq "finito"
11
11
  end
12
12
 
13
- it "defaulst to the name" do
13
+ it "defaults to the name" do
14
14
  expect(Status[:finished].human).to eq "Finished"
15
15
  end
16
16
  end
@@ -3,16 +3,17 @@ require "spec_helper"
3
3
  class TestRecord
4
4
  include ActiveRecord::TypeRestriction
5
5
 
6
- module InheritedAttributes
7
- def status
8
- @status
9
- end
6
+ def write_attribute(attribute, value)
7
+ instance_variable_set("@#{attribute}", value)
8
+ end
10
9
 
11
- def status=(value)
12
- @status = value
13
- end
10
+ def read_attribute(attribute)
11
+ instance_variable_get("@#{attribute}")
12
+ end
13
+
14
+ def raw_status
15
+ @status
14
16
  end
15
- include InheritedAttributes
16
17
 
17
18
  restrict_type_of :status, to: Status
18
19
  end
@@ -55,4 +56,37 @@ describe TestRecord do
55
56
  }.to raise_error(TypeError, error_message)
56
57
  end
57
58
  end
59
+
60
+ describe "custom serialization" do
61
+ let(:instance) { SerializingTestRecord.new }
62
+
63
+ class SerializableStatus
64
+ include EnumeratedType
65
+
66
+ declare :started, id: 1
67
+ declare :finished, id: 2
68
+
69
+ def self.deserialize(value)
70
+ detect { |type| type.id == value.to_i }
71
+ end
72
+
73
+ def serialize
74
+ id
75
+ end
76
+ end
77
+
78
+ class SerializingTestRecord < TestRecord
79
+ restrict_type_of :status, to: SerializableStatus
80
+ end
81
+
82
+ it "serializes" do
83
+ instance.status = SerializableStatus[:started]
84
+ expect(instance.raw_status).to eq(1)
85
+ end
86
+
87
+ it "deserializes" do
88
+ instance.status = SerializableStatus[:started]
89
+ expect(instance.status).to eq(SerializableStatus[:started])
90
+ end
91
+ end
58
92
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_enumerated_type
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Eddy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-09 00:00:00.000000000 Z
11
+ date: 2014-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport