adequate_serialization 0.1.1 → 1.0.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: 5c44c471509c73b1edc5a18d3ccfdf9e05643ce234b10be8a5a2e3eadaa89ab4
4
- data.tar.gz: 290271ba291a48c85f7e4cc5691d32056f2c3215cf6a5af069f26a5b63a7e4ad
3
+ metadata.gz: de42ee593fdcb4fafa1b10217deb4ca0668925680d9d7280e928a02e968a11c4
4
+ data.tar.gz: 71233f85c7cac26fd422f9e71f24b53fc063edae544284c9d07751c97f988c70
5
5
  SHA512:
6
- metadata.gz: 81aaf4709e2f4d6e0546f8212ee01ebf3fcf0b55d66d8f54c2f16e763eb4853d36154fada8e2f1f311a49cf15aa8cae6f094e2f90861e5aeb3ffc4fe7fff12ad
7
- data.tar.gz: 9e510a610e415d090c0ee430d3add1b3ddd4d08a8e762d35f5f8d8bd8f6580a3039eba0bd7bfd3d766b4680e5bb3e2261701ae3dbbc3637ef3da936a6c71e9a5
6
+ metadata.gz: 3422db6a91f8474ccc00420458e4f0d62fac1611a78b62264e73fea5f1fdfdaac9017d247f8242a8e29f2917f933ddd189985e359f8bd6426b52807f544d0466
7
+ data.tar.gz: 528cf6375346a18ad6ed69684319f48027753d9d5ad742936e8759d83a04dcba21d2a2d8cc1710fdb40fd326d1a5f10ea8b269dc89fee5d0f20c3c07841ae9b9
@@ -0,0 +1,40 @@
1
+ workflow "Main" {
2
+ on = "push"
3
+ resolves = "Publish"
4
+ }
5
+
6
+ action "Install" {
7
+ uses = "docker://culturehq/actions-bundler:latest"
8
+ args = "install"
9
+ }
10
+
11
+ action "Audit" {
12
+ needs = "Install"
13
+ uses = "docker://culturehq/actions-bundler:latest"
14
+ args = "exec bundle audit"
15
+ }
16
+
17
+ action "Lint" {
18
+ needs = "Install"
19
+ uses = "docker://culturehq/actions-bundler:latest"
20
+ args = "exec rubocop --parallel"
21
+ }
22
+
23
+ action "Test" {
24
+ needs = "Install"
25
+ uses = "docker://culturehq/actions-bundler:latest"
26
+ args = "exec rake test"
27
+ }
28
+
29
+ action "Tag" {
30
+ needs = ["Audit", "Lint", "Test"]
31
+ uses = "actions/bin/filter@master"
32
+ args = "tag"
33
+ }
34
+
35
+ action "Publish" {
36
+ needs = "Tag"
37
+ uses = "docker://culturehq/actions-bundler:latest"
38
+ args = "build release:rubygem_push"
39
+ secrets = ["BUNDLE_GEM__PUSH_KEY"]
40
+ }
data/.rubocop.yml CHANGED
@@ -1,7 +1,10 @@
1
1
  AllCops:
2
2
  DisplayCopNames: true
3
3
  DisplayStyleGuide: true
4
- TargetRubyVersion: 2.5
4
+ TargetRubyVersion: 2.6
5
+
6
+ Lint/AmbiguousBlockAssociation:
7
+ Enabled: false
5
8
 
6
9
  Style/Documentation:
7
10
  Enabled: false
data/CHANGELOG.md CHANGED
@@ -6,9 +6,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.0.0] - 2019-03-25
10
+ ### Added
11
+ - The ability to define serializers inline in the object they're serializing.
12
+
13
+ ### Changed
14
+ - Renamed the `AdequateSerialization::Steps::PassthroughStep` class to just `AdequateSerialization::Steps::Step`.
15
+
16
+ ### Removed
17
+ - `AdequateSerialization.hook_into_rails!` is now no longer necessary as we assume you want to if `::Rails` is defined.
18
+
9
19
  ## [0.1.1] - 2018-08-30
10
20
  ### Changed
11
21
  - No longer trigger another query when the `ActiveRecord` relation being serialized isn't loaded.
12
22
 
13
- [Unreleased]: https://github.com/CultureHQ/client/compare/v0.1.1...HEAD
14
- [0.1.1]: https://github.com/CultureHQ/client/compare/v0.1.0...v0.1.1
23
+ [Unreleased]: https://github.com/CultureHQ/adequate_serialization/compare/v1.0.0...HEAD
24
+ [1.0.0]: https://github.com/CultureHQ/adequate_serialization/compare/v0.1.1...v1.0.0
25
+ [0.1.1]: https://github.com/CultureHQ/adequate_serialization/compare/v0.1.0...v0.1.1
data/Gemfile CHANGED
@@ -4,5 +4,5 @@ source 'https://rubygems.org'
4
4
 
5
5
  gemspec
6
6
 
7
- gem 'rails', '~> 5', '>= 5.2'
8
- gem 'sqlite3'
7
+ gem 'rails', '~> 5.2'
8
+ gem 'sqlite3', '= 1.3.13' # can't be upgraded until next version of rails
data/Gemfile.lock CHANGED
@@ -1,48 +1,48 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- adequate_serialization (0.1.1)
4
+ adequate_serialization (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- actioncable (5.2.1)
10
- actionpack (= 5.2.1)
9
+ actioncable (5.2.2.1)
10
+ actionpack (= 5.2.2.1)
11
11
  nio4r (~> 2.0)
12
12
  websocket-driver (>= 0.6.1)
13
- actionmailer (5.2.1)
14
- actionpack (= 5.2.1)
15
- actionview (= 5.2.1)
16
- activejob (= 5.2.1)
13
+ actionmailer (5.2.2.1)
14
+ actionpack (= 5.2.2.1)
15
+ actionview (= 5.2.2.1)
16
+ activejob (= 5.2.2.1)
17
17
  mail (~> 2.5, >= 2.5.4)
18
18
  rails-dom-testing (~> 2.0)
19
- actionpack (5.2.1)
20
- actionview (= 5.2.1)
21
- activesupport (= 5.2.1)
19
+ actionpack (5.2.2.1)
20
+ actionview (= 5.2.2.1)
21
+ activesupport (= 5.2.2.1)
22
22
  rack (~> 2.0)
23
23
  rack-test (>= 0.6.3)
24
24
  rails-dom-testing (~> 2.0)
25
25
  rails-html-sanitizer (~> 1.0, >= 1.0.2)
26
- actionview (5.2.1)
27
- activesupport (= 5.2.1)
26
+ actionview (5.2.2.1)
27
+ activesupport (= 5.2.2.1)
28
28
  builder (~> 3.1)
29
29
  erubi (~> 1.4)
30
30
  rails-dom-testing (~> 2.0)
31
31
  rails-html-sanitizer (~> 1.0, >= 1.0.3)
32
- activejob (5.2.1)
33
- activesupport (= 5.2.1)
32
+ activejob (5.2.2.1)
33
+ activesupport (= 5.2.2.1)
34
34
  globalid (>= 0.3.6)
35
- activemodel (5.2.1)
36
- activesupport (= 5.2.1)
37
- activerecord (5.2.1)
38
- activemodel (= 5.2.1)
39
- activesupport (= 5.2.1)
35
+ activemodel (5.2.2.1)
36
+ activesupport (= 5.2.2.1)
37
+ activerecord (5.2.2.1)
38
+ activemodel (= 5.2.2.1)
39
+ activesupport (= 5.2.2.1)
40
40
  arel (>= 9.0)
41
- activestorage (5.2.1)
42
- actionpack (= 5.2.1)
43
- activerecord (= 5.2.1)
41
+ activestorage (5.2.2.1)
42
+ actionpack (= 5.2.2.1)
43
+ activerecord (= 5.2.2.1)
44
44
  marcel (~> 0.3.1)
45
- activesupport (5.2.1)
45
+ activesupport (5.2.2.1)
46
46
  concurrent-ruby (~> 1.0, >= 1.0.2)
47
47
  i18n (>= 0.7, < 2)
48
48
  minitest (~> 5.1)
@@ -50,72 +50,75 @@ GEM
50
50
  arel (9.0.0)
51
51
  ast (2.4.0)
52
52
  builder (3.2.3)
53
- concurrent-ruby (1.0.5)
53
+ bundler-audit (0.6.1)
54
+ bundler (>= 1.2.0, < 3)
55
+ thor (~> 0.18)
56
+ concurrent-ruby (1.1.5)
54
57
  crass (1.0.4)
55
58
  docile (1.3.1)
56
- erubi (1.7.1)
57
- globalid (0.4.1)
59
+ erubi (1.8.0)
60
+ globalid (0.4.2)
58
61
  activesupport (>= 4.2.0)
59
- i18n (1.1.0)
62
+ i18n (1.6.0)
60
63
  concurrent-ruby (~> 1.0)
61
- jaro_winkler (1.5.1)
62
- json (2.1.0)
63
- loofah (2.2.2)
64
+ jaro_winkler (1.5.2)
65
+ json (2.2.0)
66
+ loofah (2.2.3)
64
67
  crass (~> 1.0.2)
65
68
  nokogiri (>= 1.5.9)
66
- mail (2.7.0)
69
+ mail (2.7.1)
67
70
  mini_mime (>= 0.1.1)
68
- marcel (0.3.2)
71
+ marcel (0.3.3)
69
72
  mimemagic (~> 0.3.2)
70
- method_source (0.9.0)
71
- mimemagic (0.3.2)
73
+ method_source (0.9.2)
74
+ mimemagic (0.3.3)
72
75
  mini_mime (1.0.1)
73
- mini_portile2 (2.3.0)
76
+ mini_portile2 (2.4.0)
74
77
  minitest (5.11.3)
75
78
  nio4r (2.3.1)
76
- nokogiri (1.8.4)
77
- mini_portile2 (~> 2.3.0)
78
- parallel (1.12.1)
79
- parser (2.5.1.2)
79
+ nokogiri (1.10.2)
80
+ mini_portile2 (~> 2.4.0)
81
+ parallel (1.15.0)
82
+ parser (2.6.2.0)
80
83
  ast (~> 2.4.0)
81
- powerpack (0.1.2)
82
- rack (2.0.5)
84
+ psych (3.1.0)
85
+ rack (2.0.6)
83
86
  rack-test (1.1.0)
84
87
  rack (>= 1.0, < 3)
85
- rails (5.2.1)
86
- actioncable (= 5.2.1)
87
- actionmailer (= 5.2.1)
88
- actionpack (= 5.2.1)
89
- actionview (= 5.2.1)
90
- activejob (= 5.2.1)
91
- activemodel (= 5.2.1)
92
- activerecord (= 5.2.1)
93
- activestorage (= 5.2.1)
94
- activesupport (= 5.2.1)
88
+ rails (5.2.2.1)
89
+ actioncable (= 5.2.2.1)
90
+ actionmailer (= 5.2.2.1)
91
+ actionpack (= 5.2.2.1)
92
+ actionview (= 5.2.2.1)
93
+ activejob (= 5.2.2.1)
94
+ activemodel (= 5.2.2.1)
95
+ activerecord (= 5.2.2.1)
96
+ activestorage (= 5.2.2.1)
97
+ activesupport (= 5.2.2.1)
95
98
  bundler (>= 1.3.0)
96
- railties (= 5.2.1)
99
+ railties (= 5.2.2.1)
97
100
  sprockets-rails (>= 2.0.0)
98
101
  rails-dom-testing (2.0.3)
99
102
  activesupport (>= 4.2.0)
100
103
  nokogiri (>= 1.6)
101
104
  rails-html-sanitizer (1.0.4)
102
105
  loofah (~> 2.2, >= 2.2.2)
103
- railties (5.2.1)
104
- actionpack (= 5.2.1)
105
- activesupport (= 5.2.1)
106
+ railties (5.2.2.1)
107
+ actionpack (= 5.2.2.1)
108
+ activesupport (= 5.2.2.1)
106
109
  method_source
107
110
  rake (>= 0.8.7)
108
111
  thor (>= 0.19.0, < 2.0)
109
112
  rainbow (3.0.0)
110
- rake (12.3.1)
111
- rubocop (0.58.2)
113
+ rake (12.3.2)
114
+ rubocop (0.66.0)
112
115
  jaro_winkler (~> 1.5.1)
113
116
  parallel (~> 1.10)
114
117
  parser (>= 2.5, != 2.5.1.1)
115
- powerpack (~> 0.1)
118
+ psych (>= 3.1.0)
116
119
  rainbow (>= 2.2.2, < 4.0)
117
120
  ruby-progressbar (~> 1.7)
118
- unicode-display_width (~> 1.0, >= 1.0.1)
121
+ unicode-display_width (>= 1.4.0, < 1.6)
119
122
  ruby-progressbar (1.10.0)
120
123
  simplecov (0.16.1)
121
124
  docile (~> 1.1)
@@ -130,11 +133,11 @@ GEM
130
133
  activesupport (>= 4.0)
131
134
  sprockets (>= 3.0.0)
132
135
  sqlite3 (1.3.13)
133
- thor (0.20.0)
136
+ thor (0.20.3)
134
137
  thread_safe (0.3.6)
135
138
  tzinfo (1.2.5)
136
139
  thread_safe (~> 0.1)
137
- unicode-display_width (1.4.0)
140
+ unicode-display_width (1.5.0)
138
141
  websocket-driver (0.7.0)
139
142
  websocket-extensions (>= 0.1.0)
140
143
  websocket-extensions (0.1.3)
@@ -144,13 +147,14 @@ PLATFORMS
144
147
 
145
148
  DEPENDENCIES
146
149
  adequate_serialization!
147
- bundler (~> 1)
150
+ bundler (~> 2)
151
+ bundler-audit (~> 0.6)
148
152
  minitest (~> 5)
149
- rails (~> 5, >= 5.2)
153
+ rails (~> 5.2)
150
154
  rake (~> 12)
151
- rubocop (~> 0.58)
155
+ rubocop (~> 0.66)
152
156
  simplecov (~> 0.16)
153
- sqlite3
157
+ sqlite3 (= 1.3.13)
154
158
 
155
159
  BUNDLED WITH
156
- 1.16.1
160
+ 2.0.1
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2018 CultureHQ
3
+ Copyright (c) 2019 CultureHQ
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,6 +1,5 @@
1
1
  # AdequateSerialization
2
2
 
3
- [![Build Status](https://travis-ci.org/CultureHQ/adequate_serialization.svg?branch=master)](https://travis-ci.org/CultureHQ/adequate_serialization)
4
3
  [![Gem Version](https://img.shields.io/gem/v/adequate_serialization.svg)](https://github.com/CultureHQ/adeqaute_serialization)
5
4
 
6
5
  Serializes objects adequately. `AdequateSerialization` allows you to define serializers that will convert your objects into simple hashes that are suitable for variable purposes such as caching or using in an HTTP response. It stems from the simple idea of giving slightly more control over the `as_json` method that gets called when objects are serialized using Rails' default controller serialization.
@@ -23,21 +22,29 @@ Or install it yourself as:
23
22
 
24
23
  ## Usage
25
24
 
26
- First, include `AdequateSerialization::Serializable` in any object that you want to be able to serialize. Then, define a serializer matching the name of that object, postfixed with `"Serializer"`. Use the `AdequateSerialization` DSL to define the attributes that are available to the serializer. You can then call `as_json` on any instance of that object to get the resultant hash. Below is an example:
25
+ There are two ways to define the serialization process for objects.
26
+
27
+ For larger objects where it makes sense to define the serialization in a separate class, you should include the `AdequateSerialization::Serializable` in the object that you want to be able to serialize. Then, define a serializer matching the name of that object, postfixed with `"Serializer"`, as in:
27
28
 
28
29
  ```ruby
29
- class User
30
- include AdequateSerialization::Serializable
30
+ class UserSerializer < AdequateSerialization::Serializer
31
+ attribute :id, :name, :title
32
+ end
33
+ ```
34
+
35
+ For smaller objects where it makes sense to define the serialization inline, you can include the result of the `AdequateSerialization::inline` method, as in:
31
36
 
32
- attr_reader :id, :name, :title
37
+ ```ruby
38
+ class User
39
+ include AdequateSerialization.inline { attribute :id, :name, :title }
33
40
 
34
41
  ...
35
42
  end
43
+ ```
36
44
 
37
- class UserSerializer < AdequateSerialization::Serializer
38
- attribute :id, :name, :title
39
- end
45
+ For both types of serialization definition, you can then use the `AdequateSerialization` DSL to define the attributes that are available to the serializer. You can then call `as_json` on any instance of that object to get the resultant hash. Below is an example:
40
46
 
47
+ ```ruby
41
48
  User.new(id: 1, name: 'Clark Kent', title: 'Superman').as_json
42
49
  # => {:id=>1, :name=>"Clark Kent", :title=>"Superman"}
43
50
  ```
@@ -125,11 +132,11 @@ This relies on the objects to which you are attaching having an `id` attribute a
125
132
 
126
133
  ### Usage with Rails
127
134
 
128
- To get `AdequateSerializer` working with Rails, you can call `AdequateSerializion.hook_into_rails!` in an initializer. This will do three things:
135
+ If `::Rails` is defined when `adequate_serialization` is required, it will hook into the serialization process for `ActiveRecord` objects in three ways:
129
136
 
130
- 1. Introduce caching behavior so that when serializing objects they will by default be stored in the Rails cache.
131
- 1. Include `AdequateSerializer::Serializable` in `ActiveRecord::Base` so that all of your models will be serializable.
132
- 2. Overwrite `ActiveRecord::Relation`'s `as_json` method to use the `AdequateSerializer::Rails::RelationSerializer` object, which by default will use the `Rails.cache.fetch_multi` method in order to more efficiently serialize all of the records in the relation.
137
+ 1. By introducing caching behavior so that when serializing objects they will by default be stored in the Rails cache.
138
+ 2. By including `AdequateSerializer::Serializable` in `ActiveRecord::Base` so that all of your models will be serializable.
139
+ 3. By overwriting `ActiveRecord::Relation`'s `as_json` method to use the `AdequateSerializer::Rails::RelationSerializer` object, which by default will use the `Rails.cache.fetch_multi` method in order to more efficiently serialize all of the records in the relation.
133
140
 
134
141
  You can still use plain objects to be serialized, and if you want to take advantage of the caching behavior, you can define a `cache_key` method on the objects that you're serializing. This will cause `AdequateSerialization` to start putting them into the Rails cache.
135
142
 
@@ -164,7 +171,7 @@ and the result will now contain the `title` attribute (provided it was configure
164
171
  The serialization process happens through a series of `AdequateSerialization::Steps`. The caching behavior mentioned in the `Usage with Rails` section is one such step that gets introduced. You can introduce more yourself like so:
165
172
 
166
173
  ```ruby
167
- class LoggingStep < AdequateSerialization::Steps::PassthroughStep
174
+ class LoggingStep < AdequateSerialization::Steps::Step
168
175
  def apply(response)
169
176
  Logger.log("#{response.object} is being serialized with #{response.opts} options")
170
177
  apply_next(response)
@@ -21,9 +21,10 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ['lib']
23
23
 
24
- spec.add_development_dependency 'bundler', '~> 1'
24
+ spec.add_development_dependency 'bundler', '~> 2'
25
+ spec.add_development_dependency 'bundler-audit', '~> 0.6'
25
26
  spec.add_development_dependency 'minitest', '~> 5'
26
27
  spec.add_development_dependency 'rake', '~> 12'
27
- spec.add_development_dependency 'rubocop', '~> 0.58'
28
+ spec.add_development_dependency 'rubocop', '~> 0.66'
28
29
  spec.add_development_dependency 'simplecov', '~> 0.16'
29
30
  end
@@ -1,6 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AdequateSerialization
4
+ def self.dump(object)
5
+ if object.is_a?(Hash)
6
+ object
7
+ elsif object.respond_to?(:as_json)
8
+ object.as_json
9
+ else
10
+ object
11
+ end
12
+ end
13
+
4
14
  module Attribute
5
15
  class Simple
6
16
  attr_reader :name
@@ -38,6 +48,7 @@ module AdequateSerialization
38
48
 
39
49
  def serialize_to(serializer, response, model, includes)
40
50
  return unless model.public_send(condition)
51
+
41
52
  attribute.serialize_to(serializer, response, model, includes)
42
53
  end
43
54
  end
@@ -52,6 +63,7 @@ module AdequateSerialization
52
63
 
53
64
  def serialize_to(serializer, response, model, includes)
54
65
  return if model.public_send(condition)
66
+
55
67
  attribute.serialize_to(serializer, response, model, includes)
56
68
  end
57
69
  end
@@ -65,6 +77,7 @@ module AdequateSerialization
65
77
 
66
78
  def serialize_to(serializer, response, model, includes)
67
79
  return unless includes.include?(attribute.name)
80
+
68
81
  attribute.serialize_to(serializer, response, model, includes)
69
82
  end
70
83
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AdequateSerialization
4
+ # With this module, you can define serializers inline in the object that
5
+ # they're serializing. You use it with the `AdequateSerialization::inline`
6
+ # method to define the serializer dynamically, as in:
7
+ #
8
+ # class User
9
+ # attr_reader :id, :name, :title
10
+ #
11
+ # def initialize(id:, name:, title: nil)
12
+ # @id = id
13
+ # @name = name
14
+ # @title = title
15
+ # end
16
+ #
17
+ # include AdequateSerialization.inline {
18
+ # attribute :id, :name
19
+ # attribute :title, optional: true
20
+ # }
21
+ # end
22
+ #
23
+ # user = User.new(id: 1, name: 'Clark Kent')
24
+ # user.as_json
25
+ # # => {:id=>1, :name=>"Clark Kent"}
26
+ #
27
+ # user = User.new(id: 2, name: 'Diana Prince', title: 'Wonder Woman')
28
+ # user.as_json(includes: :title)
29
+ # # => {:id=>1, :name=>"Diana Prince", :title=>"Wonder Woman"}
30
+ class InlineSerializer < Module
31
+ attr_reader :block
32
+
33
+ def initialize(&block)
34
+ @block = block
35
+ end
36
+
37
+ def included(base)
38
+ base.include(Serializable)
39
+
40
+ # No need to memoize within the method because the block will hold on to
41
+ # local variables for us.
42
+ serializer = Class.new(Serializer, &block).new
43
+ base.define_singleton_method(:serializer) { serializer }
44
+ end
45
+ end
46
+
47
+ def self.inline(&block)
48
+ InlineSerializer.new(&block)
49
+ end
50
+ end
@@ -2,7 +2,21 @@
2
2
 
3
3
  module AdequateSerialization
4
4
  module Rails
5
- class CacheStep < Steps::PassthroughStep
5
+ module CacheKey
6
+ def self.cacheable?(object)
7
+ if object.class < ActiveRecord::Base
8
+ object.has_attribute?(:updated_at)
9
+ else
10
+ object.respond_to?(:cache_key)
11
+ end
12
+ end
13
+
14
+ def self.for(object, includes = [])
15
+ includes.empty? ? object : [object, *includes]
16
+ end
17
+ end
18
+
19
+ class CacheStep < Steps::Step
6
20
  def apply(response)
7
21
  object = response.object
8
22
  opts = response.opts
@@ -16,5 +30,7 @@ module AdequateSerialization
16
30
  end
17
31
  end
18
32
  end
33
+
34
+ AdequateSerialization.prepend(CacheStep)
19
35
  end
20
36
  end
@@ -38,6 +38,7 @@ module AdequateSerialization
38
38
  def cache_keys_for(opts)
39
39
  relation.map do |record|
40
40
  return nil unless CacheKey.cacheable?(record)
41
+
41
42
  CacheKey.for(record, opts.includes)
42
43
  end
43
44
  end
@@ -63,5 +64,13 @@ module AdequateSerialization
63
64
  record.class.serializer.serialize(record, opts)
64
65
  end
65
66
  end
67
+
68
+ module RelationHook
69
+ def as_json(*options)
70
+ RelationSerializer.new(self).as_json(*options)
71
+ end
72
+ end
73
+
74
+ ActiveRecord::Relation.include(RelationHook)
66
75
  end
67
76
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module AdequateSerialization
4
4
  module Steps
5
- class SerializeStep < PassthroughStep
5
+ class SerializeStep < Step
6
6
  def apply(response)
7
7
  object = response.object
8
8
  serialized = object.class.serializer.serialize(object, response.opts)
@@ -2,16 +2,10 @@
2
2
 
3
3
  module AdequateSerialization
4
4
  module Steps
5
- class LastStep
6
- def apply(response)
7
- response.object
8
- end
9
- end
10
-
11
- class PassthroughStep
5
+ class Step
12
6
  attr_reader :next_step
13
7
 
14
- def initialize(next_step = LastStep.new)
8
+ def initialize(next_step = :last)
15
9
  @next_step = next_step
16
10
  end
17
11
 
@@ -22,7 +16,7 @@ module AdequateSerialization
22
16
  private
23
17
 
24
18
  def apply_next(response)
25
- next_step.apply(response)
19
+ next_step == :last ? response.object : next_step.apply(response)
26
20
  end
27
21
  end
28
22
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AdequateSerialization
4
- VERSION = '0.1.1'
4
+ VERSION = '1.0.0'
5
5
  end
@@ -2,33 +2,18 @@
2
2
 
3
3
  require 'adequate_serialization/attribute'
4
4
  require 'adequate_serialization/decorator'
5
+ require 'adequate_serialization/inline_serializer'
5
6
  require 'adequate_serialization/options'
6
7
  require 'adequate_serialization/serializable'
7
8
  require 'adequate_serialization/serializer'
8
9
  require 'adequate_serialization/steps'
9
10
  require 'adequate_serialization/version'
10
11
 
11
- require 'adequate_serialization/steps/passthrough_step'
12
+ require 'adequate_serialization/steps/step'
12
13
  require 'adequate_serialization/steps/serialize_step'
13
14
 
14
- module AdequateSerialization
15
- class << self
16
- def dump(object)
17
- if object.is_a?(Hash)
18
- object
19
- elsif object.respond_to?(:as_json)
20
- object.as_json
21
- else
22
- object
23
- end
24
- end
25
-
26
- def hook_into_rails!
27
- @hook_into_rails ||=
28
- begin
29
- require 'adequate_serialization/rails/hook'
30
- Rails.hook_in!
31
- end
32
- end
33
- end
15
+ if defined?(::Rails)
16
+ require 'adequate_serialization/rails/cache_step'
17
+ require 'adequate_serialization/rails/relation_serializer'
18
+ ActiveRecord::Base.include(AdequateSerialization::Serializable)
34
19
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adequate_serialization
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Deisz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-30 00:00:00.000000000 Z
11
+ date: 2019-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1'
19
+ version: '2'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1'
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler-audit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.6'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: minitest
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -58,14 +72,14 @@ dependencies:
58
72
  requirements:
59
73
  - - "~>"
60
74
  - !ruby/object:Gem::Version
61
- version: '0.58'
75
+ version: '0.66'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
- version: '0.58'
82
+ version: '0.66'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: simplecov
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -87,9 +101,9 @@ executables: []
87
101
  extensions: []
88
102
  extra_rdoc_files: []
89
103
  files:
104
+ - ".github/main.workflow"
90
105
  - ".gitignore"
91
106
  - ".rubocop.yml"
92
- - ".travis.yml"
93
107
  - CHANGELOG.md
94
108
  - Gemfile
95
109
  - Gemfile.lock
@@ -102,16 +116,15 @@ files:
102
116
  - lib/adequate_serialization.rb
103
117
  - lib/adequate_serialization/attribute.rb
104
118
  - lib/adequate_serialization/decorator.rb
119
+ - lib/adequate_serialization/inline_serializer.rb
105
120
  - lib/adequate_serialization/options.rb
106
- - lib/adequate_serialization/rails/cache_key.rb
107
121
  - lib/adequate_serialization/rails/cache_step.rb
108
- - lib/adequate_serialization/rails/hook.rb
109
122
  - lib/adequate_serialization/rails/relation_serializer.rb
110
123
  - lib/adequate_serialization/serializable.rb
111
124
  - lib/adequate_serialization/serializer.rb
112
125
  - lib/adequate_serialization/steps.rb
113
- - lib/adequate_serialization/steps/passthrough_step.rb
114
126
  - lib/adequate_serialization/steps/serialize_step.rb
127
+ - lib/adequate_serialization/steps/step.rb
115
128
  - lib/adequate_serialization/version.rb
116
129
  homepage: https://github.com/CultureHQ/adequate_serialization
117
130
  licenses:
@@ -132,8 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
145
  - !ruby/object:Gem::Version
133
146
  version: '0'
134
147
  requirements: []
135
- rubyforge_project:
136
- rubygems_version: 2.7.6
148
+ rubygems_version: 3.0.3
137
149
  signing_key:
138
150
  specification_version: 4
139
151
  summary: Serializes objects adequately
data/.travis.yml DELETED
@@ -1,10 +0,0 @@
1
- language: ruby
2
- rvm: 2.5.1
3
- cache: bundler
4
- branches:
5
- only:
6
- - master
7
- before_install:
8
- - gem update --system
9
- - gem install bundler
10
- script: bundle exec rake && bundle exec rubocop --parallel
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module AdequateSerialization
4
- module Rails
5
- module CacheKey
6
- def self.cacheable?(object)
7
- if object.class < ActiveRecord::Base
8
- object.has_attribute?(:updated_at)
9
- else
10
- object.respond_to?(:cache_key)
11
- end
12
- end
13
-
14
- def self.for(object, includes = [])
15
- includes.empty? ? object : [object, *includes]
16
- end
17
- end
18
- end
19
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'adequate_serialization/rails/cache_key'
4
- require 'adequate_serialization/rails/cache_step'
5
- require 'adequate_serialization/rails/relation_serializer'
6
-
7
- module AdequateSerialization
8
- module Rails
9
- module RelationHook
10
- def as_json(*options)
11
- RelationSerializer.new(self).as_json(*options)
12
- end
13
- end
14
-
15
- def self.hook_in!
16
- ActiveRecord::Base.include(Serializable)
17
- ActiveRecord::Relation.include(RelationHook)
18
- AdequateSerialization.prepend(CacheStep)
19
- end
20
- end
21
- end