fast_serializer_ruby 0.4.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 27c5455f3a87cdcc93c46a8c715b18ade3a681f5c2cff42283e7b910f8ed7d1e
4
- data.tar.gz: c7a3a754d887f00ec3652c3264f6c474ec30608148f392667cc7c1159524dff6
3
+ metadata.gz: 443a464f8f7d09a40c3457933896eaa35a27c841db28a259dd6fd95edbf2ddd1
4
+ data.tar.gz: 1ab61528123941c2d6905795ca0c9f5bc95dab2982635300344b0eabdd3d62d6
5
5
  SHA512:
6
- metadata.gz: 99288e9f703d5b17535e1f989f7792e3de2dd93f1f9f313b62b13c6d72edecc47d358e4e8e973df8736fc642e8c27f1ffda6b9346ac587e70c0fdf94bb7018c4
7
- data.tar.gz: fc9031c91ffaa474b3c339b9f7ee347d8a8c08dd3c820585d0882de169e31ff7c0ed27ded3b82584917ebd013804bdb49b6733ba938ea8c4aa176f7d65c182b5
6
+ metadata.gz: cbe8efbf120d930648abb69eca0b541ffe73181a2f63b1f0e0b4132506fb699e7a3b7007cf1f5c2e97415ef3936539222c282d7ea8766214b4a69e5c73b5d4b9
7
+ data.tar.gz: 8f66f7174aa206a31c1b7885c2e1f2deae54fe882870db55017bef74363cae428bc8e14d20fcd925cca9cedb8041ef10349672a21feaf279a97577e4932d9cdb
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
  # rspec failure tracking
13
13
  .rspec_status
14
14
  .rspec
15
+ .history
data/.travis.yml CHANGED
@@ -7,6 +7,7 @@ rvm:
7
7
  - 2.4
8
8
  - 2.5
9
9
  - 2.6
10
+ - 2.7
10
11
  - jruby-9.2.0.0
11
12
  - ruby-head
12
13
  - jruby-head
@@ -17,6 +18,10 @@ matrix:
17
18
  - rvm: ruby-head
18
19
  - rvm: jruby-head
19
20
 
21
+ before_install:
22
+ - yes | gem update --system --force
23
+ - gem install bundler
24
+
20
25
  before_script:
21
26
  - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
22
27
  - chmod +x ./cc-test-reporter
@@ -31,5 +36,5 @@ cache: bundler
31
36
 
32
37
  env:
33
38
  global:
34
- CC_TEST_REPORTER_ID=a051d3d3560aa2e58cc5efb9f7b8eaafe87f983a1c4d6cb1573fda50ede5b685
39
+ CC_TEST_REPORTER_ID=99325a42b78ef51a9d51574d20dbcd6acdfe75c8568f1433f73b61f17149e05d
35
40
  COVERAGE=1
data/CODE_OF_CONDUCT.md CHANGED
@@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
55
55
  ## Enforcement
56
56
 
57
57
  Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at evgeny.stepanov@jetruby.com. All
58
+ reported by contacting the project team at estepnv@icloud.com. All
59
59
  complaints will be reviewed and investigated and will result in a response that
60
60
  is deemed necessary and appropriate to the circumstances. The project team is
61
61
  obligated to maintain confidentiality with regard to the reporter of an incident.
data/Gemfile CHANGED
@@ -4,5 +4,20 @@ source 'https://rubygems.org'
4
4
 
5
5
  git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
6
 
7
- # Specify your gem's dependencies in fast_serializer.gemspec
8
7
  gemspec
8
+
9
+ group :development do
10
+ gem 'pry-byebug', '~> 3.7.0'
11
+ gem 'pry'
12
+ end
13
+
14
+ group :test do
15
+ gem 'active_model_serializers', '~> 0.10.0'
16
+ gem 'factory_bot'
17
+ gem 'faker'
18
+ gem 'allocation_stats'
19
+ gem 'simplecov', '~> 0.17.1'
20
+ gem 'benchmark-memory', '~> 0.1'
21
+ gem 'rspec', '~> 3.0'
22
+ gem 'rspec-benchmark'
23
+ end
data/Gemfile.lock CHANGED
@@ -1,20 +1,20 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fast_serializer_ruby (0.4.2)
4
+ fast_serializer_ruby (0.6.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- actionpack (5.2.3)
10
- actionview (= 5.2.3)
11
- activesupport (= 5.2.3)
12
- rack (~> 2.0)
9
+ actionpack (5.2.4.1)
10
+ actionview (= 5.2.4.1)
11
+ activesupport (= 5.2.4.1)
12
+ rack (~> 2.0, >= 2.0.8)
13
13
  rack-test (>= 0.6.3)
14
14
  rails-dom-testing (~> 2.0)
15
15
  rails-html-sanitizer (~> 1.0, >= 1.0.2)
16
- actionview (5.2.3)
17
- activesupport (= 5.2.3)
16
+ actionview (5.2.4.1)
17
+ activesupport (= 5.2.4.1)
18
18
  builder (~> 3.1)
19
19
  erubi (~> 1.4)
20
20
  rails-dom-testing (~> 2.0)
@@ -24,52 +24,53 @@ GEM
24
24
  activemodel (>= 4.1, < 6.1)
25
25
  case_transform (>= 0.2)
26
26
  jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
27
- activemodel (5.2.3)
28
- activesupport (= 5.2.3)
29
- activesupport (5.2.3)
27
+ activemodel (5.2.4.1)
28
+ activesupport (= 5.2.4.1)
29
+ activesupport (5.2.4.1)
30
30
  concurrent-ruby (~> 1.0, >= 1.0.2)
31
31
  i18n (>= 0.7, < 2)
32
32
  minitest (~> 5.1)
33
33
  tzinfo (~> 1.1)
34
+ allocation_stats (0.1.5)
34
35
  benchmark-malloc (0.1.0)
36
+ benchmark-memory (0.1.2)
37
+ memory_profiler (~> 0.9)
35
38
  benchmark-perf (0.5.0)
36
39
  benchmark-trend (0.3.0)
37
- builder (3.2.3)
40
+ builder (3.2.4)
41
+ byebug (11.0.1)
38
42
  case_transform (0.2)
39
43
  activesupport
40
44
  coderay (1.1.2)
41
45
  concurrent-ruby (1.1.5)
42
- crass (1.0.5)
46
+ crass (1.0.6)
43
47
  diff-lcs (1.3)
44
48
  docile (1.3.2)
45
49
  erubi (1.9.0)
46
50
  factory_bot (5.1.1)
47
51
  activesupport (>= 4.2.0)
48
- faker (2.6.0)
49
- i18n (>= 1.6, < 1.8)
50
- ffi (1.11.1-java)
51
- i18n (1.7.0)
52
+ faker (2.10.1)
53
+ i18n (>= 1.6, < 2)
54
+ i18n (1.8.2)
52
55
  concurrent-ruby (~> 1.0)
53
- json (2.2.0)
54
- json (2.2.0-java)
56
+ json (2.3.0)
55
57
  jsonapi-renderer (0.2.2)
56
- loofah (2.3.1)
58
+ loofah (2.4.0)
57
59
  crass (~> 1.0.2)
58
60
  nokogiri (>= 1.5.9)
61
+ memory_profiler (0.9.14)
59
62
  method_source (0.9.2)
60
63
  mini_portile2 (2.4.0)
61
- minitest (5.12.2)
62
- nokogiri (1.10.5)
64
+ minitest (5.14.0)
65
+ nokogiri (1.10.7)
63
66
  mini_portile2 (~> 2.4.0)
64
- nokogiri (1.10.5-java)
65
67
  pry (0.12.2)
66
68
  coderay (~> 1.1.0)
67
69
  method_source (~> 0.9.0)
68
- pry (0.12.2-java)
69
- coderay (~> 1.1.0)
70
- method_source (~> 0.9.0)
71
- spoon (~> 0.0)
72
- rack (2.0.7)
70
+ pry-byebug (3.7.0)
71
+ byebug (~> 11.0)
72
+ pry (~> 0.10)
73
+ rack (2.1.2)
73
74
  rack-test (1.1.0)
74
75
  rack (>= 1.0, < 3)
75
76
  rails-dom-testing (2.0.3)
@@ -77,7 +78,6 @@ GEM
77
78
  nokogiri (>= 1.6)
78
79
  rails-html-sanitizer (1.3.0)
79
80
  loofah (~> 2.3)
80
- rake (10.5.0)
81
81
  rspec (3.9.0)
82
82
  rspec-core (~> 3.9.0)
83
83
  rspec-expectations (~> 3.9.0)
@@ -87,42 +87,39 @@ GEM
87
87
  benchmark-perf (~> 0.5.0)
88
88
  benchmark-trend (~> 0.3.0)
89
89
  rspec (>= 3.0.0, < 4.0.0)
90
- rspec-core (3.9.0)
91
- rspec-support (~> 3.9.0)
90
+ rspec-core (3.9.1)
91
+ rspec-support (~> 3.9.1)
92
92
  rspec-expectations (3.9.0)
93
93
  diff-lcs (>= 1.2.0, < 2.0)
94
94
  rspec-support (~> 3.9.0)
95
- rspec-mocks (3.9.0)
95
+ rspec-mocks (3.9.1)
96
96
  diff-lcs (>= 1.2.0, < 2.0)
97
97
  rspec-support (~> 3.9.0)
98
- rspec-support (3.9.0)
98
+ rspec-support (3.9.2)
99
99
  simplecov (0.17.1)
100
100
  docile (~> 1.1)
101
101
  json (>= 1.8, < 3)
102
102
  simplecov-html (~> 0.10.0)
103
103
  simplecov-html (0.10.2)
104
- spoon (0.0.6)
105
- ffi
106
104
  thread_safe (0.3.6)
107
- thread_safe (0.3.6-java)
108
- tzinfo (1.2.5)
105
+ tzinfo (1.2.6)
109
106
  thread_safe (~> 0.1)
110
107
 
111
108
  PLATFORMS
112
- java
113
109
  ruby
114
110
 
115
111
  DEPENDENCIES
116
112
  active_model_serializers (~> 0.10.0)
117
- activesupport (< 6)
113
+ allocation_stats
114
+ benchmark-memory (~> 0.1)
118
115
  factory_bot
119
116
  faker
120
117
  fast_serializer_ruby!
121
118
  pry
122
- rake (~> 10.0)
119
+ pry-byebug (~> 3.7.0)
123
120
  rspec (~> 3.0)
124
121
  rspec-benchmark
125
- simplecov
122
+ simplecov (~> 0.17.1)
126
123
 
127
124
  BUNDLED WITH
128
- 1.17.3
125
+ 2.1.4
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2019 Evgeny Stepanov
3
+ Copyright (c) 2020 Evgeny Stepanov
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,24 +1,33 @@
1
+ [![Gem Version](https://badge.fury.io/rb/fast_serializer_ruby.svg)](https://badge.fury.io/rb/fast_serializer_ruby)
1
2
  [![Build Status](https://travis-ci.org/estepnv/fast_serializer.svg?branch=master)](https://travis-ci.org/estepnv/fast_serializer)
2
- [![Maintainability](https://api.codeclimate.com/v1/badges/74cca93390234e619cf5/maintainability)](https://codeclimate.com/github/estepnv/fast_serializer/maintainability)
3
- [![Test Coverage](https://api.codeclimate.com/v1/badges/74cca93390234e619cf5/test_coverage)](https://codeclimate.com/github/estepnv/fast_serializer/test_coverage)
3
+ [![Maintainability](https://api.codeclimate.com/v1/badges/df7897bec85d376709bd/maintainability)](https://codeclimate.com/github/estepnv/fast_serializer/maintainability)
4
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/df7897bec85d376709bd/test_coverage)](https://codeclimate.com/github/estepnv/fast_serializer/test_coverage)
4
5
 
5
6
  # fast_serializer
6
7
 
7
- fast_serializer is a lightweight ruby objects serializer.
8
+ `fast_serializer_ruby` is a lightweight ruby object to hash transformer.
9
+ This library intends to solve such a typical and on the other hand important problem as efficient ruby object to hash transformation.
8
10
 
9
- It has zero dependencies and written in pure ruby.
10
- That's why it's so performant.
11
-
12
- - running on ruby 2.6 is **at least 3 times faster** than AMS (benchmarks was borrowed from fast_jsonapi repository)
11
+ ## Performance 🚀
12
+ - running on ruby 2.6 is **at least 5 times faster** than AMS (benchmarks was borrowed from fast_jsonapi repository)
13
+ - running on ruby 2.6 it consumes **3 times less RAM**
13
14
  - running on jruby 9.2.7.0 **is at least 15 times faster** than AMS after warming up
14
15
 
16
+ ## Compatibility 👌
17
+ I tried to keep the API as close as possible to active_model_serializer implementation because we all got used to it.
18
+
19
+ ## Features
20
+ - conditional rendering
21
+ - inheritence
22
+ - included/excluded associations
23
+
15
24
 
16
25
  ## Installation
17
26
 
18
27
  Add this line to your application's Gemfile:
19
28
 
20
29
  ```ruby
21
- gem 'fast_serializer'
30
+ gem 'fast_serializer_ruby'
22
31
  ```
23
32
 
24
33
  And then execute:
@@ -27,38 +36,43 @@ And then execute:
27
36
 
28
37
  Or install it yourself as:
29
38
 
30
- $ gem install fast_serializer
39
+ $ gem install fast_serializer_ruby
31
40
 
32
41
  ## Usage
33
42
 
34
- fast_serializer supports default schema definition using class methods
43
+ `fast_serializer` supports default schema definition using class methods
35
44
 
36
45
  ```ruby
37
46
  class ResourceSerializer
38
47
  include FastSerializer::Schema::Mixin
48
+
39
49
  root :resource
40
-
50
+
41
51
  attributes :id, :email, :phone
42
-
43
- attribute(:string_id, if: -> (resource, params) { params[:stringify] }) { |resource, params| resource.id.to_s }
44
- attribute(:float_id, unless: -> (resource, params) { params[:stringify] }) { |resource, params| resource.id.to_f }
45
- attribute(:full_name) { |resource, params| params[:only_first_name] ? resource.first_name : "#{resource.first_name} #{resource.last_name}" }
46
-
52
+
53
+ attribute(:string_id, if: -> { params[:stringify] }) { resource.id.to_s }
54
+ attribute(:float_id, unless: :stringify?) { object.id.to_f }
55
+ attribute(:full_name) { params[:only_first_name] ? resource.first_name : "#{resource.first_name} #{resource.last_name}" }
56
+
47
57
  has_one :has_one_relationship, serializer: ResourceSerializer
48
58
  has_many :has_many_relationship, serializer: ResourceSerializer
59
+
60
+ def stringify?
61
+ params[:stringify]
62
+ end
49
63
  end
50
64
 
51
- ResourceSerializer.new(resource, meta: {foo: "bar"}, only_first_name: false, stringify: true).serializable_hash
65
+ ResourceSerializer.new(resource, meta: {foo: "bar"}, only_first_name: false, stringify: true, exclude: [:has_many_relationship]).serializable_hash
52
66
  => {
53
67
  :resource => {
54
- :id => 7873392581,
55
- :email => "houston@luettgen.info",
56
- :full_name => "Jamar Graham",
57
- :phone => "627.051.6039 x1475",
68
+ :id => 7873392581,
69
+ :email => "houston@luettgen.info",
70
+ :full_name => "Jamar Graham",
71
+ :phone => "627.051.6039 x1475",
58
72
  :has_one_relationship => {
59
- :id => 6218322696,
60
- :email=>"terrellrobel@pagac.info",
61
- :full_name => "Clay Kuphal",
73
+ :id => 6218322696,
74
+ :email=>"terrellrobel@pagac.info",
75
+ :full_name => "Clay Kuphal",
62
76
  :phone => "1-604-682-0732 x882"
63
77
  }
64
78
  },
@@ -80,14 +94,14 @@ schema.has_one(:has_one_relationship, schema: schema)
80
94
 
81
95
  schema.serializable_hash
82
96
  => {
83
- :id => 7873392581,
84
- :email => "houston@luettgen.info",
85
- :full_name => "Jamar Graham",
86
- :phone => "627.051.6039 x1475",
97
+ :id => 7873392581,
98
+ :email => "houston@luettgen.info",
99
+ :full_name => "Jamar Graham",
100
+ :phone => "627.051.6039 x1475",
87
101
  :has_one_relationship => {
88
- :id => 6218322696,
89
- :email=>"terrellrobel@pagac.info",
90
- :full_name => "Clay Kuphal",
102
+ :id => 6218322696,
103
+ :email=>"terrellrobel@pagac.info",
104
+ :full_name => "Clay Kuphal",
91
105
  :phone => "1-604-682-0732 x882"
92
106
  }
93
107
  }
@@ -101,8 +115,6 @@ schema.serializable_hash
101
115
 
102
116
  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.
103
117
 
104
- 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).
105
-
106
118
  ## Contributing
107
119
 
108
120
  Bug reports and pull requests are welcome on GitHub at https://github.com/estepnv/fast_serializer. 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.
@@ -10,10 +10,12 @@ Gem::Specification.new do |spec|
10
10
  spec.authors = ['Evgeny Stepanov']
11
11
  spec.email = ['estepnv@icloud.com']
12
12
 
13
- spec.summary = 'Yet another serializer gem, but fast'
14
- spec.description = 'fast_serializer is a lightweight ruby objects serializer.'
13
+ spec.summary = 'fast_serializer is a lightweight ruby objects serializer.'
14
+ spec.description = 'This library intends to solve such a typical and on the other hand important problem as efficient ruby object to hash transformation.'
15
15
  spec.homepage = 'https://github.com/estepnv/fast_serializer'
16
16
  spec.license = 'MIT'
17
+ spec.platform = Gem::Platform::RUBY
18
+ spec.required_ruby_version = '>= 2.3.0'
17
19
 
18
20
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
21
  # to allow pushing to a single host or delete this section to allow pushing to any host.
@@ -31,17 +33,7 @@ Gem::Specification.new do |spec|
31
33
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
32
34
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{(test|spec|features|git|rspec|travis)/}) }
33
35
  end
34
- spec.bindir = 'exe'
36
+ spec.bindir = 'bin'
35
37
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
38
  spec.require_paths = ['lib']
37
-
38
- spec.add_development_dependency 'active_model_serializers', '~> 0.10.0'
39
- spec.add_development_dependency 'activesupport', '<6'
40
- spec.add_development_dependency 'factory_bot'
41
- spec.add_development_dependency 'faker'
42
- spec.add_development_dependency 'pry'
43
- spec.add_development_dependency 'rake', '~> 10.0'
44
- spec.add_development_dependency 'rspec', '~> 3.0'
45
- spec.add_development_dependency 'rspec-benchmark'
46
- spec.add_development_dependency 'simplecov'
47
39
  end
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'fast_serializer/version'
4
+ require 'fast_serializer/utils'
4
5
  require 'fast_serializer/configuration'
5
6
  require 'fast_serializer/json_model'
6
7
  require 'fast_serializer/schema'
8
+ require 'fast_serializer/schema/mixin'
7
9
 
8
10
  module FastSerializer
9
11
  class Error < StandardError; end
10
- # Your code goes here...
11
12
  end
@@ -1,11 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'json'
4
+
3
5
  module FastSerializer
4
6
  class Configuration
5
- attr_accessor :coder
7
+
8
+ RubyVer = Struct.new(:major, :feature, :fix) do
9
+
10
+ def is_2_4_or_less
11
+ major == 2 && feature <= 4
12
+ end
13
+ end
14
+
15
+ attr_reader :coder, :ruby_ver
16
+ attr_accessor :strict
6
17
 
7
18
  def initialize
8
19
  @coder = JSON
20
+ @strict = false
21
+ @ruby_ver = RubyVer.new(*RUBY_VERSION.split(".").map(&:to_i))
22
+ end
23
+
24
+ def coder=(obj)
25
+ if obj.respond_to?(:dump) && obj.respond_to?(:load)
26
+ @coder = obj
27
+ else
28
+ raise ArgumentError, "must respond to #load and #dump methods"
29
+ end
9
30
  end
10
31
  end
11
32
 
@@ -3,7 +3,7 @@
3
3
  module FastSerializer
4
4
  module JsonModel
5
5
  class Array < Relationship
6
- def serialize(resources, params = {}, context = nil)
6
+ def serialize(resources, params, context)
7
7
  return if resources.nil?
8
8
 
9
9
  if @serializer_klass
@@ -13,7 +13,7 @@ module FastSerializer
13
13
  end
14
14
  end
15
15
 
16
- def included?(_resources, _params = {}, context = nil)
16
+ def included?(*)
17
17
  true
18
18
  end
19
19
  end
@@ -3,33 +3,86 @@
3
3
  module FastSerializer
4
4
  module JsonModel
5
5
  class Attribute < Node
6
- def serialize(resource, params = {}, context = nil)
7
- context ||= self
6
+ attr_accessor :mixin,
7
+ :method_name,
8
+ :method_arity,
9
+ :cond,
10
+ :cond_arity,
11
+ :cond_method_name,
12
+ :injected
13
+
14
+ def initialize(_)
15
+ super
16
+ @mixin = nil
17
+ @method_name = nil
18
+ @injected = false
19
+ @cond_method_name = nil
20
+ @cond = nil
21
+ @cond = @opts[:if] || @opts[:unless] || @cond
8
22
 
9
23
  if method.is_a?(Proc)
24
+ @method_name = "__#{key}__"
25
+ @method_arity = method.arity.abs
26
+ @mixin = Module.new
27
+
28
+ if FastSerializer.config.ruby_ver.is_2_4_or_less
29
+ @mixin.redefine_method @method_name, &method
30
+ else
31
+ @mixin.define_method @method_name, &method
32
+ end
33
+
34
+ end
35
+
36
+ if !cond.nil? && cond.is_a?(Proc)
37
+ @cond_method_name = "__#{key}_cond__"
38
+ @cond_arity = cond.arity.abs
39
+ @mixin ||= Module.new
10
40
 
11
- if method.arity.abs == 1
12
- context.instance_exec(resource, &method)
41
+ if FastSerializer.config.ruby_ver.is_2_4_or_less
42
+ @mixin.redefine_method @cond_method_name, &cond
13
43
  else
14
- context.instance_exec(resource, params, &method)
44
+ @mixin.define_method @cond_method_name, &cond
15
45
  end
16
46
 
47
+ end
48
+
49
+ end
50
+
51
+ def injectable?
52
+ !mixin.nil?
53
+ end
54
+
55
+ def inject(context)
56
+ context.include(mixin)
57
+ self.injected = true
58
+ end
59
+
60
+ def serialize(resource, params, context)
61
+
62
+ val = if injected && !method_name.nil? && !context.nil?
63
+ call_method_on_context(context, method_name, method_arity, resource, params)
64
+
65
+ elsif method.is_a?(Proc)
66
+ call_proc_binding_to_context(context, method, method_arity, resource, params)
67
+
17
68
  else
18
69
  resource.public_send(method)
19
70
  end
71
+
72
+ val.freeze
73
+
74
+ val
20
75
  end
21
76
 
22
- def included?(resource, params, context = nil)
23
- return true if @opts[:if].nil? && @opts[:unless].nil?
77
+ def included?(resource, params, context)
78
+ return true if cond.nil?
79
+
80
+ res = if injected && !cond_method_name.nil? && !context.nil?
81
+ call_method_on_context(context, cond_method_name, cond_arity, resource, params)
24
82
 
25
- cond = @opts[:if] || @opts[:unless]
83
+ elsif cond.is_a?(Proc)
84
+ call_proc_binding_to_context(context, cond, cond_arity, resource, params)
26
85
 
27
- res = if cond.is_a?(Proc)
28
- if cond.arity.abs == 1
29
- context.instance_exec(resource, &cond)
30
- else
31
- context.instance_exec(resource, params, &cond)
32
- end
33
86
  else
34
87
  context.public_send(cond)
35
88
  end
@@ -38,6 +91,30 @@ module FastSerializer
38
91
 
39
92
  res
40
93
  end
94
+
95
+ private
96
+
97
+ def call_proc_binding_to_context(context, prc, arity, resource, params)
98
+ case arity
99
+ when 1
100
+ context.instance_exec(resource, &prc)
101
+ when 2
102
+ context.instance_exec(resource, params, &prc)
103
+ when 0
104
+ context.instance_exec(&prc)
105
+ end
106
+ end
107
+
108
+ def call_method_on_context(context, method_name, arity, resource, params)
109
+ case arity
110
+ when 0
111
+ context.public_send(method_name)
112
+ when 1
113
+ context.public_send(method_name, resource)
114
+ when 2
115
+ context.public_send(method_name, resource, params)
116
+ end
117
+ end
41
118
  end
42
119
  end
43
120
  end
@@ -3,7 +3,7 @@
3
3
  module FastSerializer
4
4
  module JsonModel
5
5
  class HasManyRelationship < Relationship
6
- def serialize(resource, params = {}, context = nil)
6
+ def serialize(resource, params, context)
7
7
  collection = resource.public_send(method)
8
8
  return if collection.nil?
9
9
 
@@ -3,7 +3,7 @@
3
3
  module FastSerializer
4
4
  module JsonModel
5
5
  class HasOneRelationship < Relationship
6
- def serialize(resource, params = {}, context = nil)
6
+ def serialize(resource, params, context)
7
7
  relation = resource.public_send(method)
8
8
 
9
9
  if @serializer_klass
@@ -11,11 +11,15 @@ module FastSerializer
11
11
  @opts = opts || {}
12
12
  end
13
13
 
14
- def serialize(_resource, _params = {}, context = nil)
14
+ def injectable?
15
+ false
16
+ end
17
+
18
+ def serialize(_resource, _params, context = nil)
15
19
  raise NotImplementedError
16
20
  end
17
21
 
18
- def included?(_resource, _params = {}, context = nil)
22
+ def included?(_resource, _params, context = nil)
19
23
  raise NotImplementedError
20
24
  end
21
25
  end
@@ -5,7 +5,7 @@ module FastSerializer
5
5
  class Object < Node
6
6
  attr_accessor :attributes
7
7
 
8
- def initialize(*args)
8
+ def initialize(args = {})
9
9
  super
10
10
  @attributes = {}
11
11
  end
@@ -14,18 +14,20 @@ module FastSerializer
14
14
  attributes[attribute.key] = attribute
15
15
  end
16
16
 
17
- def serialize(resource, params = {}, context = nil)
17
+ def serialize(resource, params, context)
18
18
  return if resource.nil?
19
19
 
20
- attributes.values.each_with_object({}) do |attribute, res|
20
+ res = attributes.values.each_with_object({}) do |attribute, res|
21
21
  next res unless attribute.included?(resource, params, context)
22
22
 
23
23
  val = attribute.serialize(resource, params, context)
24
24
  res[attribute.key] = val
25
25
  end
26
+
27
+ res
26
28
  end
27
29
 
28
- def included?(_resource, _params = {}, context = nil)
30
+ def included?(*)
29
31
  true
30
32
  end
31
33
  end
@@ -5,7 +5,7 @@ module FastSerializer
5
5
  class Relationship < Attribute
6
6
  attr_accessor :serialization_schema
7
7
 
8
- def initialize(key: nil, method: nil, opts: {}, serializer: nil, schema: nil)
8
+ def initialize(key: nil, method: nil, opts: {}, serializer: nil, schema: nil, strict: false)
9
9
  super
10
10
  @serializer_klass = serializer
11
11
  @schema = schema
@@ -5,14 +5,16 @@ require 'forwardable'
5
5
  module FastSerializer
6
6
 
7
7
  class Schema
8
- Context = Struct.new(:resource, :params)
9
8
 
10
- attr_accessor :_root, :serialization_schema, :params
9
+ attr_reader :_root, :serialization_schema, :params, :strict
11
10
 
12
- def initialize(params = {})
13
- @root = nil
14
- @serialization_schema = JsonModel::Object.new
15
- @params = (params || {}).symbolize_keys
11
+ public
12
+
13
+ def initialize(params = {}, root = nil, strict = nil)
14
+ @root = root
15
+ @strict = strict || FastSerializer.config.strict
16
+ @serialization_schema = FastSerializer::JsonModel::Object.new
17
+ @params = FastSerializer::Utils.symbolize_keys(params || {})
16
18
  @params[:self] = self
17
19
  @params[:include_index] = {}
18
20
  @params[:exclude_index] = {}
@@ -24,7 +26,6 @@ module FastSerializer
24
26
  def include=(list)
25
27
  return if !list
26
28
 
27
-
28
29
  if list.any?
29
30
  @params[:include] = list.map(&:to_sym)
30
31
  @params[:include_index] = @params[:include].map { |key| [key, nil] }.to_h
@@ -45,7 +46,7 @@ module FastSerializer
45
46
  attribute_names.each do |attribute_name|
46
47
  serialization_schema.add_attribute JsonModel::Attribute.new(
47
48
  key: attribute_name,
48
- method: attribute_name
49
+ method: attribute_name,
49
50
  )
50
51
  end
51
52
  end
@@ -57,7 +58,7 @@ module FastSerializer
57
58
  serialization_schema.add_attribute JsonModel::Attribute.new(
58
59
  key: attribute_name,
59
60
  method: block,
60
- opts: opts
61
+ opts: opts,
61
62
  )
62
63
  end
63
64
 
@@ -69,7 +70,7 @@ module FastSerializer
69
70
  method: opts.delete(:method) || attribute_name,
70
71
  opts: opts,
71
72
  schema: opts.delete(:schema),
72
- serializer: opts.delete(:serializer)
73
+ serializer: opts.delete(:serializer),
73
74
  )
74
75
  end
75
76
 
@@ -95,19 +96,17 @@ module FastSerializer
95
96
  method: attribute_name,
96
97
  opts: opts,
97
98
  schema: opts.delete(:schema),
98
- serializer: opts.delete(:serializer)
99
+ serializer: opts.delete(:serializer),
99
100
  )
100
101
  end
101
102
 
102
103
  # @param [String] root_key - a key under which serialization result is nested
103
104
  def root(root_key)
104
- self._root = root_key
105
+ @_root = root_key
105
106
  end
106
107
 
107
108
  def deep_copy
108
- schema = FastSerializer::Schema.new
109
- schema.params = params
110
- schema._root = _root
109
+ schema = FastSerializer::Schema.new(params, _root, strict)
111
110
 
112
111
  serialization_schema.attributes.each do |key, attribute|
113
112
  schema.serialization_schema.attributes[key] = attribute
@@ -117,7 +116,7 @@ module FastSerializer
117
116
  end
118
117
 
119
118
  def serialize_resource(resource, params = {}, context = self)
120
- _params_dup = self.params.merge(params).symbolize_keys
119
+ _params_dup = FastSerializer::Utils.symbolize_keys(self.params.merge(params))
121
120
  meta = _params_dup.delete(:meta)
122
121
 
123
122
  is_collection = resource.respond_to?(:size) && !resource.respond_to?(:each_pair)
@@ -150,52 +149,5 @@ module FastSerializer
150
149
  def serialize_resource_to_json(resource, params = {}, context = self)
151
150
  FastSerializer.config.coder.dump(serialize_resource(resource, params, context))
152
151
  end
153
-
154
- module Mixin
155
-
156
- module ClassMethods
157
- attr_accessor :__schema__
158
-
159
- def inherited(subclass)
160
- subclass.__schema__ = self.__schema__.deep_copy
161
- end
162
-
163
- def method_missing(method, *args, &block)
164
- if __schema__.respond_to?(method)
165
- __schema__.public_send(method, *args, &block)
166
- else
167
- super
168
- end
169
- end
170
- end
171
-
172
- module InstanceMethods
173
- attr_accessor :resource, :params
174
-
175
- def initialize(resource, params = {})
176
- self.resource = resource
177
- self.params = params || {}
178
- end
179
-
180
- def serializable_hash(opts = {})
181
- self.params = params.merge(opts)
182
- self.class.__schema__.serialize_resource(resource, params, self)
183
- end
184
-
185
- def serialized_json(opts = {})
186
- self.params = params.merge(opts)
187
- self.class.__schema__.serialize_resource_to_json(resource, params, self)
188
- end
189
-
190
- alias as_json serializable_hash
191
- alias to_json serialized_json
192
- end
193
-
194
- def self.included(base)
195
- base.extend ClassMethods
196
- base.include InstanceMethods
197
- base.__schema__ = FastSerializer::Schema.new
198
- end
199
- end
200
152
  end
201
153
  end
@@ -0,0 +1,60 @@
1
+ module FastSerializer
2
+ class Schema
3
+ module Mixin
4
+
5
+ module ClassMethods
6
+ attr_accessor :__schema__, :__patched__
7
+
8
+ def inherited(subclass)
9
+ subclass.__schema__ = self.__schema__.deep_copy
10
+ end
11
+
12
+ def method_missing(method, *args, &block)
13
+ if __schema__.respond_to?(method)
14
+ __schema__.public_send(method, *args, &block)
15
+ else
16
+ super
17
+ end
18
+ end
19
+ end
20
+
21
+ module InstanceMethods
22
+ attr_accessor :resource, :params
23
+
24
+ def initialize(resource, params = {})
25
+ self.resource = resource
26
+ self.params = params || {}
27
+ end
28
+
29
+ alias_method :object, :resource
30
+
31
+ def serializable_hash(opts = {})
32
+ self.params = params.merge(opts)
33
+
34
+ if !self.class.__patched__
35
+ injectable_attributes = self.class.__schema__.serialization_schema.attributes.select { |key, attribute| attribute.injectable? }
36
+ injectable_attributes.each { |key, attribute| attribute.inject(self.class) }
37
+ self.class.__patched__ = true
38
+ self.class.__patched__.freeze
39
+ end
40
+
41
+ self.class.__schema__.serialize_resource(resource, params, self)
42
+ end
43
+
44
+ def serialized_json(opts = {})
45
+ self.params = params.merge(opts)
46
+ self.class.__schema__.serialize_resource_to_json(resource, params, self)
47
+ end
48
+
49
+ alias as_json serializable_hash
50
+ alias to_json serialized_json
51
+ end
52
+
53
+ def self.included(base)
54
+ base.extend ClassMethods
55
+ base.include InstanceMethods
56
+ base.__schema__ = FastSerializer::Schema.new
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,9 @@
1
+ module FastSerializer
2
+ module Utils
3
+ def self.symbolize_keys(hash)
4
+ res = {}
5
+ hash.each { |key, _| res[key.to_sym] = hash[key] }
6
+ hash
7
+ end
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FastSerializer
4
- VERSION = '0.4.2'
4
+ VERSION = '0.6.0'
5
5
  end
metadata CHANGED
@@ -1,142 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_serializer_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeny Stepanov
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-17 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: active_model_serializers
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 0.10.0
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: 0.10.0
27
- - !ruby/object:Gem::Dependency
28
- name: activesupport
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "<"
32
- - !ruby/object:Gem::Version
33
- version: '6'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "<"
39
- - !ruby/object:Gem::Version
40
- version: '6'
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: faker
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: '10.0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: '10.0'
97
- - !ruby/object:Gem::Dependency
98
- name: rspec
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - "~>"
102
- - !ruby/object:Gem::Version
103
- version: '3.0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: '3.0'
111
- - !ruby/object:Gem::Dependency
112
- name: rspec-benchmark
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - ">="
116
- - !ruby/object:Gem::Version
117
- version: '0'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - ">="
123
- - !ruby/object:Gem::Version
124
- version: '0'
125
- - !ruby/object:Gem::Dependency
126
- name: simplecov
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - ">="
130
- - !ruby/object:Gem::Version
131
- version: '0'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - ">="
137
- - !ruby/object:Gem::Version
138
- version: '0'
139
- description: fast_serializer is a lightweight ruby objects serializer.
11
+ date: 2020-02-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This library intends to solve such a typical and on the other hand important
14
+ problem as efficient ruby object to hash transformation.
140
15
  email:
141
16
  - estepnv@icloud.com
142
17
  executables: []
@@ -166,6 +41,8 @@ files:
166
41
  - lib/fast_serializer/json_model/object.rb
167
42
  - lib/fast_serializer/json_model/relationship.rb
168
43
  - lib/fast_serializer/schema.rb
44
+ - lib/fast_serializer/schema/mixin.rb
45
+ - lib/fast_serializer/utils.rb
169
46
  - lib/fast_serializer/version.rb
170
47
  homepage: https://github.com/estepnv/fast_serializer
171
48
  licenses:
@@ -182,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
182
59
  requirements:
183
60
  - - ">="
184
61
  - !ruby/object:Gem::Version
185
- version: '0'
62
+ version: 2.3.0
186
63
  required_rubygems_version: !ruby/object:Gem::Requirement
187
64
  requirements:
188
65
  - - ">="
@@ -192,5 +69,5 @@ requirements: []
192
69
  rubygems_version: 3.0.4
193
70
  signing_key:
194
71
  specification_version: 4
195
- summary: Yet another serializer gem, but fast
72
+ summary: fast_serializer is a lightweight ruby objects serializer.
196
73
  test_files: []