fast_serializer_ruby 0.2.1 → 0.6.3

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: 4226b5f0bc68c749226425f93db90d60bfe5b0cba0faa74a77e9cfd24680a73b
4
- data.tar.gz: 697304fc0c88cdd9848c974027689c5eb4a0f90daa3e95fb4c2d2e723ceb8040
3
+ metadata.gz: 8d8ad37dfcd016a3db647dd533569d8d9f70850eb2bf6fe825867175eb9b6dc2
4
+ data.tar.gz: cf9225cb0900f487bbe5b066127dfde74aec433e79eddc9303d008b4242f30f4
5
5
  SHA512:
6
- metadata.gz: ad76357998ec5b0de5768057e6dd6191f4cc8f7eba5dfc9170206245570ad725ba822f1bbf5d35dfed5364227f6dd28130aaadc70717158f42d80d906f76f98b
7
- data.tar.gz: 2a43e81fe55e7d4a8dd56b56a489030d38f4d810564b332ee0f965b0d16b3152622a5aab97779dfa45b8cfe276640b16da6fd1eb7c81c4a629bbb82939ab6e05
6
+ metadata.gz: b2168332398602023551252abc1d458cf3f37d1449d80a701ac316bd44d2270381e8cd70e72dc2aa52e54c4701167f1d4f30050be8a059822158d1de438bd285
7
+ data.tar.gz: 396590ae7497cc7eae403cf7db3f0a6a6045cefb833442ddfc750ad34077a8bdbf1d7ed9056d011b31af20b9d8e90d3fd402c46b6ae3a78735f34d441961ed99
data/.gitignore CHANGED
@@ -12,3 +12,6 @@
12
12
  # rspec failure tracking
13
13
  .rspec_status
14
14
  .rspec
15
+ .history
16
+ /.vscode
17
+ .rubocop.yml
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
@@ -18,7 +19,8 @@ matrix:
18
19
  - rvm: jruby-head
19
20
 
20
21
  before_install:
21
- - gem update --system -N
22
+ - yes | gem update --system --force
23
+ - gem install bundler
22
24
 
23
25
  before_script:
24
26
  - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
@@ -34,5 +36,5 @@ cache: bundler
34
36
 
35
37
  env:
36
38
  global:
37
- CC_TEST_REPORTER_ID=a051d3d3560aa2e58cc5efb9f7b8eaafe87f983a1c4d6cb1573fda50ede5b685
39
+ CC_TEST_REPORTER_ID=99325a42b78ef51a9d51574d20dbcd6acdfe75c8568f1433f73b61f17149e05d
38
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,22 @@ 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, :test do
10
+ gem 'rake'
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 'activesupport', '< 6'
19
+ gem 'allocation_stats'
20
+ gem 'simplecov', '~> 0.17.1'
21
+ gem 'benchmark-memory', '~> 0.1'
22
+ gem 'rspec', '~> 3.0'
23
+ gem 'rspec-benchmark'
24
+ gem 'pry-byebug'
25
+ end
data/Gemfile.lock CHANGED
@@ -1,20 +1,20 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fast_serializer_ruby (0.2.0)
4
+ fast_serializer_ruby (0.6.2)
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,44 +24,49 @@ 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
- benchmark-malloc (0.1.0)
35
- benchmark-perf (0.5.0)
36
- benchmark-trend (0.3.0)
37
- builder (3.2.3)
34
+ allocation_stats (0.1.5)
35
+ benchmark-malloc (0.2.0)
36
+ benchmark-memory (0.1.2)
37
+ memory_profiler (~> 0.9)
38
+ benchmark-perf (0.6.0)
39
+ benchmark-trend (0.4.0)
40
+ builder (3.2.4)
41
+ byebug (11.1.3)
38
42
  case_transform (0.2)
39
43
  activesupport
40
44
  coderay (1.1.2)
41
- concurrent-ruby (1.1.5)
42
- crass (1.0.5)
45
+ concurrent-ruby (1.1.6)
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.2)
53
+ i18n (>= 1.6, < 2)
54
+ ffi (1.12.2-java)
55
+ i18n (1.8.2)
52
56
  concurrent-ruby (~> 1.0)
53
- json (2.2.0)
54
- json (2.2.0-java)
57
+ json (2.3.0)
58
+ json (2.3.0-java)
55
59
  jsonapi-renderer (0.2.2)
56
- loofah (2.3.1)
60
+ loofah (2.4.0)
57
61
  crass (~> 1.0.2)
58
62
  nokogiri (>= 1.5.9)
63
+ memory_profiler (0.9.14)
59
64
  method_source (0.9.2)
60
65
  mini_portile2 (2.4.0)
61
- minitest (5.12.2)
62
- nokogiri (1.10.5)
66
+ minitest (5.14.0)
67
+ nokogiri (1.10.9)
63
68
  mini_portile2 (~> 2.4.0)
64
- nokogiri (1.10.5-java)
69
+ nokogiri (1.10.9-java)
65
70
  pry (0.12.2)
66
71
  coderay (~> 1.1.0)
67
72
  method_source (~> 0.9.0)
@@ -69,7 +74,10 @@ GEM
69
74
  coderay (~> 1.1.0)
70
75
  method_source (~> 0.9.0)
71
76
  spoon (~> 0.0)
72
- rack (2.0.7)
77
+ pry-byebug (3.8.0)
78
+ byebug (~> 11.0)
79
+ pry (~> 0.10)
80
+ rack (2.2.2)
73
81
  rack-test (1.1.0)
74
82
  rack (>= 1.0, < 3)
75
83
  rails-dom-testing (2.0.3)
@@ -77,25 +85,25 @@ GEM
77
85
  nokogiri (>= 1.6)
78
86
  rails-html-sanitizer (1.3.0)
79
87
  loofah (~> 2.3)
80
- rake (10.5.0)
88
+ rake (13.0.1)
81
89
  rspec (3.9.0)
82
90
  rspec-core (~> 3.9.0)
83
91
  rspec-expectations (~> 3.9.0)
84
92
  rspec-mocks (~> 3.9.0)
85
- rspec-benchmark (0.5.1)
86
- benchmark-malloc (~> 0.1.0)
87
- benchmark-perf (~> 0.5.0)
88
- benchmark-trend (~> 0.3.0)
89
- rspec (>= 3.0.0, < 4.0.0)
90
- rspec-core (3.9.0)
91
- rspec-support (~> 3.9.0)
92
- rspec-expectations (3.9.0)
93
+ rspec-benchmark (0.6.0)
94
+ benchmark-malloc (~> 0.2)
95
+ benchmark-perf (~> 0.6)
96
+ benchmark-trend (~> 0.4)
97
+ rspec (>= 3.0)
98
+ rspec-core (3.9.1)
99
+ rspec-support (~> 3.9.1)
100
+ rspec-expectations (3.9.1)
93
101
  diff-lcs (>= 1.2.0, < 2.0)
94
102
  rspec-support (~> 3.9.0)
95
- rspec-mocks (3.9.0)
103
+ rspec-mocks (3.9.1)
96
104
  diff-lcs (>= 1.2.0, < 2.0)
97
105
  rspec-support (~> 3.9.0)
98
- rspec-support (3.9.0)
106
+ rspec-support (3.9.2)
99
107
  simplecov (0.17.1)
100
108
  docile (~> 1.1)
101
109
  json (>= 1.8, < 3)
@@ -105,7 +113,7 @@ GEM
105
113
  ffi
106
114
  thread_safe (0.3.6)
107
115
  thread_safe (0.3.6-java)
108
- tzinfo (1.2.5)
116
+ tzinfo (1.2.6)
109
117
  thread_safe (~> 0.1)
110
118
 
111
119
  PLATFORMS
@@ -115,14 +123,17 @@ PLATFORMS
115
123
  DEPENDENCIES
116
124
  active_model_serializers (~> 0.10.0)
117
125
  activesupport (< 6)
126
+ allocation_stats
127
+ benchmark-memory (~> 0.1)
118
128
  factory_bot
119
129
  faker
120
130
  fast_serializer_ruby!
121
131
  pry
122
- rake (~> 10.0)
132
+ pry-byebug
133
+ rake
123
134
  rspec (~> 3.0)
124
135
  rspec-benchmark
125
- simplecov
136
+ simplecov (~> 0.17.1)
126
137
 
127
138
  BUNDLED WITH
128
- 2.0.2
139
+ 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,16 +1,25 @@
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
+ ## Performance 🚀
12
+ - running on ruby 2.7 is **at least 6 times faster** than AMS (benchmarks was borrowed from fast_jsonapi repository)
13
+ - running on ruby 2.7 it consumes **6 times less RAM**
14
+ - running on jruby 9.2.7.0 **is at least 4 times faster** than AMS after warming up
11
15
 
12
- - running on ruby 2.6 is **at least 3 times faster** than AMS (benchmarks was borrowed from fast_jsonapi repository)
13
- - running on jruby 9.2.7.0 **is at least 15 times faster** than AMS after warming up
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
14
23
 
15
24
 
16
25
  ## Installation
@@ -18,7 +27,7 @@ That's why it's so performant.
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,8 @@ 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
+
37
+ spec.bindir = 'bin'
35
38
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
39
  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
40
  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,17 +1,29 @@
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
+ attr_reader :coder
8
+ attr_accessor :strict
6
9
 
7
10
  def initialize
8
11
  @coder = JSON
12
+ @strict = false
13
+ end
14
+
15
+ def coder=(obj)
16
+ unless obj.respond_to?(:dump) && obj.respond_to?(:load)
17
+ raise ArgumentError, 'must respond to #load and #dump methods'
18
+ end
19
+
20
+ @coder = obj
9
21
  end
10
22
  end
11
23
 
12
24
  class << self
13
25
  def config
14
- @_config ||= Configuration.new
26
+ @config ||= Configuration.new
15
27
  end
16
28
 
17
29
  def configure(&block)