ar_json_serialize 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: 0ec1e50466ff99d7a49ad96f5281cea64117372b
4
+ data.tar.gz: 465eef3bfe1260f81b7bbf4ec7ed21539e97f788
5
+ SHA512:
6
+ metadata.gz: 4e4ef817eb2bbfcfff4e0ffa14f7332beac5ca8c825cffdab367705bab52f2eb1f6498b36777fef01e4ed47afd55a89b7d0652055dd358745e1236c69f90dadb
7
+ data.tar.gz: ba05f35f23ae599884bc466c9ca5b5500f363a73a4e1e0ecbd30cfc395165bae4396ce5f3b23363ab3a307276e174de13ab8185e69cc93a59b8a43bb9df87191
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
+ .idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --fail-fast
3
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'coveralls', :require => false
5
+ gem 'rspec', '>= 2.14'
6
+ gem 'simplecov', :require => false
7
+ end
8
+
9
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alexander Simonov
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,52 @@
1
+ # ArJsonSerialize[![Coverage Status](https://coveralls.io/repos/phenomena/ar_json_serialize/badge.png?branch=master)](https://coveralls.io/r/phenomena/ar_json_serialize?branch=master)[![Build Status](https://travis-ci.org/phenomena/ar_json_serialize.png?branch=master)](https://travis-ci.org/phenomena/ar_json_serialize)
2
+ ActiveRecord JSON serializer
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'ar_json_serialize'
9
+
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install ar_json_serialize
17
+
18
+ ## Usage
19
+
20
+ You need to have some `text` field for storing some specific data in Hash/Array/etc inside you model.
21
+
22
+ Add next string in your ActiveRecord model:
23
+
24
+ ```ruby
25
+ class Promo < ActiveRecord::Base
26
+ json_serialize :test_column
27
+ end
28
+ ```
29
+
30
+ You can set value of this column with any object.
31
+
32
+ ```ruby
33
+ promo = ::Promo.new
34
+ promo.test_column = {'key1' => 'value1'}
35
+ promo.save!
36
+ ```
37
+
38
+ And use it with full power of Hashie!
39
+
40
+ ```ruby
41
+ promo = ::Promo.last
42
+ puts "Our value is #{promo.test_column.key1}"
43
+ ```
44
+
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ar_json_serialize/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ar_json_serialize"
8
+ spec.version = ArJsonSerialize::VERSION
9
+ spec.authors = ["Alexander Simonov"]
10
+ spec.email = ["alex@simonov.me"]
11
+ spec.description = %q{ActiveRecord JSON serializer}
12
+ spec.summary = %q{ActiveRecord JSON serializer}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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 'multi_json', '~> 1.0'
22
+ spec.add_dependency 'hashie', '~> 2.0.5'
23
+ spec.add_dependency 'activerecord', '>= 3.2.13', '< 5'
24
+ spec.add_development_dependency 'bundler', '~> 1.3'
25
+ spec.add_development_dependency 'rake'
26
+ end
@@ -0,0 +1,9 @@
1
+ require 'multi_json'
2
+ require 'active_record'
3
+ require 'hashie/mash'
4
+ require 'ar_json_serialize/version'
5
+ require 'ar_json_serialize/serializer'
6
+ require 'ar_json_serialize/activerecord_ext'
7
+
8
+ module ArJsonSerialize
9
+ end
@@ -0,0 +1,8 @@
1
+ module ArJsonSerialize
2
+ module ActiveRecordExt
3
+ def json_serialize(column)
4
+ self.serialize column, ::ArJsonSerialize::Serializer
5
+ end
6
+ end
7
+ end
8
+ ::ActiveRecord::Base.send :extend, ::ArJsonSerialize::ActiveRecordExt
@@ -0,0 +1,31 @@
1
+ module ArJsonSerialize
2
+ module Serializer
3
+ extend self
4
+
5
+ def load(s)
6
+ if s.present?
7
+ result = ::MultiJson.load(s)
8
+ case result
9
+ when ::Hash
10
+ ::Hashie::Mash.new(result)
11
+ when ::Array
12
+ result.map do |item|
13
+ if item.is_a?(::Hash)
14
+ ::Hashie::Mash.new(item)
15
+ else
16
+ item
17
+ end
18
+ end
19
+ else
20
+ result
21
+ end
22
+ else
23
+ ''
24
+ end
25
+ end
26
+
27
+ def dump(s)
28
+ ::MultiJson.dump(s)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module ArJsonSerialize
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+ describe ::ArJsonSerialize::ActiveRecordExt do
3
+ it 'should extend ActiveRecord::Base' do
4
+ ::ActiveRecord::Base.should respond_to(:json_serialize)
5
+ end
6
+
7
+ it 'should call ActiveRecord::Base.serialize' do
8
+ ::ActiveRecord::Base.should_receive(:serialize).with('column', ::ArJsonSerialize::Serializer)
9
+ ::ActiveRecord::Base.json_serialize('column')
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+ describe ArJsonSerialize do
3
+
4
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+ describe ::ArJsonSerialize::Serializer do
3
+ %w(dump load).each do |k|
4
+ it { should respond_to(k.to_sym) }
5
+ end
6
+
7
+ context '#load' do
8
+ it 'should call MultiJson.load' do
9
+ ::MultiJson.should_receive(:load).with('string')
10
+ subject.load('string')
11
+ end
12
+
13
+ [nil, ''].each do |k|
14
+ it "should not call MultiJson.load if string is #{k.inspect}" do
15
+ ::MultiJson.should_not_receive(:load)
16
+ subject.load(k)
17
+ end
18
+
19
+ it "should return empty if string is #{k.inspect}" do
20
+ subject.load(k).should == ''
21
+ end
22
+
23
+ end
24
+
25
+ context 'content is' do
26
+ subject { ::ArJsonSerialize::Serializer.load(data) }
27
+
28
+ context 'String "foo"' do
29
+ let(:data) { '"foo"' }
30
+ it { should be_a(::String) }
31
+ it { should == 'foo' }
32
+ end
33
+
34
+ context 'Hash {"key1":"value1"}' do
35
+ let(:data) { '{"key1":"value1"}' }
36
+ it { should be_a(::Hashie::Mash) }
37
+ it { should == {'key1' => 'value1'} }
38
+ its(:key1) { should == 'value1' }
39
+ end
40
+
41
+ context 'Array' do
42
+ context '["1", "2", "3"]' do
43
+ let(:data) { '["1","2","3"]' }
44
+ it { should be_a(::Array) }
45
+ it { should == %w(1 2 3)}
46
+ end
47
+
48
+ context '["1","2","3",{"key1":"value1"}]' do
49
+ let(:data) { '["1","2","3",{"key1":"value1"}]' }
50
+ it { should be_a(::Array) }
51
+ it { should == ['1', '2', '3', {"key1"=>"value1"}] }
52
+ its(:last) { should be_a(::Hashie::Mash) }
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+
59
+ end
60
+
61
+ context '#dump' do
62
+ it 'should call MultiJson.dump' do
63
+ ::MultiJson.should_receive(:dump).with('foo')
64
+ subject.dump('foo')
65
+ end
66
+ end
67
+
68
+ end
@@ -0,0 +1,15 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require 'coveralls'
8
+ Coveralls.wear!
9
+ require 'ar_json_serialize'
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+ config.order = 'random'
15
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar_json_serialize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Simonov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: multi_json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hashie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.5
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 2.0.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 3.2.13
48
+ - - <
49
+ - !ruby/object:Gem::Version
50
+ version: '5'
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: 3.2.13
58
+ - - <
59
+ - !ruby/object:Gem::Version
60
+ version: '5'
61
+ - !ruby/object:Gem::Dependency
62
+ name: bundler
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: '1.3'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ version: '1.3'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ description: ActiveRecord JSON serializer
90
+ email:
91
+ - alex@simonov.me
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - .gitignore
97
+ - .rspec
98
+ - .travis.yml
99
+ - Gemfile
100
+ - LICENSE.txt
101
+ - README.md
102
+ - Rakefile
103
+ - ar_json_serialize.gemspec
104
+ - lib/ar_json_serialize.rb
105
+ - lib/ar_json_serialize/activerecord_ext.rb
106
+ - lib/ar_json_serialize/serializer.rb
107
+ - lib/ar_json_serialize/version.rb
108
+ - spec/activerecord_ext_spec.rb
109
+ - spec/ar_json_serializer_spec.rb
110
+ - spec/serializer_spec.rb
111
+ - spec/spec_helper.rb
112
+ homepage: ''
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.0.3
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: ActiveRecord JSON serializer
136
+ test_files:
137
+ - spec/activerecord_ext_spec.rb
138
+ - spec/ar_json_serializer_spec.rb
139
+ - spec/serializer_spec.rb
140
+ - spec/spec_helper.rb