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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0c025020189aba2a0c4d9cbdd591c98cd390b2f5
4
- data.tar.gz: 0dedf186e39e0955493d3aaecfaa5b3911240da6
3
+ metadata.gz: 3feb5bc8e7c871046cb647742055d1ce7bb4e69e
4
+ data.tar.gz: d20799661a20eff1e48029e8db401e14d5f1ce6f
5
5
  SHA512:
6
- metadata.gz: 349ae8f45cfbc1ff6b03f9987ad3e9d4780e2154753a1fef92924729decfc5b26a193ed7f4e41691f123354c7a46c9eb2e888682db381e7976149a48ca41f028
7
- data.tar.gz: 74540a02a97c87f6c648f30698146845ddbcf02bf756c1a4bfc4a39053d5bc2b5fc501017218fae27fbdb93ddf3273e9cb56c0276afa29479bb3797f43e11530
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 :field
66
- serialize :field
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 `field_data` if not specified.
74
+ It is set `field` if not specified.
75
75
 
76
76
  ```
77
77
  class SerializedObject; end
78
78
  object = InSerializedValue.new
79
- object.field_data = SerializedObject.new
80
- p object.field_data.class # => SerializedObject
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/[my-github-username]/object_field/fork )
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
- object = Oj.load(self.send(field_name))
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
- accessor = as || accessor_name(field_name)
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
- object = Marshal.load(Zlib.inflate(self.send(field_name)))
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
- [field_name, :_data].join.to_sym
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
 
@@ -1,4 +1,4 @@
1
1
  module ObjectField
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
4
4
 
@@ -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 :field1, :field2
7
- serialize :field1
8
- serialize :field2, as: :attributes
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? :field1_data).to be_truthy
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.field2).not_to eq(object)
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.2
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-09-27 00:00:00.000000000 Z
11
+ date: 2014-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj