abc_jsonapi 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +55 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +63 -0
- data/LICENSE +21 -0
- data/README.md +37 -0
- data/Rakefile +6 -0
- data/abc_jsonapi.gemspec +35 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/abc_jsonapi/collection.rb +27 -0
- data/lib/abc_jsonapi/helpers.rb +6 -0
- data/lib/abc_jsonapi/included_resource.rb +71 -0
- data/lib/abc_jsonapi/model.rb +72 -0
- data/lib/abc_jsonapi/relationship.rb +52 -0
- data/lib/abc_jsonapi/serializer.rb +113 -0
- data/lib/abc_jsonapi/version.rb +3 -0
- data/lib/abc_jsonapi/virtual_attribute.rb +15 -0
- data/lib/abc_jsonapi.rb +27 -0
- metadata +187 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a96cedf5858ef1a23cdf9a35898055f3fdc755a24e6ca0964de971007cb94a14
|
4
|
+
data.tar.gz: 9d2e596d5b7fd8d01f8bc5dd156b6336d62acc182b605ef64c45dfd47e8caa50
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ef73201e7743a4fd4354c413e11c6fa1267f494428318944438f5311181790d0dc2ea5ab9f38cabc110771cd4adbd227afc0f4d8d66eb62239eefc24fc65b2b9
|
7
|
+
data.tar.gz: c285d419c355103b866b27e9026d4c59f276d9b1539ff9d16057d0141f14361158498e45489a9dc29987e22544d6ca18cbb6205cbd3972130e1829402adcead8
|
data/.gitignore
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
/.byebug_history
|
14
|
+
|
15
|
+
/lib/dev/
|
16
|
+
/lib/abc_jsonapi/module_test.rb
|
17
|
+
|
18
|
+
# Used by dotenv library to load environment variables.
|
19
|
+
# .env
|
20
|
+
|
21
|
+
## Specific to RubyMotion:
|
22
|
+
.dat*
|
23
|
+
.repl_history
|
24
|
+
build/
|
25
|
+
*.bridgesupport
|
26
|
+
build-iPhoneOS/
|
27
|
+
build-iPhoneSimulator/
|
28
|
+
|
29
|
+
## Specific to RubyMotion (use of CocoaPods):
|
30
|
+
#
|
31
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
32
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
33
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
34
|
+
#
|
35
|
+
# vendor/Pods/
|
36
|
+
|
37
|
+
## Documentation cache and generated files:
|
38
|
+
/.yardoc/
|
39
|
+
/_yardoc/
|
40
|
+
/doc/
|
41
|
+
/rdoc/
|
42
|
+
|
43
|
+
## Environment normalization:
|
44
|
+
/.bundle/
|
45
|
+
/vendor/bundle
|
46
|
+
/lib/bundler/man/
|
47
|
+
|
48
|
+
# for a library or gem, you might want to ignore these files since the code is
|
49
|
+
# intended to run in multiple environments; otherwise, check them in:
|
50
|
+
# Gemfile.lock
|
51
|
+
# .ruby-version
|
52
|
+
# .ruby-gemset
|
53
|
+
|
54
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
55
|
+
.rvmrc
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
abc_jsonapi (0.1.1)
|
5
|
+
activesupport (>= 5.2)
|
6
|
+
i18n
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (6.0.0)
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
i18n (>= 0.7, < 2)
|
14
|
+
minitest (~> 5.1)
|
15
|
+
tzinfo (~> 1.1)
|
16
|
+
zeitwerk (~> 2.1, >= 2.1.8)
|
17
|
+
byebug (11.0.1)
|
18
|
+
coderay (1.1.2)
|
19
|
+
concurrent-ruby (1.1.5)
|
20
|
+
diff-lcs (1.3)
|
21
|
+
i18n (1.7.0)
|
22
|
+
concurrent-ruby (~> 1.0)
|
23
|
+
method_source (0.9.2)
|
24
|
+
minitest (5.12.2)
|
25
|
+
pg (1.1.4)
|
26
|
+
pry (0.12.2)
|
27
|
+
coderay (~> 1.1.0)
|
28
|
+
method_source (~> 0.9.0)
|
29
|
+
rake (10.5.0)
|
30
|
+
rspec (3.8.0)
|
31
|
+
rspec-core (~> 3.8.0)
|
32
|
+
rspec-expectations (~> 3.8.0)
|
33
|
+
rspec-mocks (~> 3.8.0)
|
34
|
+
rspec-core (3.8.1)
|
35
|
+
rspec-support (~> 3.8.0)
|
36
|
+
rspec-expectations (3.8.4)
|
37
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
38
|
+
rspec-support (~> 3.8.0)
|
39
|
+
rspec-mocks (3.8.1)
|
40
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
41
|
+
rspec-support (~> 3.8.0)
|
42
|
+
rspec-support (3.8.2)
|
43
|
+
sequel (5.21.0)
|
44
|
+
thread_safe (0.3.6)
|
45
|
+
tzinfo (1.2.5)
|
46
|
+
thread_safe (~> 0.1)
|
47
|
+
zeitwerk (2.2.0)
|
48
|
+
|
49
|
+
PLATFORMS
|
50
|
+
ruby
|
51
|
+
|
52
|
+
DEPENDENCIES
|
53
|
+
abc_jsonapi!
|
54
|
+
bundler (~> 2.0)
|
55
|
+
byebug
|
56
|
+
pg
|
57
|
+
pry
|
58
|
+
rake (~> 10.0)
|
59
|
+
rspec (~> 3.0)
|
60
|
+
sequel
|
61
|
+
|
62
|
+
BUNDLED WITH
|
63
|
+
2.0.1
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2019 ezname
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# AbcJsonapi
|
2
|
+
|
3
|
+
Minimalistic gem for JSON Serialization according to https://jsonapi.org spec
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'abc_jsonapi'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install abc_jsonapi
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
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.
|
28
|
+
|
29
|
+
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).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/abc_jsonapi.
|
34
|
+
|
35
|
+
## License
|
36
|
+
|
37
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/abc_jsonapi.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "abc_jsonapi/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "abc_jsonapi"
|
8
|
+
spec.version = AbcJsonapi::VERSION
|
9
|
+
spec.authors = ["Zhmakin Evgeniy"]
|
10
|
+
spec.email = ["zhmakin.ev@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Json API serializer gem"
|
13
|
+
spec.homepage = "https://github.com/ezname/abc_jsonapi"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
spec.add_development_dependency "pry"
|
29
|
+
spec.add_development_dependency "sequel"
|
30
|
+
spec.add_development_dependency "pg"
|
31
|
+
spec.add_development_dependency "byebug"
|
32
|
+
|
33
|
+
spec.add_dependency "activesupport", ">= 5.2"
|
34
|
+
spec.add_dependency "i18n"
|
35
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "abc_jsonapi"
|
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,27 @@
|
|
1
|
+
require 'abc_jsonapi/model'
|
2
|
+
|
3
|
+
module AbcJsonapi
|
4
|
+
class Collection
|
5
|
+
attr_reader :collection, :resource_type, :resource_attributes, :virtual_attributes, :relationships
|
6
|
+
|
7
|
+
def initialize(collection:, resource_type:, resource_attributes:, virtual_attributes:, relationships:)
|
8
|
+
@collection = collection
|
9
|
+
@resource_type = resource_type
|
10
|
+
@resource_attributes = resource_attributes
|
11
|
+
@virtual_attributes = virtual_attributes
|
12
|
+
@relationships = relationships
|
13
|
+
end
|
14
|
+
|
15
|
+
def serializable_hash
|
16
|
+
collection.map do |model|
|
17
|
+
AbcJsonapi::Model.new(
|
18
|
+
model: model,
|
19
|
+
resource_type: resource_type,
|
20
|
+
resource_attributes: resource_attributes,
|
21
|
+
virtual_attributes: virtual_attributes,
|
22
|
+
relationships: relationships
|
23
|
+
).serializable_hash
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module AbcJsonapi
|
2
|
+
class IncludedResource
|
3
|
+
attr_reader :resource, :includes, :serializer_namespace, :includes_result
|
4
|
+
|
5
|
+
def initialize(resource:, includes:, serializer_namespace:)
|
6
|
+
@includes_result = []
|
7
|
+
@resource = resource
|
8
|
+
@includes = includes
|
9
|
+
@serializer_namespace = serializer_namespace
|
10
|
+
end
|
11
|
+
|
12
|
+
def serializable_hash
|
13
|
+
includes.each do |include_path|
|
14
|
+
include_chain = include_path.split('.')
|
15
|
+
if resource.is_a?(Enumerable)
|
16
|
+
resource.each do |single_res|
|
17
|
+
get_included_records(single_res, include_chain.dup)
|
18
|
+
end
|
19
|
+
else
|
20
|
+
get_included_records(resource, include_chain)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
includes_result.flatten
|
24
|
+
end
|
25
|
+
|
26
|
+
def get_included_records(resource, include_chain)
|
27
|
+
return if resource.nil? || include_chain.empty?
|
28
|
+
|
29
|
+
# Get first include name of include_chain and delete it from include_chain
|
30
|
+
inc_resource_name = include_chain.shift
|
31
|
+
|
32
|
+
# Check if include name is exist in relationships array. Take it or return nil
|
33
|
+
resource_class_name = resource.is_a?(Enumerable) ? resource[0].class.name : resource.class.name
|
34
|
+
relationship = serializer(resource_class_name).relationships.find { |h| h[:name] == inc_resource_name.to_sym }
|
35
|
+
return if relationship.nil?
|
36
|
+
|
37
|
+
# Get included resource
|
38
|
+
if resource.is_a?(Enumerable)
|
39
|
+
resource = included_items_from_collection(resource, inc_resource_name)
|
40
|
+
else
|
41
|
+
resource = resource.public_send(inc_resource_name)
|
42
|
+
end
|
43
|
+
return if resource.nil?
|
44
|
+
|
45
|
+
@includes_result << serializer(inc_resource_name).new(resource).serializable_hash[:data]
|
46
|
+
|
47
|
+
# If resource is a collection call get_included_records for each. Otherwise send whole resource
|
48
|
+
if resource.is_a?(Enumerable)
|
49
|
+
resource.each do |single_res|
|
50
|
+
get_included_records(single_res, include_chain.dup)
|
51
|
+
end
|
52
|
+
else
|
53
|
+
get_included_records(resource, include_chain)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def included_items_from_collection(collection, include_name)
|
58
|
+
# Run custom include strategy if block given. Otherwise run default method
|
59
|
+
if (block = relationship.dig(:block)).present?
|
60
|
+
block.call(resource)
|
61
|
+
else
|
62
|
+
collection.map(&:include_name).flatten.uniq
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def serializer(included_resource_name)
|
67
|
+
(serializer_namespace + included_resource_name.to_s.classify + 'Serializer').constantize
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'abc_jsonapi/virtual_attribute'
|
2
|
+
require 'abc_jsonapi/relationship'
|
3
|
+
|
4
|
+
module AbcJsonapi
|
5
|
+
class Model
|
6
|
+
attr_reader :model, :resource_type, :resource_attributes, :virtual_attributes, :relationships
|
7
|
+
|
8
|
+
def initialize(model:, resource_type:, resource_attributes:, virtual_attributes:, relationships:)
|
9
|
+
@model = model
|
10
|
+
@resource_type = resource_type.to_sym
|
11
|
+
@resource_attributes = resource_attributes
|
12
|
+
@virtual_attributes = virtual_attributes
|
13
|
+
@relationships = relationships
|
14
|
+
end
|
15
|
+
|
16
|
+
def serializable_hash
|
17
|
+
data = {}
|
18
|
+
data[:id] = model.id.to_s
|
19
|
+
data[:type] = resource_type
|
20
|
+
data[:attributes] = transform_keys_if_necessary(attributes_hash)
|
21
|
+
data[:relationships] = transform_keys_if_necessary(relationships_hash, true) if relationships.any?
|
22
|
+
data
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def attributes_hash
|
28
|
+
result = {}
|
29
|
+
resource_attributes.each do |attribute|
|
30
|
+
result.merge!(attribute => model.public_send(attribute))
|
31
|
+
end
|
32
|
+
|
33
|
+
virtual_attributes.each do |attribute|
|
34
|
+
attribute = AbcJsonapi::VirtualAttribute.new(
|
35
|
+
model: model,
|
36
|
+
name: attribute[:name],
|
37
|
+
block: attribute[:block]
|
38
|
+
)
|
39
|
+
result.merge!(attribute.serializable_hash)
|
40
|
+
end
|
41
|
+
|
42
|
+
result
|
43
|
+
end
|
44
|
+
|
45
|
+
def relationships_hash
|
46
|
+
result = {}
|
47
|
+
relationships.each do |relationship|
|
48
|
+
rel_model = AbcJsonapi::Relationship.new(
|
49
|
+
model: model,
|
50
|
+
relationship: relationship[:name],
|
51
|
+
type: relationship[:type]
|
52
|
+
)
|
53
|
+
result[relationship[:name].to_sym] = rel_model.serializable_hash
|
54
|
+
end
|
55
|
+
result
|
56
|
+
end
|
57
|
+
|
58
|
+
def transform_keys_if_necessary(hash, deep = false)
|
59
|
+
transform_method = deep ? 'deep_transform_keys' : 'transform_keys'
|
60
|
+
|
61
|
+
if AbcJsonapi.config.transform_keys
|
62
|
+
case AbcJsonapi.config.key_transform_method
|
63
|
+
when 'camel'
|
64
|
+
hash.public_send(transform_method){ |key| key.to_s.camelize(:lower).to_sym }
|
65
|
+
when 'snake'
|
66
|
+
hash.public_send(transform_method){ |key| key.to_s.underscore.to_sym }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module AbcJsonapi
|
2
|
+
class Relationship
|
3
|
+
attr_reader :model, :relationship, :type
|
4
|
+
|
5
|
+
def initialize(model:, relationship:, type:)
|
6
|
+
@model = model
|
7
|
+
@relationship = relationship
|
8
|
+
@type = type
|
9
|
+
end
|
10
|
+
|
11
|
+
def serializable_hash
|
12
|
+
case type
|
13
|
+
when :has_one
|
14
|
+
{ data: serialize_has_one }
|
15
|
+
when :has_many
|
16
|
+
{ data: serialize_has_many }
|
17
|
+
when :belongs_to
|
18
|
+
{ data: serialize_belongs_to }
|
19
|
+
else
|
20
|
+
{ data: nil }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def serialize_has_one
|
25
|
+
data = {}
|
26
|
+
rel = model.public_send(relationship)
|
27
|
+
return if rel.nil?
|
28
|
+
data[:id] = rel.id.to_s
|
29
|
+
data[:type] = Helpers.pluralize_if_necessary(relationship.to_s).to_sym
|
30
|
+
data
|
31
|
+
end
|
32
|
+
|
33
|
+
def serialize_has_many
|
34
|
+
data = model.public_send(relationship).map do |relation|
|
35
|
+
{
|
36
|
+
id: relation.id.to_s,
|
37
|
+
type: Helpers.pluralize_if_necessary(relationship.to_s).to_sym
|
38
|
+
}
|
39
|
+
end
|
40
|
+
data
|
41
|
+
end
|
42
|
+
|
43
|
+
def serialize_belongs_to
|
44
|
+
data = {}
|
45
|
+
id = model.public_send("#{relationship}_id")
|
46
|
+
return if id.blank?
|
47
|
+
data[:id] = id.to_s
|
48
|
+
data[:type] = Helpers.pluralize_if_necessary(relationship.to_s).to_sym
|
49
|
+
data
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'active_support/core_ext/string'
|
3
|
+
require 'active_support/json'
|
4
|
+
require 'i18n'
|
5
|
+
require 'byebug'
|
6
|
+
require 'abc_jsonapi/model'
|
7
|
+
require 'abc_jsonapi/collection'
|
8
|
+
require 'abc_jsonapi/included_resource'
|
9
|
+
|
10
|
+
module AbcJsonapi
|
11
|
+
module Serializer
|
12
|
+
extend ActiveSupport::Concern
|
13
|
+
|
14
|
+
attr_reader :resource, :result_hash, :resource_type, :resource_attributes,
|
15
|
+
:relationships, :virtual_attributes, :includes, :meta
|
16
|
+
|
17
|
+
included do
|
18
|
+
@resource_attributes = []
|
19
|
+
@relationships = []
|
20
|
+
@virtual_attributes = []
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(resource, options = {})
|
24
|
+
@resource = resource
|
25
|
+
@result_hash = { data: nil }
|
26
|
+
@resource_type = self.class.resource_type
|
27
|
+
@resource_attributes = self.class.resource_attributes
|
28
|
+
@relationships = self.class.relationships
|
29
|
+
@virtual_attributes = self.class.virtual_attributes
|
30
|
+
@includes = options[:include]
|
31
|
+
@meta = options[:meta]
|
32
|
+
end
|
33
|
+
|
34
|
+
def serializable_hash
|
35
|
+
result_hash[:meta] = meta if meta.present?
|
36
|
+
result_hash[:data] = data_hash
|
37
|
+
result_hash[:included] = included_hash if @includes.present?
|
38
|
+
result_hash
|
39
|
+
end
|
40
|
+
|
41
|
+
def serialized_json
|
42
|
+
ActiveSupport::JSON.encode(serializable_hash)
|
43
|
+
end
|
44
|
+
|
45
|
+
module ClassMethods
|
46
|
+
attr_reader :resource_attributes, :relationships, :virtual_attributes
|
47
|
+
|
48
|
+
def attributes(*attributes)
|
49
|
+
@resource_attributes = attributes
|
50
|
+
end
|
51
|
+
|
52
|
+
def has_one(relationship, &block)
|
53
|
+
@relationships << { type: :has_one, name: relationship, block: block }
|
54
|
+
end
|
55
|
+
|
56
|
+
def has_many(relationship, &block)
|
57
|
+
@relationships << { type: :has_many, name: relationship, block: block }
|
58
|
+
end
|
59
|
+
|
60
|
+
def belongs_to(relationship, &block)
|
61
|
+
@relationships << { type: :belongs_to, name: relationship, block: block }
|
62
|
+
end
|
63
|
+
|
64
|
+
def resource_type(rtype = nil)
|
65
|
+
@resource_type ||= rtype || Helpers.pluralize_if_necessary(default_type)
|
66
|
+
end
|
67
|
+
|
68
|
+
def attribute(name, &block)
|
69
|
+
@virtual_attributes << { name: name, block: block }
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def default_type
|
75
|
+
self.to_s[/(\w+)Serializer$/, 1].tableize
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def included_hash
|
82
|
+
AbcJsonapi::IncludedResource.new(
|
83
|
+
resource: resource,
|
84
|
+
includes: includes,
|
85
|
+
serializer_namespace: namespace
|
86
|
+
).serializable_hash
|
87
|
+
end
|
88
|
+
|
89
|
+
def namespace
|
90
|
+
self.class.name.gsub(/()?\w+Serializer$/, '')
|
91
|
+
end
|
92
|
+
|
93
|
+
def data_hash
|
94
|
+
if resource.is_a?(Enumerable)
|
95
|
+
AbcJsonapi::Collection.new(
|
96
|
+
collection: resource,
|
97
|
+
resource_type: resource_type,
|
98
|
+
resource_attributes: resource_attributes,
|
99
|
+
virtual_attributes: virtual_attributes,
|
100
|
+
relationships: relationships
|
101
|
+
).serializable_hash
|
102
|
+
else
|
103
|
+
AbcJsonapi::Model.new(
|
104
|
+
model: resource,
|
105
|
+
resource_type: resource_type,
|
106
|
+
resource_attributes: resource_attributes,
|
107
|
+
virtual_attributes: virtual_attributes,
|
108
|
+
relationships: relationships
|
109
|
+
).serializable_hash
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module AbcJsonapi
|
2
|
+
class VirtualAttribute
|
3
|
+
attr_reader :model, :name, :block
|
4
|
+
|
5
|
+
def initialize(model:, name:, block:)
|
6
|
+
@model = model
|
7
|
+
@name = name
|
8
|
+
@block = block
|
9
|
+
end
|
10
|
+
|
11
|
+
def serializable_hash
|
12
|
+
{ name.to_sym => block.call(model) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/abc_jsonapi.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "abc_jsonapi/version"
|
2
|
+
require "abc_jsonapi/serializer"
|
3
|
+
require "abc_jsonapi/helpers"
|
4
|
+
|
5
|
+
module AbcJsonapi
|
6
|
+
class << self
|
7
|
+
attr_reader :config
|
8
|
+
|
9
|
+
def configure
|
10
|
+
yield(config)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Configuration
|
15
|
+
attr_accessor :transform_keys, :key_transform_method, :pluralize_resources
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@transform_keys ||= true
|
19
|
+
@key_transform_method ||= 'camel' # snake or camel
|
20
|
+
@pluralize_resources ||= false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
@config ||= Configuration.new
|
25
|
+
|
26
|
+
class Error < StandardError; end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,187 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: abc_jsonapi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Zhmakin Evgeniy
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-10-17 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: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
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: sequel
|
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: pg
|
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: byebug
|
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: activesupport
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '5.2'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '5.2'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: i18n
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description:
|
140
|
+
email:
|
141
|
+
- zhmakin.ev@gmail.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- ".gitignore"
|
147
|
+
- Gemfile
|
148
|
+
- Gemfile.lock
|
149
|
+
- LICENSE
|
150
|
+
- README.md
|
151
|
+
- Rakefile
|
152
|
+
- abc_jsonapi.gemspec
|
153
|
+
- bin/console
|
154
|
+
- bin/setup
|
155
|
+
- lib/abc_jsonapi.rb
|
156
|
+
- lib/abc_jsonapi/collection.rb
|
157
|
+
- lib/abc_jsonapi/helpers.rb
|
158
|
+
- lib/abc_jsonapi/included_resource.rb
|
159
|
+
- lib/abc_jsonapi/model.rb
|
160
|
+
- lib/abc_jsonapi/relationship.rb
|
161
|
+
- lib/abc_jsonapi/serializer.rb
|
162
|
+
- lib/abc_jsonapi/version.rb
|
163
|
+
- lib/abc_jsonapi/virtual_attribute.rb
|
164
|
+
homepage: https://github.com/ezname/abc_jsonapi
|
165
|
+
licenses:
|
166
|
+
- MIT
|
167
|
+
metadata: {}
|
168
|
+
post_install_message:
|
169
|
+
rdoc_options: []
|
170
|
+
require_paths:
|
171
|
+
- lib
|
172
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
173
|
+
requirements:
|
174
|
+
- - ">="
|
175
|
+
- !ruby/object:Gem::Version
|
176
|
+
version: '0'
|
177
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
178
|
+
requirements:
|
179
|
+
- - ">="
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: '0'
|
182
|
+
requirements: []
|
183
|
+
rubygems_version: 3.0.3
|
184
|
+
signing_key:
|
185
|
+
specification_version: 4
|
186
|
+
summary: Json API serializer gem
|
187
|
+
test_files: []
|