object_field 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -6
- data/lib/object_field/jsonizer.rb +3 -1
- data/lib/object_field/serializer.rb +13 -4
- data/lib/object_field/version.rb +1 -1
- data/spec/object_field/serializer_spec.rb +5 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3feb5bc8e7c871046cb647742055d1ce7bb4e69e
|
4
|
+
data.tar.gz: d20799661a20eff1e48029e8db401e14d5f1ce6f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f59f227bd6bf641720ac2616195e9cd271e0ca088b00ef40fd016b11b505ee8e1d827fc0fc03dec1f99ce5973643b157948258d337ab65275986905c724547cd
|
7
|
+
data.tar.gz: d68b0d7f5032526a66dcc2ee4993c50cfd5371da28fb7018ce5886d57a78927de8494441a63119d1cd8bbde0a76e631fcb8ad9f73fb15f90b655fa9d244b22aa
|
data/README.md
CHANGED
@@ -62,8 +62,8 @@ Like marshal option of [redis-object](https://github.com/nateware/redis-objects)
|
|
62
62
|
```ruby
|
63
63
|
class InSerializedValue
|
64
64
|
include ObjectField::Serializer
|
65
|
-
attr_accessor :
|
66
|
-
serialize :
|
65
|
+
attr_accessor :field_data
|
66
|
+
serialize :field_data
|
67
67
|
end
|
68
68
|
```
|
69
69
|
|
@@ -71,19 +71,20 @@ Setter is save the serialized value.
|
|
71
71
|
Can not be saved, Proc object and anonymous class. (To raise TypeError)
|
72
72
|
Getter returns deserialized value.
|
73
73
|
Name of accessor can be specified with `as` option.
|
74
|
-
It is set `
|
74
|
+
It is set `field` if not specified.
|
75
75
|
|
76
76
|
```
|
77
77
|
class SerializedObject; end
|
78
78
|
object = InSerializedValue.new
|
79
|
-
object.
|
80
|
-
p object.
|
79
|
+
object.field = SerializedObject.new
|
80
|
+
p object.field.class # => SerializedObject
|
81
81
|
```
|
82
82
|
|
83
83
|
## Contributing
|
84
84
|
|
85
|
-
1. Fork it ( https://github.com/
|
85
|
+
1. Fork it ( https://github.com/i2bskn/object_field/fork )
|
86
86
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
87
87
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
88
88
|
4. Push to the branch (`git push origin my-new-feature`)
|
89
89
|
5. Create a new Pull Request
|
90
|
+
|
@@ -12,7 +12,9 @@ module ObjectField
|
|
12
12
|
private
|
13
13
|
def define_jsonizer(field_name, accessor)
|
14
14
|
define_method accessor do |klass=nil|
|
15
|
-
|
15
|
+
value = self.send(field_name)
|
16
|
+
return nil if value.nil?
|
17
|
+
object = Oj.load(value)
|
16
18
|
klass ? klass.new(object) : object
|
17
19
|
end
|
18
20
|
|
@@ -2,9 +2,15 @@ module ObjectField
|
|
2
2
|
module Serializer
|
3
3
|
module ClassMethods
|
4
4
|
def serialize(field_name, as: nil, compression: Zlib::DEFAULT_COMPRESSION)
|
5
|
-
|
5
|
+
define_serializer field_name, (as || accessor_name(field_name)), compression
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
def define_serializer(field_name, accessor, compression)
|
6
10
|
define_method accessor do |klass=nil|
|
7
|
-
|
11
|
+
value = self.send(field_name)
|
12
|
+
return nil if value.nil?
|
13
|
+
object = Marshal.load(Zlib.inflate(value))
|
8
14
|
klass ? klass.new(object) : object
|
9
15
|
end
|
10
16
|
|
@@ -13,9 +19,12 @@ module ObjectField
|
|
13
19
|
end
|
14
20
|
end
|
15
21
|
|
16
|
-
private
|
17
22
|
def accessor_name(field_name)
|
18
|
-
|
23
|
+
if /(.*)_data/ =~ field_name
|
24
|
+
$1.to_sym
|
25
|
+
else
|
26
|
+
[field_name, :_as_data].join.to_sym
|
27
|
+
end
|
19
28
|
end
|
20
29
|
end
|
21
30
|
|
data/lib/object_field/version.rb
CHANGED
@@ -3,9 +3,9 @@ require "spec_helper"
|
|
3
3
|
describe ObjectField::Serializer do
|
4
4
|
class InSerializedValue
|
5
5
|
include ObjectField::Serializer
|
6
|
-
attr_accessor :
|
7
|
-
serialize :
|
8
|
-
serialize :
|
6
|
+
attr_accessor :field1_data, :field2_data
|
7
|
+
serialize :field1_data
|
8
|
+
serialize :field2_data, as: :attributes
|
9
9
|
end
|
10
10
|
|
11
11
|
class SerializeObject; end
|
@@ -25,12 +25,12 @@ describe ObjectField::Serializer do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should define accessor" do
|
28
|
-
expect(example.respond_to? :
|
28
|
+
expect(example.respond_to? :field1).to be_truthy
|
29
29
|
expect(example.respond_to? :attributes).to be_truthy
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should save Object" do
|
33
|
-
expect(stored_instance.
|
33
|
+
expect(stored_instance.field2_data).not_to eq(object)
|
34
34
|
end
|
35
35
|
|
36
36
|
it "should parse Object" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: object_field
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- i2bskn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|