active_record_enumerated_type 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +26 -0
- data/lib/active_record_enumerated_type/active_record/type_restriction.rb +2 -2
- data/lib/active_record_enumerated_type/enumerated_type.rb +6 -0
- data/lib/active_record_enumerated_type/version.rb +1 -1
- data/spec/enumerated_type_spec.rb +1 -1
- data/spec/type_restriction_spec.rb +42 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NWZmNmIwOTc0ZjIyZTM5ODM0Yjg1ZmQ0Y2M1ZDVhOTViMTQ2NzYzMQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
OWQ2MTJjZjIxZGQ4MTM2MDkzNGFjMmFjN2M1MjFjMThiM2I3NGQyMg==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YWQ3Zjg2MWY0MjRjM2ZkNzEzMmQ2YTVlZDMwNmZmYWRjZmY0M2Y5NzBhYjA1
|
10
|
+
YzM1MzI3ZWE2MTZhOTY4Y2EwNDNiZjE1NjljMTMwOWUwNGRmMThhMzVmOWUw
|
11
|
+
N2YzMGQxY2U4MjQyMjU1M2U5N2IwNjBiNmRlYTk1YTAxN2FmNmY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
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
|
-
|
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}."
|
@@ -3,16 +3,17 @@ require "spec_helper"
|
|
3
3
|
class TestRecord
|
4
4
|
include ActiveRecord::TypeRestriction
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
6
|
+
def write_attribute(attribute, value)
|
7
|
+
instance_variable_set("@#{attribute}", value)
|
8
|
+
end
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
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.
|
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-
|
11
|
+
date: 2014-08-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|