as_json_representations 0.4.3 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7303933b1ec6f554b43cb2c14c09325faf4cbd50935537da910d1a8069d6cfb6
4
- data.tar.gz: d1420afac1861f0e044544bb9a48cebdd55ec77875e9487eb99175406a2cdad3
3
+ metadata.gz: daa006efc48883832cee9320fdd7c6f97857739664befca6e43a9926b7e32f87
4
+ data.tar.gz: aca509444daca9ce848074ddab33a5e7db143d5bc4aef381fb55e8c62f208fc5
5
5
  SHA512:
6
- metadata.gz: e701364c44f827f43decab103fd106b00a30cb691c30fcfc1de2182b6e52c75f855c074118f1eedcdcf307ead5d0d4541f0730e4ee4208fcb29a2447cba47c43
7
- data.tar.gz: f6c989bd434fc0c17f3ba6cbe23dec6d59c1ee58ae33589648854d0e8017ed65e890aa36e317cbdc47ca802c577ffb767194586c1e054c6d3922f031a8b3da01
6
+ metadata.gz: 171f411f75287d344bfc1372dbde7fdaf97384c149ca704ef948eff4330d23fbb91e2f15057a42f3184e783a767025da2de26aaa931e3e68c062a25dcdcd372c
7
+ data.tar.gz: 0257036af2c8696a39a11afe994523c178185c705a00bb448cbfd04125cd457701420c3c122cad7605ca14ddd6cc89f1039742e18f5176c3c22d51d962b94840
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- as_json_representations (0.4.3)
4
+ as_json_representations (0.5.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -32,4 +32,4 @@ DEPENDENCIES
32
32
  rspec (~> 3.0)
33
33
 
34
34
  BUNDLED WITH
35
- 1.16.4
35
+ 1.17.2
data/README.md CHANGED
@@ -4,6 +4,18 @@
4
4
 
5
5
  Creates representations of your model data in a simple and clean way.
6
6
 
7
+ ## Features
8
+
9
+ * Easy to use
10
+ * Easy to define representations
11
+ * Support representations hinheritance
12
+ * Support module definition
13
+ * Support options
14
+ * Support ActiveRecord collection options
15
+ * Faster
16
+
17
+ The performance of this gem is up to **7x higher** than other for a simple class instance serialization: [test](https://github.com/rjurado01/as_json_representations_benchmarks)
18
+
7
19
  ## Installation
8
20
 
9
21
  Add this line to your application's Gemfile:
@@ -138,6 +150,23 @@ When you includes representation module (parent) into other module (child):
138
150
  * You can extend parent representations
139
151
  * You must use `extend: true` when use the same name
140
152
 
153
+ ## ActiveRecord collection options
154
+
155
+ You can use this ActiveRecord option when you define a representation:
156
+
157
+ * [includes](https://apidock.com/rails/ActiveRecord/QueryMethods/includes)
158
+
159
+ ```
160
+ representation :private, includes: [:city]
161
+ {
162
+ age: age,
163
+ city: city.as_json(representation: :basic)
164
+ }
165
+ end
166
+ ```
167
+
168
+ This options will be used on the collection before to serializing it automatically.
169
+
141
170
  ## Development
142
171
 
143
172
  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.
@@ -5,6 +5,16 @@ module AsJsonRepresentations
5
5
  def representation(name, options={}, &block)
6
6
  @representations ||= {}
7
7
  @representations[name] = options.merge(name: name, class: self, block: block)
8
+
9
+ # copy parent representation options that should be inherited
10
+ return unless options[:extend]
11
+ extend_representation_name = options[:extend] == true ? name : options[:extend]
12
+ extend_representation = (parent_entity || self).representations[extend_representation_name]
13
+
14
+ %i[includes eager_load].each do |option|
15
+ next unless (extend_option_value = extend_representation[option])
16
+ @representations[name][option] = extend_option_value + (options[option] || [])
17
+ end
8
18
  end
9
19
 
10
20
  def representations
@@ -51,7 +61,7 @@ module AsJsonRepresentations
51
61
  if !options[:representation] && defined?(super)
52
62
  super(options)
53
63
  else
54
- #{base}.render_representation(self, options)
64
+ #{base}.render_representation(self, options.dup)
55
65
  end
56
66
  end
57
67
  }
@@ -61,9 +71,13 @@ module AsJsonRepresentations
61
71
  end
62
72
 
63
73
  def self.included(base)
64
- return unless base.class == Module
65
- AsJsonRepresentations.send(:included, base)
66
- base.instance_variable_set :@parent_entity, self
74
+ if base.class == Module
75
+ AsJsonRepresentations.send(:included, base)
76
+ base.instance_variable_set :@parent_entity, self
77
+ else
78
+ context = self
79
+ base.define_singleton_method(:representations) { context.representations }
80
+ end
67
81
  end
68
82
  end
69
83
  end
@@ -5,11 +5,23 @@ module AsJsonRepresentations
5
5
  end
6
6
 
7
7
  def self.included(base)
8
- return if base.respond_to? :as_json
9
-
10
8
  base.class_eval do
11
9
  def as_json(options={})
12
- map do |item|
10
+ subject = self
11
+
12
+ # call supported methods of ActiveRecord::QueryMethods
13
+ %i[includes eager_load].each do |method|
14
+ next unless respond_to? method
15
+
16
+ args = klass.representations.dig(options[:representation], method)
17
+
18
+ # we need to reassign because ActiveRecord returns new object
19
+ subject = subject.public_send(method, args) if args
20
+ end
21
+
22
+ return super if respond_to? :super
23
+
24
+ subject.map do |item|
13
25
  item.respond_to?(:as_json) ? item.as_json(options) : item
14
26
  end
15
27
  end
@@ -1,3 +1,3 @@
1
1
  module AsJsonRepresentations
2
- VERSION = '0.4.3'
2
+ VERSION = '0.5.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: as_json_representations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - rjurado01
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-27 00:00:00.000000000 Z
11
+ date: 2019-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -92,8 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
92
  - !ruby/object:Gem::Version
93
93
  version: '0'
94
94
  requirements: []
95
- rubyforge_project:
96
- rubygems_version: 2.7.3
95
+ rubygems_version: 3.0.3
97
96
  signing_key:
98
97
  specification_version: 4
99
98
  summary: Creates representations of your model data