jsonizer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 475471c240795e2d040c4bb43f97cba7d51852a2
4
+ data.tar.gz: c90ad644ce4695df7b025ae5f851ed757131a7d0
5
+ SHA512:
6
+ metadata.gz: 65bb931630c2f6ca7141b085013164cdf051060123021d342656807daf9df339eb3953af2c9298b7255ba0968aca37969f03c449ea811b084c7c5900eada4771
7
+ data.tar.gz: d585c241c0228a6b322c2f7a838a13a54ccf40d58b803f93521dc26b1d968895b4923cecc06b24a6711ae7294d239cfc952d98849ea820e9205c19b978f4aa63
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jsonizer.gemspec
4
+ gemspec
5
+ gem 'coveralls', group: :test, require: false
@@ -0,0 +1,60 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ jsonizer (0.1.0)
5
+ json
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ adamantium (0.0.7)
11
+ backports (~> 3.0, >= 3.0.3)
12
+ ice_nine (~> 0.7.0)
13
+ backports (3.1.1)
14
+ colorize (0.5.8)
15
+ coveralls (0.6.3)
16
+ colorize
17
+ multi_json (~> 1.3)
18
+ rest-client
19
+ simplecov (>= 0.7)
20
+ thor
21
+ diff-lcs (1.2.2)
22
+ equalizer (0.0.5)
23
+ adamantium (~> 0.0.6)
24
+ backports (~> 3.0, >= 3.0.3)
25
+ ice_nine (0.7.0)
26
+ json (1.7.7)
27
+ mime-types (1.22)
28
+ multi_json (1.7.2)
29
+ rake (10.0.4)
30
+ redcarpet (2.2.2)
31
+ rest-client (1.6.7)
32
+ mime-types (>= 1.16)
33
+ rspec (2.13.0)
34
+ rspec-core (~> 2.13.0)
35
+ rspec-expectations (~> 2.13.0)
36
+ rspec-mocks (~> 2.13.0)
37
+ rspec-core (2.13.1)
38
+ rspec-expectations (2.13.0)
39
+ diff-lcs (>= 1.1.3, < 2.0)
40
+ rspec-mocks (2.13.0)
41
+ simplecov (0.7.1)
42
+ multi_json (~> 1.0)
43
+ simplecov-html (~> 0.7.1)
44
+ simplecov-html (0.7.1)
45
+ thor (0.18.1)
46
+ yard (0.8.5.2)
47
+
48
+ PLATFORMS
49
+ ruby
50
+
51
+ DEPENDENCIES
52
+ bundler
53
+ coveralls
54
+ equalizer
55
+ jsonizer!
56
+ rake
57
+ redcarpet
58
+ rspec
59
+ simplecov
60
+ yard
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Simon Kohlmeyer
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.
@@ -0,0 +1,86 @@
1
+ [![Build Status](https://secure.travis-ci.org/voidus/jsonizer.png?branch=master)][travis]
2
+ [![Code Climate](https://codeclimate.com/github/voidus/jsonizer.png)][codeclimate]
3
+ [![Coverage Status](https://coveralls.io/repos/voidus/jsonizer/badge.png?branch=master)][coveralls]
4
+ [![Dependencied](https://gemnasium.com/voidus/jsonizer.png)][dependencies]
5
+
6
+ [travis]: https://travis-ci.org/voidus/jsonizer
7
+ [codeclimate]: https://codeclimate.com/github/voidus/jsonizer
8
+ [coveralls]: https://coveralls.io/r/voidus/jsonizer
9
+ [dependencies]: https://gemnasium.com/voidus/jsonizer
10
+
11
+ # Jsonizer
12
+
13
+ Module to easily provide json serialization
14
+
15
+ It was structurally inspired by http://github.com/dkubb/equalizer
16
+
17
+ ## Installation
18
+
19
+ Using [Bundler](http://gembundler.com) (recommended)
20
+
21
+ * Add `gem 'jsonizer'` to your Gemfile
22
+ * Run `bundle install`
23
+
24
+ Using rubygems
25
+
26
+ * Run `gem install jsonizer`
27
+
28
+ Installing from git
29
+
30
+ * Clone the repository using `git clone git://github.com/voidus/jsonizer`
31
+ * Enter the directory using `cd jsonizer`
32
+ * Build and install the gem with `rake install`
33
+
34
+ ## Usage
35
+
36
+ ```ruby
37
+ class TransferObject
38
+ include Jsonizer.new :operation_id, :parameter
39
+
40
+ attr_reader :operation_id, :parameter
41
+ attr_accessor :calculation_strategy
42
+
43
+ def initialize operation_id, parameter, transient = "default transient attribute"
44
+ @operation_id = operation_id
45
+ @parameter = parameter
46
+ @transient_attribute = transient
47
+ end
48
+ end
49
+
50
+ TestTransferObject.new("add", [15, 20], 'transient').to_json nil
51
+ # {"json_class":"TestTransferObject","operation_id":"add","parameter":[15,20]}
52
+
53
+ JSON.dump(TestTransferObject.new("add", [15, 20], 'transient))
54
+ # {"json_class":"TestTransferObject","operation_id":"add","parameter":[15,20]}
55
+
56
+ JSON.load(JSON.dump(TestTransferObject.new("add", [15, 20], 'transient))).inspect
57
+ # #<TestTransferObject:0x000000018ae188
58
+ # @operation_id="add",
59
+ # @parameter=[15, 20],
60
+ # @transient_attribute="default transient attribute">
61
+
62
+ JSON.dump(TestTransferObject.new("nested", TestTransferObject.new("op", "param")))
63
+ # {"json_class":"TestTransferObject",
64
+ # "operation_id":"nested",
65
+ # "parameter":
66
+ # {"json_class":"TestTransferObject",
67
+ # "operation_id":"op",
68
+ # "parameter":"param"}}
69
+
70
+ JSON.load(JSON.dump(TestTransferObject.new("nested", TestTransferObject.new("op", "param"))))
71
+ # <TestTransferObject:0x0000000268d308
72
+ # @operation_id="nested",
73
+ # @parameter=#<TestTransferObject:0x0000000268d5b0
74
+ # @operation_id="op",
75
+ # @parameter="param",
76
+ # @transient_attribute="default transient attribute">,
77
+ # @transient_attribute="default transient attribute">
78
+ ```
79
+
80
+ ## Contributing
81
+
82
+ 1. Fork it
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your tests and changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,2 @@
1
+ require 'jsonizer/version'
2
+ require 'jsonizer/jsonizer'
@@ -0,0 +1,66 @@
1
+ require 'json'
2
+
3
+ # Defines all methods neccessary for JSON encoding through JSON[], JSON.dump and #to_json
4
+ class Jsonizer < Module
5
+
6
+ # Initialize a Jsonizer to use the given attributes
7
+ #
8
+ # They will be used to define as_json, to_json and self.json_create
9
+ #
10
+ # @param [Array<Symbol>] attributes
11
+ # @return [undefined]
12
+ def initialize *attributes
13
+ @attributes = attributes
14
+ define_as_json
15
+ define_to_json
16
+ end
17
+
18
+ # Include the equalizer instance into mod
19
+ #
20
+ # @param mod [Module]
21
+ # @return [undefined]
22
+ def included mod
23
+ raise ArgumentError, "Cannot jsonize anonymous classes. They cannot be restored." unless mod.name
24
+ mod.extend(class_methods_module)
25
+ end
26
+
27
+ private
28
+
29
+ # Define #as_json using @attributes that returns a hash that will be converted to json
30
+ #
31
+ # @return [undefined]
32
+ def define_as_json
33
+ attributes = @attributes
34
+ define_method :as_json do |*|
35
+ hash = {'json_class' => self.class.name}
36
+ attributes.each do |attribute|
37
+ hash[attribute.to_s] = self.send(attribute.to_sym)
38
+ end
39
+ hash
40
+ end
41
+ end
42
+
43
+ # Define to_json that converts #as_json to a json string
44
+ #
45
+ # @return [undefined]
46
+ def define_to_json
47
+ define_method :to_json do |*|
48
+ as_json.to_json
49
+ end
50
+ end
51
+
52
+ # Creates a module that defines #json_create using the given attributes.
53
+ #
54
+ # It will pass them as positional attributes to #new in the same order that they were given in Jsonizer.new
55
+ #
56
+ # @return [undefined]
57
+ def class_methods_module
58
+ attributes = @attributes
59
+ Module.new do
60
+ define_method :json_create do |values|
61
+ params = attributes.map {|attribute| values[attribute.to_s]}
62
+ new(*params)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ class Jsonizer < Module
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+ require 'json'
3
+ require 'equalizer'
4
+
5
+ describe Jsonizer do
6
+ it 'should have a version number' do
7
+ Jsonizer::VERSION.should_not be_nil
8
+ end
9
+
10
+ context 'When included without parameters' do
11
+ class NoParamIncludeClass
12
+ include Jsonizer.new
13
+ end
14
+
15
+ subject {NoParamIncludeClass.new}
16
+
17
+ it {should respond_to :to_json}
18
+ its(:class) {should respond_to :json_create}
19
+ it {should be_same_class_after_json}
20
+ end
21
+
22
+ context "With a jsonized and an unrelated attribute" do
23
+ class WithUnrelated
24
+ include Jsonizer.new :jsonized_attribute
25
+ include Equalizer.new :jsonized_attribute, :unrelated_attribute
26
+ attr_reader :jsonized_attribute, :unrelated_attribute
27
+
28
+ def initialize jsonized_attribute, unrelated_attribute = 'default_unrelated_attribute'
29
+ @jsonized_attribute = jsonized_attribute
30
+ @unrelated_attribute = unrelated_attribute
31
+ end
32
+ end
33
+
34
+ let(:original) {WithUnrelated.new 'original_jsonized_attribute', 'original_unrelated_attribute'}
35
+ subject {original}
36
+ it {should respond_to :to_json}
37
+ its(:class) {should respond_to :json_create}
38
+ it {should be_same_class_after_json}
39
+ it {should_not be_eql_after_json}
40
+
41
+ describe "after json conversion" do
42
+ subject {JSON.load(JSON.dump(original))}
43
+ its(:jsonized_attribute) {should eql original.jsonized_attribute}
44
+ its(:unrelated_attribute) {should_not eql original.unrelated_attribute}
45
+ its(:unrelated_attribute) {should eql 'default_unrelated_attribute'}
46
+ end
47
+ end
48
+
49
+ context "With multiple jsonized and unrelated attribute" do
50
+ class Multiple
51
+ include Jsonizer.new :jsonized_a, :jsonized_b
52
+ include Equalizer.new :jsonized_a, :unrelated_attribute
53
+ attr_reader :jsonized_a, :jsonized_b, :unrelated_attribute
54
+
55
+ def initialize jsonized_a, jsonized_b = 14, unrelated_attribute = 'default_unrelated_attribute'
56
+ @jsonized_a = jsonized_a
57
+ @jsonized_b = jsonized_a
58
+ @unrelated_attribute = unrelated_attribute
59
+ end
60
+ end
61
+
62
+ let(:original) {Multiple.new 'original_jsonized_a', 1024, 'original_unrelated_attribute'}
63
+ subject {original}
64
+
65
+ it {should respond_to :to_json}
66
+ its(:class) {should respond_to :json_create}
67
+ it {should be_same_class_after_json}
68
+ it {should_not be_eql_after_json}
69
+
70
+ describe "after json conversion" do
71
+ subject {JSON.load(JSON.dump(original))}
72
+ its(:jsonized_a) {should eql original.jsonized_a}
73
+ its(:jsonized_b) {should eql original.jsonized_b}
74
+ its(:unrelated_attribute) {should_not eql original.unrelated_attribute}
75
+ its(:unrelated_attribute) {should eql 'default_unrelated_attribute'}
76
+ end
77
+ end
78
+
79
+ context "Anonymous classes" do
80
+ it "should throw when included" do
81
+ expect {Class.new do include Jsonizer.new end}.to raise_error ArgumentError
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,46 @@
1
+ def to_json_and_back obj
2
+ JSON.load(JSON.dump(obj))
3
+ end
4
+
5
+ RSpec::Matchers.define :be_same_class_after_json do
6
+ match do |subject|
7
+ to_json_and_back(subject).class.equal? subject.class
8
+ end
9
+
10
+ failure_message_for_should do |subject|
11
+ actual = to_json_and_back subject
12
+ <<HERE
13
+ expected #{subject.inspect} to be the same class after json dump-load
14
+ got class #{actual.class} instead
15
+ value: #{actual.inspect}
16
+ HERE
17
+ end
18
+
19
+ description do
20
+ "be the same class after JSON.dump and JSON.load"
21
+ end
22
+ end
23
+
24
+ RSpec::Matchers.define :be_eql_after_json do
25
+ match do |subject|
26
+ to_json_and_back(subject).eql? subject
27
+ end
28
+
29
+ failure_message_for_should do |subject|
30
+ <<HERE
31
+ expected #{subject.inspect} to be eql after json dump-load
32
+ got #{to_json_and_back(subject).inspect} instead"
33
+ HERE
34
+ end
35
+
36
+ failure_message_for_should_not do |subject|
37
+ <<HERE
38
+ expected #{subject.inspect} not to be eql after json dump-load
39
+ (got #{to_json_and_back(subject).inspect} after dump/load)"
40
+ HERE
41
+ end
42
+
43
+ description do
44
+ "be the eql after JSON.dump and JSON.load"
45
+ end
46
+ end
@@ -0,0 +1,16 @@
1
+ unless ENV['autotest']
2
+ require 'simplecov'
3
+ require 'coveralls'
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
8
+ SimpleCov.start do
9
+ add_filter '/spec/'
10
+ end
11
+ end
12
+
13
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
14
+ require 'jsonizer'
15
+
16
+ require_relative 'matchers'
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Simon Kohlmeyer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
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: simplecov
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: equalizer
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
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: redcarpet
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: |
126
+ Module to easily provide json serialization
127
+ Structurally inspired by http://github.com/dkubb/equalizer
128
+ email:
129
+ - simon.kohlmeyer@gmail.com
130
+ executables: []
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - lib/jsonizer.rb
135
+ - lib/jsonizer/jsonizer.rb
136
+ - lib/jsonizer/version.rb
137
+ - spec/jsonizer_spec.rb
138
+ - spec/matchers.rb
139
+ - spec/spec_helper.rb
140
+ - Gemfile
141
+ - Gemfile.lock
142
+ - LICENSE.txt
143
+ - README.md
144
+ - Rakefile
145
+ homepage: http://github.com/voidus/jsonizer
146
+ licenses:
147
+ - MIT
148
+ metadata: {}
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - '>='
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.0.0.rc.2
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: Module to easily provide json serialization
169
+ test_files:
170
+ - spec/jsonizer_spec.rb
171
+ - spec/matchers.rb
172
+ - spec/spec_helper.rb
173
+ has_rdoc: