json_dumper 0.1.0

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: 79357d5f4525bea45352f2a0a4d12e256099254f
4
+ data.tar.gz: 3e6a651e6eadd83dbde1c5086580dca624e4cd47
5
+ SHA512:
6
+ metadata.gz: b67062a0b892a32951eb9b53749b2ffc722829bf6d917ae8ff6944710e42121882f720c99f9820bf6c333a9b6777d7e9ee261994f72f149394f74fdfe0462c2c
7
+ data.tar.gz: 52290d5cb95766106bf0fbc290b3d13459dec136dc277a1535060cd5240f98d4fccda6fd671f9060102fb6571f53de500845e9a21af0b0947b509e12a86f77b1
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.14.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at dimanrussian@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in json_dumper.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 therusskiy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # JsonDumper
2
+
3
+ Serialize Ruby into JSON.
4
+
5
+ This gem is intended to
6
+ * help serialize Ruby objects and ActiveRecord objects into json
7
+ * help organize your code
8
+ * solve N+1 query problem
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'json_dumper'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install json_dumper
25
+
26
+ ## Usage
27
+
28
+ TODO: Write usage instructions here
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
+
34
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/therusskiy/json_dumper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
39
+
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
44
+
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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "json_dumper"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'json_dumper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "json_dumper"
8
+ spec.version = JsonDumper::VERSION
9
+ spec.authors = ["therusskiy"]
10
+ spec.email = ["dimanrussian@gmail.com"]
11
+
12
+ spec.summary = %q{Serialize ruby objects for javascript consumption}
13
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = "https://www.github.com/therusskiy/json_dumper"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_runtime_dependency "activerecord"
34
+ spec.add_development_dependency "bundler", "~> 1.13"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "rspec", "~> 3.0"
37
+ spec.add_development_dependency "gon"
38
+ end
@@ -0,0 +1,68 @@
1
+ module JsonDumper
2
+ class Base
3
+ attr_accessor :entity, :return_nils
4
+
5
+ def initialize(entity = nil)
6
+ self.return_nils = entity.nil?
7
+ self.entity = entity
8
+ end
9
+
10
+ def self.method_missing(name, *args, &block)
11
+ name_sym = name
12
+ name = name.to_s
13
+ value = args[0]
14
+ if name.start_with?('fetch_')
15
+ return Delayed.new(name.gsub('fetch_', ''), value, args[1..-1], self)
16
+ end
17
+ if instance.respond_to?(name)
18
+ if value.respond_to?(:each) && !value.respond_to?(:each_pair)
19
+ value.map do |ent|
20
+ new_dumper = new(ent)
21
+ if new_dumper.return_nils
22
+ return nil
23
+ end
24
+ result = DumperHash.new(new_dumper.send(name, *(args[1..-1]), &block))
25
+ preload_method_name = "#{name}_preload"
26
+ result.preload = instance.respond_to?(preload_method_name) ? instance.send(preload_method_name) : {}
27
+ result
28
+ end
29
+ elsif name.end_with?('_preload')
30
+ instance.send(name)
31
+ else
32
+ new_dumper = new(value)
33
+ if new_dumper.return_nils
34
+ return nil
35
+ end
36
+ result = DumperHash.new(new_dumper.send(name, *(args[1..-1]), &block))
37
+ preload_method_name = "#{name}_preload"
38
+ result.preload = instance.respond_to?(preload_method_name) ? instance.send(preload_method_name) : {}
39
+ result
40
+ end
41
+ elsif name.end_with?('_preload') && instance.respond_to?(name.gsub('_preload', ''))
42
+ return {}
43
+ else
44
+ super name_sym, *args, &block
45
+ end
46
+ end
47
+
48
+ def self.respond_to_missing?(method_name, _ = false)
49
+ new.respond_to? method_name
50
+ end
51
+
52
+ def method_missing(name, *args, &block)
53
+ if entity.respond_to? name
54
+ entity.send(name, *args, &block)
55
+ else
56
+ super name, *args, &block
57
+ end
58
+ end
59
+
60
+ def respond_to_missing?(method_name, _ = false)
61
+ entity.respond_to? method_name
62
+ end
63
+
64
+ def self.instance
65
+ @instance ||= new
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,12 @@
1
+ module JsonDumper
2
+ class Delayed
3
+ attr_accessor :method_name, :entity, :args, :klass
4
+
5
+ def initialize(method_name, entity, args, klass)
6
+ self.method_name = method_name
7
+ self.entity = entity
8
+ self.args = args
9
+ self.klass = klass
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ module JsonDumper
2
+ class DumperHash < Hash
3
+ attr_accessor :preload
4
+ def initialize(hash)
5
+ hash.each_pair do |k, v|
6
+ self[k] = v
7
+ end
8
+ end
9
+
10
+ def camel
11
+ JsonDumper::KeyTransformer.keys_to_camelcase(self)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,35 @@
1
+ module JsonDumper
2
+ module Helper
3
+ def render_dumper_json(hash)
4
+ render json: dumper_json(hash)
5
+ end
6
+
7
+ def dumper_json(hash)
8
+ result = hash.map do |k, v|
9
+ if v.is_a?(JsonDumper::Delayed)
10
+ [k, dumper_fetch(v, camelcase: false)]
11
+ else
12
+ [k, v]
13
+ end
14
+ end.to_h
15
+ KeyTransformer.keys_to_camelcase(result)
16
+ end
17
+
18
+ def dumper_fetch(delayed, camelcase: true)
19
+ preload_hash = delayed.klass.send("#{delayed.method_name}_preload")
20
+ preloader.preload(delayed.entity,preload_hash)
21
+ result = delayed.klass.send(delayed.method_name, delayed.entity, *delayed.args)
22
+ if camelcase
23
+ result = KeyTransformer.keys_to_camelcase(result)
24
+ end
25
+ result
26
+ end
27
+
28
+ ############ PRIVATE ##############
29
+ private
30
+
31
+ def preloader
32
+ ActiveRecord::Base::Preloader.new
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,46 @@
1
+ module JsonDumper
2
+ class KeyTransformer
3
+ def self.keys_to_camelcase(obj)
4
+ if array_like?(obj)
5
+ return keys_to_camelcase_array(obj)
6
+ end
7
+ result = obj.map do |k, v|
8
+ new_value = if v.respond_to?(:each_pair)
9
+ keys_to_camelcase(v)
10
+ elsif array_like?(v)
11
+ keys_to_camelcase_array(v)
12
+ else
13
+ v
14
+ end
15
+ new_key = if k.is_a?(String)
16
+ camelize(k)
17
+ elsif k.is_a?(Symbol)
18
+ camelize(k.to_s).to_sym
19
+ else
20
+ k
21
+ end
22
+ [new_key, new_value]
23
+ end.to_h
24
+ if defined?(HashWithIndifferentAccess) && obj.is_a?(HashWithIndifferentAccess)
25
+ result = HashWithIndifferentAccess.new(result)
26
+ end
27
+ result
28
+ end
29
+
30
+ def self.keys_to_camelcase_array(arr)
31
+ arr.map do |v|
32
+ v.respond_to?(:each_pair) ? keys_to_camelcase(v) : v
33
+ end
34
+ end
35
+
36
+ def self.array_like?(obj)
37
+ obj.is_a?(Array) || (defined?(ActiveRecord::Relation) && obj.is_a?(ActiveRecord::Relation))
38
+ end
39
+
40
+ def self.camelize(string)
41
+ string.split('_').each_with_index.map do |word, index|
42
+ index == 0 ? word : word.capitalize
43
+ end.join
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,13 @@
1
+ if defined?(Gon)
2
+ # Monkey patching
3
+ class Gon
4
+ class << self
5
+ def set_variable(name, value)
6
+ if value.is_a?(::JsonDumper::Delayed)
7
+ value = Class.new.extend(::JsonDumper::Helper).dumper_fetch(value)
8
+ end
9
+ current_gon.gon[::JsonDumper::KeyTransformer.camelize(name)] = value
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module JsonDumper
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,11 @@
1
+ require "json_dumper/version"
2
+ require "json_dumper/key_transformer"
3
+ require "json_dumper/dumper_hash"
4
+ require "json_dumper/delayed"
5
+ require "json_dumper/base"
6
+ require "json_dumper/helper"
7
+ require "json_dumper/patches/gon"
8
+
9
+ module JsonDumper
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_dumper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - therusskiy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-13 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: '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: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.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: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: gon
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
+ description:
84
+ email:
85
+ - dimanrussian@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - json_dumper.gemspec
101
+ - lib/json_dumper.rb
102
+ - lib/json_dumper/base.rb
103
+ - lib/json_dumper/delayed.rb
104
+ - lib/json_dumper/dumper_hash.rb
105
+ - lib/json_dumper/helper.rb
106
+ - lib/json_dumper/key_transformer.rb
107
+ - lib/json_dumper/patches/gon.rb
108
+ - lib/json_dumper/version.rb
109
+ homepage: https://www.github.com/therusskiy/json_dumper
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.6.10
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Serialize ruby objects for javascript consumption
133
+ test_files: []