as_json_representations 0.4.1 → 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: d489e6472aa1956629b9a73dfa7fe239e1ae9cd3d794f23a69085da4909bcead
4
- data.tar.gz: a1eff31bbd91cc4ba2c04cc2518b5b4a2e90864fa20161230f21b4d4882fd0a0
3
+ metadata.gz: a2411f12b165485b579cf9650b2ee1de6d197daa4fdbea9ecd952b31b0c68a8f
4
+ data.tar.gz: 3c4e24fb9e8d229ee9177e605535cc8ab2bdd6a968b15d1e611f1abea1965821
5
5
  SHA512:
6
- metadata.gz: e1c6f1861aecfac8d575788cc90acb4d313dc51e2f045968f26819741f0a5260c8366c5a734ff5f0697891218736f5a18eb28da2b8a532e95640aea1579555be
7
- data.tar.gz: d7a542c0ddda417baad9977535c42160ebc8880a56d22491b11ad35b0cf708736450080ec34787cf3632b525b0300042819f1a93368c72228e46004b1de73f54
6
+ metadata.gz: ab50b7d16894eb166c95a833d433b8138380b9f5eb51ff01719d61a32b550e623e1801bcb413abee8e3c63accfeabc2fe616316199e9989b7c027b91f6cf4543
7
+ data.tar.gz: 294d76aaeb638dbe1fa55c04c89300a0c376c4af9739d19d72c7bd450562f08fac89efb85e7c798eeb773fae0a68dbefe70feb6b159fa94a42ba9fa8e3b9cb50
data/.gitignore CHANGED
@@ -9,4 +9,5 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ .byebug_history
12
13
  *.gem
data/Gemfile CHANGED
@@ -1,6 +1,8 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in as_json_representations.gemspec
6
6
  gemspec
7
+
8
+ gem 'byebug'
@@ -1,13 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- as_json_representations (0.4.1)
4
+ as_json_representations (0.6.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ byebug (11.0.1)
9
10
  diff-lcs (1.3)
10
- rake (10.5.0)
11
+ rake (13.0.1)
11
12
  rspec (3.7.0)
12
13
  rspec-core (~> 3.7.0)
13
14
  rspec-expectations (~> 3.7.0)
@@ -28,8 +29,9 @@ PLATFORMS
28
29
  DEPENDENCIES
29
30
  as_json_representations!
30
31
  bundler (~> 1.15)
31
- rake (~> 10.0)
32
+ byebug
33
+ rake (~> 13.0)
32
34
  rspec (~> 3.0)
33
35
 
34
36
  BUNDLED WITH
35
- 1.16.4
37
+ 1.17.3
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.
@@ -22,6 +22,6 @@ Gem::Specification.new do |spec|
22
22
  spec.require_paths = ['lib']
23
23
 
24
24
  spec.add_development_dependency 'bundler', '~> 1.15'
25
- spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rake', '~> 13.0'
26
26
  spec.add_development_dependency 'rspec', '~> 3.0'
27
27
  end
@@ -1,22 +1,34 @@
1
1
  require 'as_json_representations/collection.rb'
2
2
 
3
3
  module AsJsonRepresentations
4
+ QUERY_METHODS = %i[includes eager_load preload].freeze
5
+
4
6
  module ClassMethods
5
7
  def representation(name, options={}, &block)
6
8
  @representations ||= {}
7
9
  @representations[name] = options.merge(name: name, class: self, block: block)
10
+
11
+ # copy parent representation options that should be inherited
12
+ return unless options[:extend]
13
+ extend_representation_name = options[:extend] == true ? name : options[:extend]
14
+ extend_representation = (parent_entity || self).representations[extend_representation_name]
15
+
16
+ QUERY_METHODS.each do |option|
17
+ next unless (extend_option_value = extend_representation[option])
18
+ @representations[name][option] = extend_option_value + (options[option] || [])
19
+ end
8
20
  end
9
21
 
10
22
  def representations
11
23
  @representations
12
24
  end
13
25
 
14
- def parent
15
- @parent
26
+ def parent_entity
27
+ @parent_entity
16
28
  end
17
29
 
18
30
  def find_representation(name)
19
- representations[name] || @parent&.find_representation(name) if name
31
+ representations[name] || @parent_entity&.find_representation(name) if name
20
32
  end
21
33
 
22
34
  def render_representation(object, options)
@@ -32,7 +44,7 @@ module AsJsonRepresentations
32
44
 
33
45
  representation =
34
46
  if representation[:extend] == true
35
- representation[:class].parent&.find_representation(representation[:name])
47
+ representation[:class].parent_entity&.find_representation(representation[:name])
36
48
  else
37
49
  find_representation(representation[:extend])
38
50
  end
@@ -51,7 +63,7 @@ module AsJsonRepresentations
51
63
  if !options[:representation] && defined?(super)
52
64
  super(options)
53
65
  else
54
- #{base}.render_representation(self, options)
66
+ #{base}.render_representation(self, options.dup)
55
67
  end
56
68
  end
57
69
  }
@@ -61,9 +73,13 @@ module AsJsonRepresentations
61
73
  end
62
74
 
63
75
  def self.included(base)
64
- return unless base.class == Module
65
- AsJsonRepresentations.send(:included, base)
66
- base.instance_variable_set :@parent, self
76
+ if base.class == Module
77
+ AsJsonRepresentations.send(:included, base)
78
+ base.instance_variable_set :@parent_entity, self
79
+ else
80
+ context = self
81
+ base.define_singleton_method(:representations) { context.representations }
82
+ end
67
83
  end
68
84
  end
69
85
  end
@@ -1,13 +1,33 @@
1
1
  module AsJsonRepresentations
2
2
  module Collection
3
- def as_json(options={})
4
- map do |item|
5
- item.respond_to?(:as_json) ? item.as_json(options) : item
6
- end
7
- end
8
-
9
3
  def representation(name, options={})
10
4
  as_json(options.merge(representation: name))
11
5
  end
6
+
7
+ def self.included(base)
8
+ base.class_eval do
9
+ def as_json(options={})
10
+ subject = self
11
+
12
+ if respond_to?(:klass) && klass.respond_to?(:representations)
13
+ # call supported methods of ActiveRecord::QueryMethods
14
+ QUERY_METHODS.each do |method|
15
+ next unless respond_to? method
16
+
17
+ args = klass.representations.dig(options[:representation], method)
18
+
19
+ # we need to reassign because ActiveRecord returns new object
20
+ subject = subject.public_send(method, args) if args
21
+ end
22
+ end
23
+
24
+ return super if respond_to? :super
25
+
26
+ subject.map do |item|
27
+ item.respond_to?(:as_json) ? item.as_json(options) : item
28
+ end
29
+ end
30
+ end
31
+ end
12
32
  end
13
33
  end
@@ -1,3 +1,3 @@
1
1
  module AsJsonRepresentations
2
- VERSION = '0.4.1'
2
+ VERSION = '0.6.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.1
4
+ version: 0.6.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-24 00:00:00.000000000 Z
11
+ date: 2020-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '13.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '13.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -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.1.2
97
96
  signing_key:
98
97
  specification_version: 4
99
98
  summary: Creates representations of your model data