as_json_representations 0.3.0 → 0.4.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: 8538dc805ef2b3830ae49cb42803e1ca8cfc18fd2baf5149a1c8c2d5ee804a84
4
- data.tar.gz: 0f8fe6df1adda47ff911753e502a830d096f698f00454369393afb149bb6eb29
3
+ metadata.gz: c281672043920d58c37a13f7d482d4cf28eed8cb1f76f6cdbe96c477715cebae
4
+ data.tar.gz: 1f8e071bd25a5206e738957fc7314ed0db9ed9708c72bee400cbd8bfcaf6e7e5
5
5
  SHA512:
6
- metadata.gz: 3bf53d9de42ac55e154b462b37b032e808fcc9c42be55a2faaa2609679226c9b33306fc04e86e72e5c0e2f1be6d0e02e9d6e4cdf12f26fd5a08ebbce98053064
7
- data.tar.gz: a718f409c924edd9e22fef11cc5b674f2d7957c38ae35c51e3d9ffe8452547cf13a6b3e222041ca647d56ea2e4c91ec6a1d2fd638930e0185179fed46acecc4d
6
+ metadata.gz: 0043c6b6ff892e063b7cb74ca7cb567d760ce034e48a7f198b5afa60afa379506c591963c8c4a83176fe45cbfd413fd2e4b4e30d198f2e77c330e6ea0566a2ca
7
+ data.tar.gz: 82b77be08e2b719af905fd33c90ff43954c32ca2705fc0ca02b670033a2d44c9f4d1036f1ed2836945cd02d06e8a69fef3685ce0679d74498c6be7e6a988d2ee
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- as_json_representations (0.3.0)
4
+ as_json_representations (0.4.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.2
35
+ 1.16.4
data/README.md CHANGED
@@ -84,6 +84,60 @@ user.representation(:private, date: '2017-12-21') # short form
84
84
  # {:full_name=>"John Doe", :date=>"2017-12-21", :age=>30, :city=>{:name=>"Madrid"}}
85
85
  ```
86
86
 
87
+ ## Modules Inheritance
88
+
89
+ You can include a module representation into other module like this example:
90
+
91
+ ```ruby
92
+ module ParentRepresentations
93
+ include AsJsonRepresentations
94
+
95
+ representation :a do {name: name} end
96
+ representation :b do {name: name} end
97
+ representation :c do {name: name} end
98
+ end
99
+
100
+ module ChildRepresentations
101
+ include ParentRepresentations
102
+
103
+ representation :a do # overwrite
104
+ {color: color}
105
+ end
106
+
107
+ representation :b, extend: true do # extend parent representation with same name
108
+ {color: color}
109
+ end
110
+
111
+ representation :d, extend: :c do # extend parent representation
112
+ {color: color}
113
+ end
114
+ end
115
+
116
+ class Child
117
+ include ChildRepresentations
118
+
119
+ attr_accessor :color
120
+
121
+ def initialize(name, color)
122
+ @name = name
123
+ @color = color
124
+ end
125
+ end
126
+
127
+ child = Child.new('child', 'red')
128
+ child.as_json(representation: :a) # {color: 'red'}
129
+ child.as_json(representation: :b) # {name: 'child', color: 'red'}
130
+ child.as_json(representation: :c) # {name: 'child'}
131
+ child.as_json(representation: :d) # {name: 'child', color: 'red'}
132
+ ```
133
+
134
+ When you includes representation module (parent) into other module (child):
135
+
136
+ * Parent representations are included
137
+ * If a representation is redefined, it is overwritten
138
+ * You can extend parent representations
139
+ * You must use `extend: true` when use the same name
140
+
87
141
  ## Development
88
142
 
89
143
  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.
@@ -1,28 +1,37 @@
1
+ require 'as_json_representations/collection.rb'
2
+
1
3
  module AsJsonRepresentations
2
4
  module ClassMethods
3
5
  def representation(name, options={}, &block)
4
6
  @representations ||= {}
5
- @representations[name] = options.merge(block: block)
7
+ @representations[name] = options.merge(name: name, block: block)
6
8
  end
7
9
 
8
10
  def representations
9
11
  @representations
10
12
  end
11
13
 
12
- def render_representation(object, options)
13
- name_representation = options.delete(:representation)&.to_sym
14
- return {} unless (representation = representations[name_representation])
14
+ def find_representation(name)
15
+ representations[name] || @parent&.find_representation(name) if name
16
+ end
15
17
 
16
- data = object.instance_exec(options, &representation[:block])
18
+ def render_representation(object, options)
19
+ representation_name = options.delete(:representation)&.to_sym
20
+ return {} unless (representation = find_representation(representation_name))
17
21
 
18
- while representation[:extend] && (representation = representations[representation[:extend]])
22
+ data = {}
23
+ loop do
19
24
  data = object.instance_exec(
20
25
  options,
21
26
  &representation[:block]
22
27
  ).merge(data)
23
- end
24
28
 
25
- data
29
+ representation = [representation[:name], true].include?(representation[:extend]) ?
30
+ @parent.find_representation(representation[:name]) :
31
+ find_representation(representation[:extend])
32
+
33
+ return data unless representation
34
+ end
26
35
  end
27
36
  end
28
37
 
@@ -43,6 +52,22 @@ module AsJsonRepresentations
43
52
  def representation(name, options={})
44
53
  as_json(options.merge(representation: name))
45
54
  end
55
+
56
+ def self.included(base)
57
+ return unless base.class == Module
58
+ AsJsonRepresentations.send(:included, base)
59
+ base.instance_variable_set :@parent, self
60
+ end
46
61
  end
47
62
  end
48
63
  end
64
+
65
+ Array.include(AsJsonRepresentations::Collection)
66
+
67
+ if defined?(Mongoid::Criteria)
68
+ Mongoid::Criteria.include(AsJsonRepresentations::Collection)
69
+ end
70
+
71
+ if defined?(ActiveRecord::Relation)
72
+ ActiveRecord::Relation.include(AsJsonRepresentations::Collection)
73
+ end
@@ -0,0 +1,13 @@
1
+ module AsJsonRepresentations
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
+ def representation(name, options={})
10
+ as_json(options.merge(representation: name))
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module AsJsonRepresentations
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.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.3.0
4
+ version: 0.4.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-08-07 00:00:00.000000000 Z
11
+ date: 2018-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,6 +71,7 @@ files:
71
71
  - bin/console
72
72
  - bin/setup
73
73
  - lib/as_json_representations.rb
74
+ - lib/as_json_representations/collection.rb
74
75
  - lib/as_json_representations/version.rb
75
76
  homepage: https://github.com/rjurado01/as_json_representations
76
77
  licenses:
@@ -92,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
93
  version: '0'
93
94
  requirements: []
94
95
  rubyforge_project:
95
- rubygems_version: 2.7.3
96
+ rubygems_version: 2.7.6
96
97
  signing_key:
97
98
  specification_version: 4
98
99
  summary: Creates representations of your model data