serialize_yaml2json 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: 143579bcbdbf4d6e20fa4a3f2fb743efc656d4bc
4
+ data.tar.gz: d896eb4e0ec4854ac8d463cba0ff302cfcae5e00
5
+ SHA512:
6
+ metadata.gz: 74598265a4016ee1f98536dc318edd3de903523bf0a7c67d803c7ab305737f3106cb7ce6c713e9c6032cfa17b8d1fb0f665711b86f127f8db3f508260bdce629
7
+ data.tar.gz: 6ed39e3f2b0682ecbba72bcea3ae6e17c4f22171d85217de63896456b22f7fb984b503459154df4edffac30feefa1555d0409b18d13b21181bef06a7453ec703
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in serialize_yaml2json.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Supreme Golf
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,62 @@
1
+ # SerializeYAML2JSON
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/serialize_yaml2json.png)](http://badge.fury.io/rb/serialize_yaml2json)
4
+ [![Code Climate](https://codeclimate.com/github/supremegolf/serialize_yaml2json.png)](https://codeclimate.com/github/supremegolf/serialize_yaml2json)
5
+ [![Build Status](https://travis-ci.org/supremegolf/serialize_yaml2json.svg?branch=master)](https://travis-ci.org/supremegolf/serialize_yaml2json)
6
+
7
+ ActiveRecord::Coder to transparently transition serialized attributes from YAML to JSON. In a recent
8
+ test creating/reading serialized records JSON outperfoms YAML by a wide margin.
9
+
10
+ Serializing the entire contents of the `ENV` hash 50,000 times:
11
+
12
+ user system total real
13
+ yaml: 77.390000 9.220000 86.610000 (117.776747)
14
+ json: 52.870000 8.240000 61.110000 ( 89.297786)
15
+
16
+ Deserializing those same 50,000 records:
17
+
18
+ user system total real
19
+ yaml: 10.120000 0.380000 10.500000 ( 15.818386)
20
+ json: 2.840000 0.100000 2.940000 ( 7.896888)
21
+
22
+ More information available [here](http://pjkh.com/articles/postgresql-json-vs-rails-serialize/).
23
+
24
+ ## Installation
25
+
26
+ Add this line to your application's Gemfile:
27
+
28
+ ```ruby
29
+ gem 'serialize_yaml2json'
30
+ ```
31
+
32
+ And then execute:
33
+
34
+ $ bundle
35
+
36
+ Or install it yourself as:
37
+
38
+ $ gem install serialize_yaml2json
39
+
40
+ ## Usage
41
+
42
+ Update your models to use this Coder.
43
+
44
+ class Widget < ActiveRecord::Base
45
+ # serialize :data
46
+ serialize :data, SerializeYAML2JSON::Coder
47
+ end
48
+
49
+ # NOTE: 'data' will now be a hash with indifferent access.
50
+ # This is because JSON doesn't understand symbols and stringifies
51
+ # all keys. YAML could do either. This is necessary to support
52
+ # existing access to the attributes.
53
+
54
+
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it ( https://github.com/supremegolf/serialize_yaml2json/fork )
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -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,22 @@
1
+ require 'serialize_yaml2json/version'
2
+ require 'active_record'
3
+ require 'active_support/core_ext/hash/indifferent_access'
4
+
5
+ module SerializeYAML2JSON
6
+ class Coder
7
+
8
+ def self.dump(obj)
9
+ ::ActiveRecord::Coders::JSON.dump(obj)
10
+ end
11
+
12
+ def self.load(yaml_or_json)
13
+ if yaml_or_json.is_a?(String) && yaml_or_json =~ /^---/
14
+ obj = ::ActiveRecord::Coders::YAMLColumn.new.load(yaml_or_json)
15
+ else
16
+ obj = ::ActiveRecord::Coders::JSON.load(yaml_or_json)
17
+ end
18
+ obj.with_indifferent_access
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module SerializeYAML2JSON
2
+ VERSION = "0.0.1"
3
+ end
@@ -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 'serialize_yaml2json/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "serialize_yaml2json"
8
+ spec.version = SerializeYAML2JSON::VERSION
9
+ spec.authors = ["Philip Hallstrom"]
10
+ spec.email = ["philip@supremegolf.com.com"]
11
+ spec.summary = %q{ActiveRecord::Coder to transition serialized attributes from YAML to JSON.}
12
+ spec.description = %q{}
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 'activerecord', '~> 4.1'
22
+ spec.add_dependency 'activesupport', '~> 4.1'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.7'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec'
27
+ spec.add_development_dependency 'sqlite3'
28
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe SerializeYAML2JSON do
4
+ subject { w = Widget.create!(data: {one:1}); w.reload; w }
5
+
6
+ it "dumps JSON" do
7
+ expect(subject.read_attribute_before_type_cast(:data).value).to eq '{"one":1}'
8
+ end
9
+
10
+ it "loads JSON" do
11
+ expect(subject.read_attribute_before_type_cast(:data).value).to eq '{"one":1}'
12
+ expect(subject.data).to be_a Hash
13
+ expect(subject.data['one']).to eq 1
14
+ expect(subject.data[:one]).to eq 1
15
+ end
16
+
17
+ describe "with an existing YAML record" do
18
+ before do
19
+ subject.class.connection.update("UPDATE widgets SET data = '---\n:one: 1\n'")
20
+ subject.reload
21
+ end
22
+
23
+ it "loads YAML" do
24
+ expect(subject.read_attribute_before_type_cast(:data).value).to eq "---\n:one: 1\n"
25
+ expect(subject.data).to be_a Hash
26
+ expect(subject.data['one']).to eq 1
27
+ expect(subject.data[:one]).to eq 1
28
+ end
29
+
30
+ it "dumps JSON" do
31
+ subject.data = {two:2}
32
+ subject.save!
33
+ subject.reload
34
+ expect(subject.read_attribute_before_type_cast(:data).value).to eq '{"two":2}'
35
+ expect(subject.data).to be_a Hash
36
+ expect(subject.data['two']).to eq 2
37
+ expect(subject.data[:two]).to eq 2
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -0,0 +1,27 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'serialize_yaml2json'
3
+
4
+ RSpec.configure do |config|
5
+ config.filter_run focus: true
6
+ config.run_all_when_everything_filtered = true
7
+
8
+ config.before(:suite) do
9
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
10
+
11
+ silence_stream(STDOUT) do
12
+ ActiveRecord::Schema.define(:version => 1) do
13
+ create_table :widgets do |t|
14
+ t.column :data, :text
15
+ end
16
+ end
17
+ end
18
+
19
+ class Widget < ActiveRecord::Base
20
+ self.table_name = 'widgets'
21
+ serialize :data, SerializeYAML2JSON::Coder
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe SerializeYAML2JSON do
4
+
5
+ it 'has a version number' do
6
+ expect(SerializeYAML2JSON::VERSION).not_to be nil
7
+ end
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serialize_yaml2json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Philip Hallstrom
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
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: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
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: sqlite3
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: ''
98
+ email:
99
+ - philip@supremegolf.com.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".travis.yml"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/serialize_yaml2json.rb
111
+ - lib/serialize_yaml2json/version.rb
112
+ - serialize_yaml2json.gemspec
113
+ - spec/serialize_yaml2json_spec.rb
114
+ - spec/spec_helper.rb
115
+ - spec/version_spec.rb
116
+ homepage: ''
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.2.2
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: ActiveRecord::Coder to transition serialized attributes from YAML to JSON.
140
+ test_files:
141
+ - spec/serialize_yaml2json_spec.rb
142
+ - spec/spec_helper.rb
143
+ - spec/version_spec.rb