dry_serialization 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fffe54a30bc7e0e3dcffda5ffd92c83362d98493bcd7d48e766867468f1a3cca
4
+ data.tar.gz: 9143b1bca8c2f5738f7bc9c3ad142f883405c69518c4cb8c17f89006e045fb8f
5
+ SHA512:
6
+ metadata.gz: f00eaa9aa4055e0de5e20eaceda000b8509dc1ee53b5032251be9c2691f988c36b39ef9a7ea51c56668339612bb054125bf053907d78fb727fcea08700ee3fd1
7
+ data.tar.gz: 5415bcd22ad6007f85b868da92dd5ae681884761dcc60f13c3758d4d80241d8806ced854dacd48e66591fe8f907e03687eb2ef5ed98a001c62b9ccaf90ddd330
@@ -0,0 +1,51 @@
1
+ # DrySerialization
2
+
3
+ ![AppVeyor](https://img.shields.io/appveyor/build/mikeyduece/dry_serialization?style=plastic)
4
+ ![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/mikeyduece/dry_serialization?style=plastic)
5
+ ![GitHub issues](https://img.shields.io/github/issues-raw/mikeyduece/dry_serialization?style=plastic)
6
+
7
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dry_serialization`. To experiment with that code, run `bin/console` for an interactive prompt.
8
+
9
+ TODO: Delete this and the text above, and describe your gem
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'dry_serialization'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install dry_serialization
26
+
27
+ ## Usage
28
+
29
+ - Install with rails generator
30
+ * currently supported serializers are `:blueprinter`
31
+
32
+
33
+ `rails g dry_serialization:<serializer_name>:install`
34
+
35
+
36
+
37
+
38
+
39
+ ## Development
40
+
41
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+
43
+ 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).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mikeyduece/dry_serialization.
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,34 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dry_serialization/version'
5
+ require 'dry_serialization'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'dry_serialization'
9
+ spec.version = DrySerialization::VERSION
10
+ spec.authors = ['Mike Heft']
11
+ spec.email = ['mikeheft@gmail.com']
12
+
13
+ spec.summary = %q{Drys up serialized returns by passing in object, serializer, and an options hash}
14
+ #spec.description = %q{TODO: Write a longer description or delete this line.}
15
+ #spec.homepage = 'TODO: Put your gem's website or public repo URL here.'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = Dir['lib/**/*', 'CHANGELOG.md', 'MIT-LICENSE', 'Rakefile', 'README.md', 'dry_serialization.gemspec']
19
+
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.17'
25
+ spec.add_development_dependency 'rspec', '~> 3.7'
26
+ spec.add_development_dependency 'factory_bot'
27
+ spec.add_development_dependency 'generator_spec'
28
+ spec.add_development_dependency 'pry'
29
+ spec.add_development_dependency 'rake'
30
+ spec.add_development_dependency 'activerecord'
31
+ spec.add_development_dependency 'sqlite3', '~> 1.4'
32
+ spec.add_development_dependency 'yard', '~> 0.9.11'
33
+ spec.add_development_dependency 'fast_jsonapi'
34
+ end
@@ -0,0 +1,7 @@
1
+ require "dry_serialization/version"
2
+ require "dry_serialization/blueprinter"
3
+ require "dry_serialization/fast_jsonapi"
4
+
5
+ module DrySerialization
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ module DrySerialization::Blueprinter
2
+
3
+ def serialized_resource(resource, blueprint, options = {})
4
+ JSON.parse(blueprint.render(resource, options), symbolize_names: true)
5
+ end
6
+
7
+ end
@@ -0,0 +1,12 @@
1
+ module DrySerialization::FastJsonapi
2
+
3
+ def serialized_resource(resource, serializer, options = {})
4
+ if resource.is_a?(ActiveRecord::Relation) || resource.is_a?(Array)
5
+ options[:is_collection] = true
6
+ else
7
+ options[:is_collection] = false
8
+ end
9
+
10
+ serializer.new(resource, options)
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module DrySerialization
2
+ VERSION = "0.3"
3
+ end
@@ -0,0 +1,53 @@
1
+ require 'rails/generators/base'
2
+
3
+ class BaseGenerator < Rails::Generators::Base
4
+ API_CONTROLLER_PATH = 'app/controllers/api_controller.rb'.freeze
5
+ SERIALIZERS = {
6
+ ams: 'ActiveModelSerializers',
7
+ blueprinter: 'Blueprinter',
8
+ fast_jsonapi: 'FastJsonapi'
9
+ }
10
+
11
+ def self.exit_on_failure?
12
+ true
13
+ end
14
+
15
+ private
16
+
17
+ # Removes other serialization gems, currently just AMS and FastJsonapi
18
+ def remove_other_supported_gems(*gems)
19
+ gems = [gems] unless gems.is_a?(Array)
20
+ return if gems.empty?
21
+
22
+ gems.each do |gem|
23
+ log_statement(gem, :yellow)
24
+ gsub_file('Gemfile', /^(\ngem #{gem.underscore})/, '')
25
+ end
26
+
27
+ end
28
+
29
+ # Adds an ApiController if one does not exist to ensure proper encapsulation,
30
+ # especially if in a Rails monolith with Rails HTML views as well
31
+ def copy_api_controller
32
+ puts set_color('Checking if an ApiController exists...', :cyan)
33
+
34
+ unless File.exists?('app/controllers/api_controller.rb')
35
+ puts set_color('ApiController not present, creating ApiController...', :cyan)
36
+ template 'api_controller.rb', API_CONTROLLER_PATH
37
+ end
38
+ end
39
+
40
+ def helper_include(serializer)
41
+ copy_api_controller
42
+ gsub_file(API_CONTROLLER_PATH, /^*(include DrySerialization::.*)\n/, '')
43
+ puts 'Adding include statement to ApiController'
44
+ insert_into_file(API_CONTROLLER_PATH,
45
+ "\n\tinclude DrySerialization::#{serializer}",
46
+ after: 'class ApiController < ActionController::API'
47
+ )
48
+ end
49
+
50
+ def log_statement(serializer, color = :green)
51
+ puts set_color("Removed #{serializer}", color)
52
+ end
53
+ end
@@ -0,0 +1,23 @@
1
+ require 'generators/base_generator'
2
+
3
+ module DrySerialization
4
+ module Blueprinter
5
+ class InstallGenerator < BaseGenerator
6
+ source_root File.expand_path("../../../templates", __FILE__)
7
+
8
+ # Add blueprinter gem to gemfile after dry_serialization declaration and bundles the newly declared gem
9
+ def install_blueprinter
10
+ remove_other_supported_gems(SERIALIZERS[:ams], SERIALIZERS[:fast_jsonapi])
11
+
12
+ puts "Installing #{SERIALIZERS[:blueprinter]}..."
13
+ insert_into_file('Gemfile',
14
+ "\ngem 'blueprinter'",
15
+ after: "gem 'dry_serialization', source: 'https://gem.fury.io/mikeyduece-gems/'")
16
+ run 'bundle install'
17
+
18
+ helper_include(SERIALIZERS[:blueprinter])
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ require 'generators/base_generator'
2
+
3
+ module DrySerialization
4
+ module FastJsonapi
5
+ class InstallGenerator < BaseGenerator
6
+ source_root File.expand_path("../../../templates", __FILE__)
7
+
8
+ # Add blueprinter gem to gemfile after dry_serialization declaration and bundles the newly declared gem
9
+ def install_blueprinter
10
+ remove_other_supported_gems(SERIALIZERS[:blueprinter], SERIALIZERS[:ams])
11
+
12
+ puts "Installing #{SERIALIZERS[:blueprinter]}..."
13
+ insert_into_file('Gemfile',
14
+ "\ngem 'fast_jsonapi'",
15
+ after: "gem 'dry_serialization', source: 'https://gem.fury.io/mikeyduece-gems/'")
16
+ run 'bundle install'
17
+
18
+ helper_include(SERIALIZERS[:fast_jsonapi])
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ class ApiController < ActionController::API
2
+
3
+ end
metadata ADDED
@@ -0,0 +1,195 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dry_serialization
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.3'
5
+ platform: ruby
6
+ authors:
7
+ - Mike Heft
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: factory_bot
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: generator_spec
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: pry
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: rake
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: activerecord
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: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.4'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.4'
125
+ - !ruby/object:Gem::Dependency
126
+ name: yard
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.9.11
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.9.11
139
+ - !ruby/object:Gem::Dependency
140
+ name: fast_jsonapi
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description:
154
+ email:
155
+ - mikeheft@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - README.md
161
+ - Rakefile
162
+ - dry_serialization.gemspec
163
+ - lib/dry_serialization.rb
164
+ - lib/dry_serialization/blueprinter.rb
165
+ - lib/dry_serialization/fast_jsonapi.rb
166
+ - lib/dry_serialization/version.rb
167
+ - lib/generators/base_generator.rb
168
+ - lib/generators/dry_serialization/blueprinter/install_generator.rb
169
+ - lib/generators/dry_serialization/fast_jsonapi/install_generator.rb
170
+ - lib/generators/templates/api_controller.rb
171
+ homepage:
172
+ licenses:
173
+ - MIT
174
+ metadata: {}
175
+ post_install_message:
176
+ rdoc_options: []
177
+ require_paths:
178
+ - lib
179
+ required_ruby_version: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - ">="
182
+ - !ruby/object:Gem::Version
183
+ version: '0'
184
+ required_rubygems_version: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ requirements: []
190
+ rubygems_version: 3.0.8
191
+ signing_key:
192
+ specification_version: 4
193
+ summary: Drys up serialized returns by passing in object, serializer, and an options
194
+ hash
195
+ test_files: []