zerial 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 32d1173deb0ea69c92627917636ed2ec9f98b56d
4
+ data.tar.gz: 6c2fbd532dba93d53468c90a32393583c17af1b8
5
+ SHA512:
6
+ metadata.gz: e192859c118aa84b8ceae970d22b6e913ec4046cca9ff9b4556cd0f76b6d0ae779b4c651617c26638cae86d4e79fe32983497eff6aa7b5e2575559907c7435d0
7
+ data.tar.gz: d1d66324a0bc4d2d86de2e39f6963fbe565e5946bfefc912ff367e7b0ef708d78ff984a88e80b0cdbd26e10f1e88bb7be1f4e73653694b365330d75ac0a68b7a
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zerial.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Jell
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Zerial
2
+
3
+ Serializer/Deserializer tools
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'zerial'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install zerial
18
+
19
+ ## Usage
20
+
21
+ A serializer is an object with 4 methods: `to_json`, `as_json`, `from_json` and `from_loaded_json`.
22
+
23
+ `to_json` and `from_json` are the main entry points of the
24
+ serializer/deserializer, and `as_json` and `from_loaded_json` are
25
+ intermediate processing methods.
26
+
27
+ See examples in `spec/lib/zerial`
28
+
29
+ ## TODO
30
+
31
+ * Remove dependency on ActiveSupport
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/zerial/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,13 @@
1
+ module Zerial
2
+ module BaseSerializer
3
+ def to_json (object)
4
+ as_json(object).to_json
5
+ end
6
+
7
+ def from_json (json_string)
8
+ from_loaded_json(
9
+ JSON.load(json_string)
10
+ )
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ require 'zerial/base_serializer'
2
+
3
+ module Zerial
4
+ module BigDecimalSerializer
5
+ extend BaseSerializer
6
+ def self.as_json (object)
7
+ object.to_s
8
+ end
9
+
10
+ def self.from_loaded_json (json)
11
+ BigDecimal.new(json)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,24 @@
1
+ require 'zerial/base_serializer'
2
+
3
+ module Zerial
4
+ class CollectionSerializer
5
+ include BaseSerializer
6
+ attr_reader :element_serializer
7
+
8
+ def initialize (element_serializer)
9
+ @element_serializer = element_serializer
10
+ end
11
+
12
+ def as_json (collection)
13
+ collection.map { |object|
14
+ element_serializer.as_json(object)
15
+ }
16
+ end
17
+
18
+ def from_loaded_json (json)
19
+ json.map { |element|
20
+ element_serializer.from_loaded_json(element)
21
+ }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ require 'zerial/base_serializer'
2
+
3
+ module Zerial
4
+ module DateSerializer
5
+ extend BaseSerializer
6
+
7
+ def self.as_json(object)
8
+ object.as_json
9
+ end
10
+
11
+ def self.from_loaded_json (json)
12
+ Date.parse(json)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require 'zerial/base_serializer'
2
+
3
+ module Zerial
4
+ module DefaultSerializer
5
+ extend BaseSerializer
6
+
7
+ def self.as_json (object)
8
+ object.as_json
9
+ end
10
+
11
+ def self.from_loaded_json (json)
12
+ json
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,42 @@
1
+ require 'zerial/base_serializer'
2
+
3
+ module Zerial
4
+ class ImmutableRecordSerializer
5
+ include BaseSerializer
6
+
7
+ attr_reader :record_class, :serializers
8
+
9
+ def initialize (record_class, serializers: {})
10
+ @record_class = record_class
11
+ @serializers = serializers
12
+ end
13
+
14
+ def as_json (object)
15
+ record_class::ATTRIBUTES.reduce({}) do |attributes, attr|
16
+ attributes.merge(
17
+ attr.to_s => serializer_for_attribute(attr).as_json(
18
+ object.public_send(attr)
19
+ )
20
+ )
21
+ end
22
+ end
23
+
24
+ def from_loaded_json (json)
25
+ record_class.new(
26
+ record_class::ATTRIBUTES.reduce({}) { |attributes, attr|
27
+ attributes.merge(
28
+ attr => serializer_for_attribute(attr).from_loaded_json(
29
+ json.fetch(attr.to_s)
30
+ )
31
+ )
32
+ }
33
+ )
34
+ end
35
+
36
+ private
37
+
38
+ def serializer_for_attribute (attr)
39
+ serializers.fetch(attr, DefaultSerializer)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,19 @@
1
+ require 'zerial/base_serializer'
2
+
3
+ module Zerial
4
+ module MoneySerializer
5
+ extend BaseSerializer
6
+ def self.as_json (object)
7
+ {
8
+ "cents" => object.cents,
9
+ "currency" => object.currency_as_string
10
+ }
11
+ end
12
+
13
+ def self.from_loaded_json (json)
14
+ Money.new(
15
+ json.fetch("cents"), json.fetch("currency")
16
+ )
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ require 'zerial/base_serializer'
2
+
3
+ module Zerial
4
+ module NilSerializer
5
+ extend BaseSerializer
6
+ def self.as_json (object)
7
+ nil
8
+ end
9
+
10
+ def self.from_loaded_json (json)
11
+ nil
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'zerial/base_serializer'
2
+
3
+ module Zerial
4
+ module TimestampSerializer
5
+ extend BaseSerializer
6
+ def self.as_json (object)
7
+ object.iso8601
8
+ end
9
+
10
+ def self.from_loaded_json (json)
11
+ Time.zone.parse(json)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Zerial
2
+ VERSION = "0.0.1"
3
+ end
data/lib/zerial.rb ADDED
@@ -0,0 +1,17 @@
1
+ require "zerial/version"
2
+
3
+ require "zerial/default_serializer"
4
+
5
+ require "zerial/big_decimal_serializer"
6
+ require "zerial/date_serializer"
7
+ require "zerial/money_serializer"
8
+ require "zerial/nil_serializer"
9
+ require "zerial/timestamp_serializer"
10
+
11
+ require "zerial/collection_serializer"
12
+ require "zerial/immutable_record_serializer"
13
+
14
+ require "active_support/core_ext"
15
+
16
+ module Zerial
17
+ end
@@ -0,0 +1,141 @@
1
+ require 'spec_helper'
2
+ require 'zerial'
3
+ require 'bigdecimal'
4
+ require 'money'
5
+ require 'immutable_record'
6
+
7
+ describe Zerial do
8
+ before do
9
+ Time.zone = "UTC"
10
+ end
11
+
12
+ def expect_correct_roundtrip (serializer, object)
13
+ expect(
14
+ serializer.from_json(serializer.to_json(object))
15
+ ).to eq(object)
16
+ end
17
+
18
+ describe Zerial::DefaultSerializer do
19
+ it "uses default as_json" do
20
+ expect_correct_roundtrip(
21
+ Zerial::DefaultSerializer,
22
+ {"hello" => [1, "2"]}
23
+ )
24
+ end
25
+ end
26
+
27
+ describe Zerial::BigDecimalSerializer do
28
+ it "works in a roundtrip" do
29
+ expect_correct_roundtrip(
30
+ Zerial::BigDecimalSerializer,
31
+ BigDecimal.new("12.234")
32
+ )
33
+ end
34
+ end
35
+
36
+ describe Zerial::DateSerializer do
37
+ it "works in a roundtrip" do
38
+ expect_correct_roundtrip(
39
+ Zerial::DateSerializer,
40
+ Date.new(2013, 1 ,1)
41
+ )
42
+ end
43
+ end
44
+
45
+ describe Zerial::MoneySerializer do
46
+ it "works in a roundtrip" do
47
+ expect_correct_roundtrip(
48
+ Zerial::MoneySerializer,
49
+ Money.new(10_00, "SEK")
50
+ )
51
+ end
52
+ end
53
+
54
+ describe Zerial::NilSerializer do
55
+ it "always returns nil when serializing and deserializing" do
56
+ expect(Zerial::NilSerializer.to_json("hello")).to eq("null")
57
+ expect(Zerial::NilSerializer.from_json('{"hello": 1}')).to be_nil
58
+ end
59
+ end
60
+
61
+ describe Zerial::TimestampSerializer do
62
+ it "works in a roundtrip" do
63
+ expect_correct_roundtrip(
64
+ Zerial::TimestampSerializer,
65
+ Time.current.change(usec: 0)
66
+ )
67
+ end
68
+ end
69
+
70
+ describe Zerial::CollectionSerializer do
71
+ it "serializes the elements of an array with a given serializer" do
72
+ expect_correct_roundtrip(
73
+ Zerial::CollectionSerializer.new(
74
+ Zerial::BigDecimalSerializer
75
+ ),
76
+ [BigDecimal.new("10"), BigDecimal.new("0.11111")]
77
+ )
78
+ end
79
+ end
80
+
81
+ describe Zerial::ImmutableRecordSerializer do
82
+ let(:record_class) {
83
+ ImmutableRecord.new(:attr1, :attr2)
84
+ }
85
+ it "can serialize an ImmutableRecord" do
86
+ expect_correct_roundtrip(
87
+ Zerial::ImmutableRecordSerializer.new(record_class),
88
+ record_class.new(attr1: "hello", attr2: 2)
89
+ )
90
+ end
91
+
92
+ it "can accept specific serializers per attribute" do
93
+ expect_correct_roundtrip(
94
+ Zerial::ImmutableRecordSerializer.new(
95
+ record_class,
96
+ serializers: {
97
+ attr1: Zerial::BigDecimalSerializer,
98
+ attr2: Zerial::MoneySerializer
99
+ }
100
+ ),
101
+ record_class.new(
102
+ attr1: BigDecimal.new("10"),
103
+ attr2: Money.new(10, "SEK")
104
+ )
105
+ )
106
+ end
107
+
108
+ it "works with deeply nested stuff" do
109
+ expect_correct_roundtrip(
110
+ Zerial::ImmutableRecordSerializer.new(
111
+ record_class,
112
+ serializers: {
113
+ attr1: Zerial::CollectionSerializer.new(
114
+ Zerial::ImmutableRecordSerializer.new(
115
+ record_class,
116
+ serializers: {
117
+ attr1: Zerial::MoneySerializer,
118
+ attr2: Zerial::TimestampSerializer
119
+ }
120
+ )
121
+ ),
122
+ attr2: Zerial::DateSerializer
123
+ }
124
+ ),
125
+ record_class.new(
126
+ attr1: [
127
+ record_class.new(
128
+ attr1: Money.new(10, "SEK"),
129
+ attr2: Time.current.change(usec: 0)
130
+ ),
131
+ record_class.new(
132
+ attr1: Money.new(20, "EUR"),
133
+ attr2: 1.day.ago.change(usec: 0)
134
+ ),
135
+ ],
136
+ attr2: Date.today
137
+ )
138
+ )
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,9 @@
1
+ RSpec.configure do |config|
2
+ config.expect_with :rspec do |expectations|
3
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
4
+ end
5
+
6
+ config.mock_with :rspec do |mocks|
7
+ mocks.verify_partial_doubles = true
8
+ end
9
+ end
data/zerial.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'zerial/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zerial"
8
+ spec.version = Zerial::VERSION
9
+ spec.authors = ["Jell"]
10
+ spec.email = ["jean-louis@jawaninja.com"]
11
+ spec.summary = %q{Simple JSON serializer}
12
+ spec.description = %q{Simple JSON serializer/parser}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport", "~> 4.1"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "money"
27
+ spec.add_development_dependency "immutable_record"
28
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zerial
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
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
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: money
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::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'
83
+ - !ruby/object:Gem::Dependency
84
+ name: immutable_record
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Simple JSON serializer/parser
98
+ email:
99
+ - jean-louis@jawaninja.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/zerial.rb
111
+ - lib/zerial/base_serializer.rb
112
+ - lib/zerial/big_decimal_serializer.rb
113
+ - lib/zerial/collection_serializer.rb
114
+ - lib/zerial/date_serializer.rb
115
+ - lib/zerial/default_serializer.rb
116
+ - lib/zerial/immutable_record_serializer.rb
117
+ - lib/zerial/money_serializer.rb
118
+ - lib/zerial/nil_serializer.rb
119
+ - lib/zerial/timestamp_serializer.rb
120
+ - lib/zerial/version.rb
121
+ - spec/lib/zerial_spec.rb
122
+ - spec/spec_helper.rb
123
+ - zerial.gemspec
124
+ homepage: ''
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.2.2
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Simple JSON serializer
148
+ test_files:
149
+ - spec/lib/zerial_spec.rb
150
+ - spec/spec_helper.rb
151
+ has_rdoc: