grape-entity 0.10.0 → 0.10.1
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 +4 -4
- data/.rspec +2 -1
- data/CHANGELOG.md +7 -0
- data/README.md +14 -0
- data/lib/grape_entity/delegator/base.rb +5 -0
- data/lib/grape_entity/delegator.rb +8 -4
- data/lib/grape_entity/entity.rb +1 -4
- data/lib/grape_entity/exposure.rb +6 -1
- data/lib/grape_entity/version.rb +1 -1
- data/spec/grape_entity/hash_spec.rb +15 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e3db71fd839585d80faac0980ec028b1204def600b6ce36101e2986e0be3cbf
|
4
|
+
data.tar.gz: 7bfcb78548991ed3ab73bfc1f23a51e1e32c2af7ccba472e87b9a767a9499c79
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ba1bf0a642f56275e8279b34d433591a21c7849ede46d0718fd6496831d4f5b5bd676bda0af6bf435beebe5c717ace6c0b53c1c90db92fdecccb8d88cdbe044
|
7
|
+
data.tar.gz: c06a650f7c29dd918fa8b83ab62dd04b91448bcddc440a43e48d055f5499982b6c4532f18abc7429f71f464895b083f8e551748c9d9fdf0f8072c2cd1af0e438
|
data/.rspec
CHANGED
data/CHANGELOG.md
CHANGED
@@ -9,6 +9,13 @@
|
|
9
9
|
* Your contribution here.
|
10
10
|
|
11
11
|
|
12
|
+
### 0.10.1 (2021-10-22)
|
13
|
+
|
14
|
+
#### Fixes
|
15
|
+
|
16
|
+
* [#359](https://github.com/ruby-grape/grape-entity/pull/359): Respect `hash_access` setting when using `expose_nil: false` option - [@magni-](https://github.com/magni-).
|
17
|
+
|
18
|
+
|
12
19
|
### 0.10.0 (2021-09-15)
|
13
20
|
|
14
21
|
#### Features
|
data/README.md
CHANGED
@@ -111,6 +111,20 @@ The field lookup takes several steps
|
|
111
111
|
* next try `object.fetch(exposure)`
|
112
112
|
* last raise an Exception
|
113
113
|
|
114
|
+
`exposure` is a Symbol by default. If `object` is a Hash with stringified keys, you can set the hash accessor at the entity-class level to properly expose its members:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
class Status < GrapeEntity
|
118
|
+
self.hash_access = :to_s
|
119
|
+
|
120
|
+
expose :code
|
121
|
+
expose :message
|
122
|
+
end
|
123
|
+
|
124
|
+
Status.represent({ 'code' => 418, 'message' => "I'm a teapot" }).as_json
|
125
|
+
#=> { code: 418, message: "I'm a teapot" }
|
126
|
+
```
|
127
|
+
|
114
128
|
#### Exposing with a Presenter
|
115
129
|
|
116
130
|
Don't derive your model classes from `Grape::Entity`, expose them using a presenter.
|
@@ -11,10 +11,14 @@ module Grape
|
|
11
11
|
module Delegator
|
12
12
|
def self.new(object)
|
13
13
|
delegator_klass =
|
14
|
-
if object.is_a?(Hash)
|
15
|
-
|
16
|
-
elsif object.
|
17
|
-
|
14
|
+
if object.is_a?(Hash)
|
15
|
+
HashObject
|
16
|
+
elsif defined?(OpenStruct) && object.is_a?(OpenStruct)
|
17
|
+
OpenStructObject
|
18
|
+
elsif object.respond_to?(:fetch, true)
|
19
|
+
FetchableObject
|
20
|
+
else
|
21
|
+
PlainObject
|
18
22
|
end
|
19
23
|
|
20
24
|
delegator_klass.new(object)
|
data/lib/grape_entity/entity.rb
CHANGED
@@ -481,9 +481,6 @@ module Grape
|
|
481
481
|
@object = object
|
482
482
|
@options = options.is_a?(Options) ? options : Options.new(options)
|
483
483
|
@delegator = Delegator.new(object)
|
484
|
-
|
485
|
-
# Why not `arity > 1`? It might be negative https://ruby-doc.org/core-2.6.6/Method.html#method-i-arity
|
486
|
-
@delegator_accepts_opts = @delegator.method(:delegate).arity != 1
|
487
484
|
end
|
488
485
|
|
489
486
|
def root_exposures
|
@@ -541,7 +538,7 @@ module Grape
|
|
541
538
|
def delegate_attribute(attribute)
|
542
539
|
if is_defined_in_entity?(attribute)
|
543
540
|
send(attribute)
|
544
|
-
elsif
|
541
|
+
elsif delegator.accepts_options?
|
545
542
|
delegator.delegate(attribute, **self.class.delegation_opts)
|
546
543
|
else
|
547
544
|
delegator.delegate(attribute)
|
@@ -56,7 +56,12 @@ module Grape
|
|
56
56
|
Condition.new_unless(
|
57
57
|
proc do |object, _options|
|
58
58
|
if options[:proc].nil?
|
59
|
-
Delegator.new(object)
|
59
|
+
delegator = Delegator.new(object)
|
60
|
+
if is_a?(Grape::Entity) && delegator.accepts_options?
|
61
|
+
delegator.delegate(attribute, **self.class.delegation_opts).nil?
|
62
|
+
else
|
63
|
+
delegator.delegate(attribute).nil?
|
64
|
+
end
|
60
65
|
else
|
61
66
|
exec_with_object(options, &options[:proc]).nil?
|
62
67
|
end
|
data/lib/grape_entity/version.rb
CHANGED
@@ -17,7 +17,7 @@ describe Grape::Entity do
|
|
17
17
|
expose :post, if: :full
|
18
18
|
expose :city
|
19
19
|
expose :street
|
20
|
-
expose :house
|
20
|
+
expose :house, expose_nil: false
|
21
21
|
end
|
22
22
|
|
23
23
|
class Company < Grape::Entity
|
@@ -62,9 +62,23 @@ describe Grape::Entity do
|
|
62
62
|
}
|
63
63
|
}
|
64
64
|
|
65
|
+
company_without_house_with_string = {
|
66
|
+
'full_name' => 'full_name',
|
67
|
+
'name' => 'name',
|
68
|
+
'address' => {
|
69
|
+
'post' => '123456',
|
70
|
+
'city' => 'city',
|
71
|
+
'street' => 'street',
|
72
|
+
'something_else' => 'something_else'
|
73
|
+
}
|
74
|
+
}
|
75
|
+
|
65
76
|
expect(EntitySpec::CompanyWithString.represent(company_with_string).serializable_hash).to eq \
|
66
77
|
company.slice(:name).merge(address: company[:address].slice(:city, :street, :house))
|
67
78
|
|
79
|
+
expect(EntitySpec::CompanyWithString.represent(company_without_house_with_string).serializable_hash).to eq \
|
80
|
+
company.slice(:name).merge(address: company[:address].slice(:city, :street))
|
81
|
+
|
68
82
|
expect(EntitySpec::CompanyWithString.represent(company_with_string, full: true).serializable_hash).to eq \
|
69
83
|
company.slice(:full_name, :name).merge(address: company[:address].slice(:city, :street, :house))
|
70
84
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-entity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.10.
|
4
|
+
version: 0.10.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Bleigh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|