object_field 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 +4 -4
- data/.coveralls.yml +2 -0
- data/.travis.yml +11 -0
- data/README.md +54 -11
- data/Rakefile +8 -0
- data/lib/object_field.rb +2 -0
- data/lib/object_field/serializer.rb +27 -0
- data/lib/object_field/version.rb +1 -1
- data/object_field.gemspec +4 -6
- data/spec/object_field/jsonizer_spec.rb +49 -0
- data/spec/object_field/serializer_spec.rb +45 -0
- data/spec/spec_helper.rb +9 -0
- metadata +48 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c025020189aba2a0c4d9cbdd591c98cd390b2f5
|
4
|
+
data.tar.gz: 0dedf186e39e0955493d3aaecfaa5b3911240da6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 349ae8f45cfbc1ff6b03f9987ad3e9d4780e2154753a1fef92924729decfc5b26a193ed7f4e41691f123354c7a46c9eb2e888682db381e7976149a48ca41f028
|
7
|
+
data.tar.gz: 74540a02a97c87f6c648f30698146845ddbcf02bf756c1a4bfc4a39053d5bc2b5fc501017218fae27fbdb93ddf3273e9cb56c0276afa29479bb3797f43e11530
|
data/.coveralls.yml
ADDED
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
# ObjectField
|
2
2
|
|
3
|
-
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/object_field.svg)](http://badge.fury.io/rb/object_field)
|
4
|
+
[![Build Status](https://travis-ci.org/i2bskn/object_field.svg)](https://travis-ci.org/i2bskn/object_field)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/i2bskn/object_field/badge.png)](https://coveralls.io/r/i2bskn/object_field)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/i2bskn/object_field/badges/gpa.svg)](https://codeclimate.com/github/i2bskn/object_field)
|
7
|
+
[![Dependency Status](https://gemnasium.com/i2bskn/object_field.svg)](https://gemnasium.com/i2bskn/object_field)
|
8
|
+
|
9
|
+
Support for object persistence.
|
10
|
+
Save the object to data store via instance method.
|
4
11
|
|
5
12
|
## Installation
|
6
13
|
|
@@ -20,21 +27,57 @@ Or install it yourself as:
|
|
20
27
|
|
21
28
|
## Usage
|
22
29
|
|
23
|
-
|
24
|
-
|
30
|
+
Include `ObjectField::Jsonizer` or `ObjectField::Serializer`.
|
31
|
+
|
32
|
+
#### Jsonizer
|
25
33
|
|
26
|
-
|
34
|
+
`.#jsonize` defines accessor of JSON field.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class InJsonValue
|
27
38
|
include ObjectField::Jsonizer
|
28
|
-
attr_accessor :field1_json, :
|
29
|
-
jsonize :field1_json, :
|
39
|
+
attr_accessor :field1_json, :field2, :field3_json
|
40
|
+
jsonize :field1_json, :field2
|
30
41
|
jsonize :field3_json, as: :attributes
|
31
42
|
end
|
43
|
+
```
|
44
|
+
|
45
|
+
Setter is save the value to JSON.
|
46
|
+
Getter returns parsed value.
|
47
|
+
Name of accessor can be specified with `as` option.
|
48
|
+
It is set `field1`, `field2_as_json` if not specified.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
object = InJsonValue.new
|
52
|
+
object.field1 = [1, 2, 3]
|
53
|
+
p object.field1_json # => "[1,2,3]"
|
54
|
+
object.field1.each {|i| p i}
|
55
|
+
```
|
56
|
+
|
57
|
+
#### Serializer
|
58
|
+
|
59
|
+
`.#serialize` defines accessor of marshal data field.
|
60
|
+
Like marshal option of [redis-object](https://github.com/nateware/redis-objects).
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class InSerializedValue
|
64
|
+
include ObjectField::Serializer
|
65
|
+
attr_accessor :field
|
66
|
+
serialize :field
|
67
|
+
end
|
68
|
+
```
|
32
69
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
70
|
+
Setter is save the serialized value.
|
71
|
+
Can not be saved, Proc object and anonymous class. (To raise TypeError)
|
72
|
+
Getter returns deserialized value.
|
73
|
+
Name of accessor can be specified with `as` option.
|
74
|
+
It is set `field_data` if not specified.
|
75
|
+
|
76
|
+
```
|
77
|
+
class SerializedObject; end
|
78
|
+
object = InSerializedValue.new
|
79
|
+
object.field_data = SerializedObject.new
|
80
|
+
p object.field_data.class # => SerializedObject
|
38
81
|
```
|
39
82
|
|
40
83
|
## Contributing
|
data/Rakefile
CHANGED
data/lib/object_field.rb
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
module ObjectField
|
2
|
+
module Serializer
|
3
|
+
module ClassMethods
|
4
|
+
def serialize(field_name, as: nil, compression: Zlib::DEFAULT_COMPRESSION)
|
5
|
+
accessor = as || accessor_name(field_name)
|
6
|
+
define_method accessor do |klass=nil|
|
7
|
+
object = Marshal.load(Zlib.inflate(self.send(field_name)))
|
8
|
+
klass ? klass.new(object) : object
|
9
|
+
end
|
10
|
+
|
11
|
+
define_method "#{accessor}=" do |object|
|
12
|
+
self.send "#{field_name}=", Zlib.deflate(Marshal.dump(object), compression)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def accessor_name(field_name)
|
18
|
+
[field_name, :_data].join.to_sym
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.included(base)
|
23
|
+
base.extend ClassMethods
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
data/lib/object_field/version.rb
CHANGED
data/object_field.gemspec
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# coding: utf-8
|
2
1
|
lib = File.expand_path('../lib', __FILE__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
3
|
require 'object_field/version'
|
@@ -19,10 +18,9 @@ Gem::Specification.new do |spec|
|
|
19
18
|
spec.require_paths = ["lib"]
|
20
19
|
|
21
20
|
spec.add_dependency "oj"
|
22
|
-
spec.add_development_dependency "bundler"
|
23
|
-
spec.add_development_dependency "rake"
|
24
|
-
|
25
|
-
|
26
|
-
# spec.add_development_dependency "simplecov"
|
21
|
+
spec.add_development_dependency "bundler"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "coveralls"
|
27
25
|
end
|
28
26
|
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ObjectField::Jsonizer do
|
4
|
+
class InJsonValue
|
5
|
+
include ObjectField::Jsonizer
|
6
|
+
attr_accessor :field1_json, :field2, :field3_json
|
7
|
+
jsonize :field1_json, :field2
|
8
|
+
jsonize :field3_json, as: :attributes
|
9
|
+
end
|
10
|
+
|
11
|
+
describe ".#included" do
|
12
|
+
it "should define singleton method" do
|
13
|
+
expect(InJsonValue.respond_to? :jsonize).to be_truthy
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it "not possible to specify more than one accessor" do
|
18
|
+
expect{InJsonValue.jsonize(:field1, :field2, as: :field_name)}.to raise_error(ArgumentError)
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "included class" do
|
22
|
+
let(:example) { InJsonValue.new }
|
23
|
+
let(:hash) { {key: :value} }
|
24
|
+
let(:stored_instance) do
|
25
|
+
example.attributes = hash
|
26
|
+
example
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should define accessor" do
|
30
|
+
expect(example.respond_to? :field1).to be_truthy
|
31
|
+
expect(example.respond_to? :field2_as_json).to be_truthy
|
32
|
+
expect(example.respond_to? :attributes).to be_truthy
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should save JSON value" do
|
36
|
+
expect(stored_instance.field3_json).to eq(Oj.dump(hash))
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should parse JSON value" do
|
40
|
+
expect(stored_instance.field3_json).not_to eq(hash)
|
41
|
+
expect(stored_instance.attributes).to eq(hash)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should generate instance with parse JSON value" do
|
45
|
+
expect(stored_instance.attributes(Struct.new(:value)).value).to eq(hash)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ObjectField::Serializer do
|
4
|
+
class InSerializedValue
|
5
|
+
include ObjectField::Serializer
|
6
|
+
attr_accessor :field1, :field2
|
7
|
+
serialize :field1
|
8
|
+
serialize :field2, as: :attributes
|
9
|
+
end
|
10
|
+
|
11
|
+
class SerializeObject; end
|
12
|
+
|
13
|
+
describe ".#included" do
|
14
|
+
it "should define singleton method" do
|
15
|
+
expect(InSerializedValue.respond_to? :serialize).to be_truthy
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "included class" do
|
20
|
+
let(:example) { InSerializedValue.new }
|
21
|
+
let(:object) { SerializeObject.new }
|
22
|
+
let(:stored_instance) do
|
23
|
+
example.attributes = object
|
24
|
+
example
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should define accessor" do
|
28
|
+
expect(example.respond_to? :field1_data).to be_truthy
|
29
|
+
expect(example.respond_to? :attributes).to be_truthy
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should save Object" do
|
33
|
+
expect(stored_instance.field2).not_to eq(object)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should parse Object" do
|
37
|
+
expect(stored_instance.attributes.class).to eq(SerializeObject)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should generate instance with parse Object" do
|
41
|
+
expect(stored_instance.attributes(Struct.new(:value)).value.class).to eq(SerializeObject)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
data/spec/spec_helper.rb
ADDED
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.2
|
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-
|
11
|
+
date: 2014-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|
@@ -28,30 +28,58 @@ dependencies:
|
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
46
60
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
61
|
+
version: '0'
|
48
62
|
type: :development
|
49
63
|
prerelease: false
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
|
-
- - "
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: coveralls
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
53
74
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
55
83
|
description: Support for object persistence.
|
56
84
|
email:
|
57
85
|
- i2bskn@gmail.com
|
@@ -59,15 +87,21 @@ executables: []
|
|
59
87
|
extensions: []
|
60
88
|
extra_rdoc_files: []
|
61
89
|
files:
|
90
|
+
- ".coveralls.yml"
|
62
91
|
- ".gitignore"
|
92
|
+
- ".travis.yml"
|
63
93
|
- Gemfile
|
64
94
|
- LICENSE.txt
|
65
95
|
- README.md
|
66
96
|
- Rakefile
|
67
97
|
- lib/object_field.rb
|
68
98
|
- lib/object_field/jsonizer.rb
|
99
|
+
- lib/object_field/serializer.rb
|
69
100
|
- lib/object_field/version.rb
|
70
101
|
- object_field.gemspec
|
102
|
+
- spec/object_field/jsonizer_spec.rb
|
103
|
+
- spec/object_field/serializer_spec.rb
|
104
|
+
- spec/spec_helper.rb
|
71
105
|
homepage: https://github.com/i2bskn/object_field
|
72
106
|
licenses:
|
73
107
|
- MIT
|
@@ -92,4 +126,7 @@ rubygems_version: 2.2.2
|
|
92
126
|
signing_key:
|
93
127
|
specification_version: 4
|
94
128
|
summary: Support for object persistence.
|
95
|
-
test_files:
|
129
|
+
test_files:
|
130
|
+
- spec/object_field/jsonizer_spec.rb
|
131
|
+
- spec/object_field/serializer_spec.rb
|
132
|
+
- spec/spec_helper.rb
|