pluck_map 1.0.0.rc1 → 1.0.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +13 -0
- data/lib/pluck_map/attribute_builder.rb +4 -2
- data/lib/pluck_map/presenter.rb +1 -1
- data/lib/pluck_map/presenters/to_json.rb +4 -0
- data/lib/pluck_map/relationships/base.rb +4 -17
- data/lib/pluck_map/structured_attribute.rb +37 -0
- data/lib/pluck_map/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83dba25350831a0d7be00ac7b2682113c512eccc6197265aa32033b7a5df70ff
|
4
|
+
data.tar.gz: faaf95d3df9d484497fed6e260fcef0bccac91d13f92a29cb9616771becb5ee6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de5ad2aed4440bd810880f6200db09b2e97550c73c39716bf18d3afb060f44e4b95bd7da8b3a1fdc74ac6e022be1ea59518bc79f51113351952a7e9cedf9f979
|
7
|
+
data.tar.gz: 0ee7091b52d833d4da0bf3588549857ec371f81731a269ae51d52879c2f74a163b07cd89cec866dd22f5acec0c05b30fbeebebc5dab558cabc2da65b53975026
|
data/CHANGELOG.md
CHANGED
@@ -1,9 +1,17 @@
|
|
1
|
+
## v1.0.0.rc2 (2019 Jun 17)
|
2
|
+
|
3
|
+
* FEATURE: Add structured attributes to allow nesting attributes as a hash (@kobsy)
|
4
|
+
|
1
5
|
## v1.0.0.rc1 (2019 May 12)
|
2
6
|
|
3
7
|
* BREAKING: Remove deprecated features/methods (@boblail)
|
4
8
|
* FEATURE: Optimize `to_json` when a presenter doesn't need to process values in Ruby (@boblail)
|
5
9
|
* FEATURE: Add `has_many` and `has_one` DSL for presenting nested resources (@boblail)
|
6
10
|
|
11
|
+
## v0.6.2 (2019 Jun 13)
|
12
|
+
|
13
|
+
* FIX: Allow presenting a subclass of the presenter's model (@kobsy)
|
14
|
+
|
7
15
|
## v0.6.1 (2019 May 12)
|
8
16
|
|
9
17
|
* FIX: Aliased SQL expressions to work-around a bug with Rails Postgres adapter (@kobsy)
|
data/README.md
CHANGED
@@ -208,6 +208,19 @@ presenter = PluckMap[Person].define do
|
|
208
208
|
end
|
209
209
|
```
|
210
210
|
|
211
|
+
### Structured attributes
|
212
|
+
|
213
|
+
You can also nest attributes by passing a block to the attribute method:
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
presenter = PluckMap[Person].define do
|
217
|
+
parent do
|
218
|
+
id select: :parent_id
|
219
|
+
type "Parent"
|
220
|
+
end
|
221
|
+
end
|
222
|
+
```
|
223
|
+
|
211
224
|
### Relationships
|
212
225
|
|
213
226
|
PluckMap can also describe nested data. There are two special methods in the `define` block that introduce child resources:
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require "pluck_map/attribute"
|
2
|
+
require "pluck_map/structured_attribute"
|
2
3
|
require "pluck_map/attributes"
|
3
4
|
require "pluck_map/relationships"
|
4
5
|
|
@@ -21,10 +22,11 @@ module PluckMap
|
|
21
22
|
@model = model
|
22
23
|
end
|
23
24
|
|
24
|
-
def method_missing(attribute_name, *args)
|
25
|
+
def method_missing(attribute_name, *args, &block)
|
25
26
|
options = args.extract_options!
|
26
27
|
options[:value] = args.first unless args.empty?
|
27
|
-
@attributes.push Attribute.new(attribute_name, @model, options)
|
28
|
+
@attributes.push block.nil? ? Attribute.new(attribute_name, @model, options) :
|
29
|
+
StructuredAttribute.new(attribute_name, @model, block, options)
|
28
30
|
:attribute_added
|
29
31
|
end
|
30
32
|
|
data/lib/pluck_map/presenter.rb
CHANGED
@@ -51,6 +51,10 @@ module PluckMap
|
|
51
51
|
arg
|
52
52
|
end
|
53
53
|
|
54
|
+
def prepare_PluckMap_StructuredAttribute(attribute)
|
55
|
+
to_json_object(attribute.attributes)
|
56
|
+
end
|
57
|
+
|
54
58
|
def prepare_PluckMap_Relationships_Many(attribute)
|
55
59
|
PluckMap::JsonSubqueryAggregate.new(attribute.scope, to_json_object(attribute.attributes))
|
56
60
|
end
|
@@ -1,27 +1,14 @@
|
|
1
|
-
require "pluck_map/
|
1
|
+
require "pluck_map/structured_attribute"
|
2
2
|
|
3
3
|
module PluckMap
|
4
4
|
module Relationships
|
5
|
-
class Base <
|
6
|
-
attr_reader :
|
5
|
+
class Base < StructuredAttribute
|
6
|
+
attr_reader :scope
|
7
7
|
|
8
8
|
def initialize(attribute_name, scope, block, options)
|
9
9
|
@scope = scope
|
10
|
-
@attributes = AttributeBuilder.build(model: scope.klass, &block)
|
11
10
|
@scope = @scope.instance_exec(&options[:scope_block]) if options[:scope_block]
|
12
|
-
|
13
|
-
select: build_select,
|
14
|
-
map: build_map)
|
15
|
-
|
16
|
-
super(attribute_name, scope.klass, options)
|
17
|
-
end
|
18
|
-
|
19
|
-
def will_map?
|
20
|
-
attributes.any?(&:will_map?)
|
21
|
-
end
|
22
|
-
|
23
|
-
def nested?
|
24
|
-
true
|
11
|
+
super(attribute_name, scope.klass, block, options)
|
25
12
|
end
|
26
13
|
|
27
14
|
protected
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "pluck_map/attribute"
|
2
|
+
|
3
|
+
module PluckMap
|
4
|
+
class StructuredAttribute < Attribute
|
5
|
+
attr_reader :attributes
|
6
|
+
|
7
|
+
def initialize(attribute_name, model, block, options={})
|
8
|
+
@attributes = AttributeBuilder.build(model: model, &block)
|
9
|
+
options = options.slice(:as).merge(select: build_select, map: build_map)
|
10
|
+
super(attribute_name, model, options)
|
11
|
+
end
|
12
|
+
|
13
|
+
def will_map?
|
14
|
+
attributes.any?(&:will_map?)
|
15
|
+
end
|
16
|
+
|
17
|
+
def nested?
|
18
|
+
true
|
19
|
+
end
|
20
|
+
|
21
|
+
protected
|
22
|
+
|
23
|
+
def build_select
|
24
|
+
attributes.selects
|
25
|
+
end
|
26
|
+
|
27
|
+
def build_map
|
28
|
+
lambda do |*values|
|
29
|
+
return nil if values.none?
|
30
|
+
attributes.each_with_object({}) do |attribute, hash|
|
31
|
+
hash[attribute.name] = attribute.exec(values)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
data/lib/pluck_map/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pluck_map
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.
|
4
|
+
version: 1.0.0.rc2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bob Lail
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-06-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -275,6 +275,7 @@ files:
|
|
275
275
|
- lib/pluck_map/relationships/many.rb
|
276
276
|
- lib/pluck_map/relationships/one.rb
|
277
277
|
- lib/pluck_map/relationships/polymorphic_one.rb
|
278
|
+
- lib/pluck_map/structured_attribute.rb
|
278
279
|
- lib/pluck_map/version.rb
|
279
280
|
- lib/pluck_map/visitors.rb
|
280
281
|
- lib/pluck_map/visitors/mysql.rb
|