grape-entity 0.6.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.coveralls.yml +1 -0
- data/.gitignore +5 -1
- data/.rspec +1 -1
- data/.rubocop.yml +124 -2
- data/.rubocop_todo.yml +21 -32
- data/.travis.yml +16 -17
- data/CHANGELOG.md +66 -0
- data/Dangerfile +2 -0
- data/Gemfile +8 -8
- data/Guardfile +4 -2
- data/README.md +101 -4
- data/Rakefile +2 -2
- data/bench/serializing.rb +7 -0
- data/grape-entity.gemspec +10 -8
- data/lib/grape-entity.rb +2 -0
- data/lib/grape_entity.rb +2 -0
- data/lib/grape_entity/condition.rb +20 -11
- data/lib/grape_entity/condition/base.rb +2 -0
- data/lib/grape_entity/condition/block_condition.rb +3 -1
- data/lib/grape_entity/condition/hash_condition.rb +2 -0
- data/lib/grape_entity/condition/symbol_condition.rb +2 -0
- data/lib/grape_entity/delegator.rb +10 -9
- data/lib/grape_entity/delegator/base.rb +2 -0
- data/lib/grape_entity/delegator/fetchable_object.rb +2 -0
- data/lib/grape_entity/delegator/hash_object.rb +4 -2
- data/lib/grape_entity/delegator/openstruct_object.rb +2 -0
- data/lib/grape_entity/delegator/plain_object.rb +2 -0
- data/lib/grape_entity/entity.rb +112 -38
- data/lib/grape_entity/exposure.rb +64 -41
- data/lib/grape_entity/exposure/base.rb +20 -6
- data/lib/grape_entity/exposure/block_exposure.rb +2 -0
- data/lib/grape_entity/exposure/delegator_exposure.rb +2 -0
- data/lib/grape_entity/exposure/formatter_block_exposure.rb +2 -0
- data/lib/grape_entity/exposure/formatter_exposure.rb +2 -0
- data/lib/grape_entity/exposure/nesting_exposure.rb +35 -30
- data/lib/grape_entity/exposure/nesting_exposure/nested_exposures.rb +25 -15
- data/lib/grape_entity/exposure/nesting_exposure/output_builder.rb +6 -2
- data/lib/grape_entity/exposure/represent_exposure.rb +3 -1
- data/lib/grape_entity/options.rb +44 -58
- data/lib/grape_entity/version.rb +3 -1
- data/spec/grape_entity/entity_spec.rb +243 -47
- data/spec/grape_entity/exposure/nesting_exposure/nested_exposures_spec.rb +6 -4
- data/spec/grape_entity/exposure/represent_exposure_spec.rb +5 -3
- data/spec/grape_entity/exposure_spec.rb +14 -2
- data/spec/grape_entity/hash_spec.rb +38 -1
- data/spec/grape_entity/options_spec.rb +66 -0
- data/spec/spec_helper.rb +17 -0
- metadata +31 -44
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fb3365afa36454c797c3d44adfa51ecc757a105fe6cb6644f3f3896ff06af48d
|
4
|
+
data.tar.gz: 1e3c91d73fb17727c931443910b2f602e037384eb4f04e055207bbaf471c3962
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba4bd4207a7c5f5f09806017b29723ba0f3ed0fc749cd360d55ebba9a41465165f91a7ae974d6af4baed71109c2ad5b7f19514a6f510953fe3733b46d35f405e
|
7
|
+
data.tar.gz: 4ac4db65df766dd004c35e926a6a43e08023d86b4477d6e396b8c9ce9b125ee889bbbe86fecf7f6f4a60f847c2ae5d38beb829ce017957737de7a52f0ab802d3
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.gitignore
CHANGED
data/.rspec
CHANGED
data/.rubocop.yml
CHANGED
@@ -1,6 +1,128 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
|
1
3
|
AllCops:
|
2
4
|
Exclude:
|
3
5
|
- vendor/**/*
|
4
|
-
-
|
6
|
+
- example/**/*
|
7
|
+
TargetRubyVersion: 2.7
|
5
8
|
|
6
|
-
|
9
|
+
# Layout stuff
|
10
|
+
#
|
11
|
+
Layout/EmptyLinesAroundArguments:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Layout/EmptyLinesAroundAttributeAccessor:
|
15
|
+
Enabled: true
|
16
|
+
|
17
|
+
Layout/FirstHashElementIndentation:
|
18
|
+
EnforcedStyle: consistent
|
19
|
+
|
20
|
+
Layout/LineLength:
|
21
|
+
Max: 120
|
22
|
+
Exclude:
|
23
|
+
- spec/**/*
|
24
|
+
|
25
|
+
Layout/SpaceAroundMethodCallOperator:
|
26
|
+
Enabled: true
|
27
|
+
|
28
|
+
# Lint stuff
|
29
|
+
#
|
30
|
+
Lint/DeprecatedOpenSSLConstant:
|
31
|
+
Enabled: true
|
32
|
+
|
33
|
+
Lint/DuplicateElsifCondition:
|
34
|
+
Enabled: true
|
35
|
+
|
36
|
+
Lint/MixedRegexpCaptureTypes:
|
37
|
+
Enabled: true
|
38
|
+
|
39
|
+
Lint/RaiseException:
|
40
|
+
Enabled: true
|
41
|
+
|
42
|
+
Lint/StructNewOverride:
|
43
|
+
Enabled: true
|
44
|
+
|
45
|
+
# Metrics stuff
|
46
|
+
#
|
47
|
+
Metrics/AbcSize:
|
48
|
+
Max: 25
|
49
|
+
|
50
|
+
Metrics/BlockLength:
|
51
|
+
Exclude:
|
52
|
+
- spec/**/*
|
53
|
+
|
54
|
+
Metrics/CyclomaticComplexity:
|
55
|
+
Max: 13
|
56
|
+
|
57
|
+
Metrics/ClassLength:
|
58
|
+
Max: 300
|
59
|
+
|
60
|
+
Metrics/MethodLength:
|
61
|
+
Max: 26
|
62
|
+
Exclude:
|
63
|
+
- spec/**/*
|
64
|
+
|
65
|
+
Metrics/PerceivedComplexity:
|
66
|
+
Max: 11
|
67
|
+
|
68
|
+
# Naming stuff
|
69
|
+
#
|
70
|
+
|
71
|
+
Naming:
|
72
|
+
Enabled: false
|
73
|
+
|
74
|
+
# Style stuff
|
75
|
+
#
|
76
|
+
Style/AccessorGrouping:
|
77
|
+
Enabled: true
|
78
|
+
|
79
|
+
Style/ArrayCoercion:
|
80
|
+
Enabled: true
|
81
|
+
|
82
|
+
Style/BisectedAttrAccessor:
|
83
|
+
Enabled: true
|
84
|
+
|
85
|
+
Style/CaseLikeIf:
|
86
|
+
Enabled: true
|
87
|
+
|
88
|
+
Style/Documentation:
|
89
|
+
Enabled: false
|
90
|
+
|
91
|
+
Style/ExponentialNotation:
|
92
|
+
Enabled: true
|
93
|
+
|
94
|
+
Style/HashAsLastArrayItem:
|
95
|
+
Enabled: true
|
96
|
+
|
97
|
+
Style/HashEachMethods:
|
98
|
+
Enabled: true
|
99
|
+
|
100
|
+
Style/HashLikeCase:
|
101
|
+
Enabled: true
|
102
|
+
|
103
|
+
Style/HashTransformKeys:
|
104
|
+
Enabled: true
|
105
|
+
|
106
|
+
Style/HashTransformValues:
|
107
|
+
Enabled: true
|
108
|
+
|
109
|
+
Style/RedundantAssignment:
|
110
|
+
Enabled: true
|
111
|
+
|
112
|
+
Style/RedundantFetchBlock:
|
113
|
+
Enabled: true
|
114
|
+
|
115
|
+
Style/RedundantFileExtensionInRequire:
|
116
|
+
Enabled: true
|
117
|
+
|
118
|
+
Style/RedundantRegexpCharacterClass:
|
119
|
+
Enabled: true
|
120
|
+
|
121
|
+
Style/RedundantRegexpEscape:
|
122
|
+
Enabled: true
|
123
|
+
|
124
|
+
Style/RegexpLiteral:
|
125
|
+
Enabled: false
|
126
|
+
|
127
|
+
Style/SlicingWithRange:
|
128
|
+
Enabled: true
|
data/.rubocop_todo.yml
CHANGED
@@ -1,45 +1,34 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on
|
3
|
+
# on 2020-02-18 16:38:42 +0100 using RuboCop version 0.79.0.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
7
7
|
# versions of RuboCop, may require this file to be generated again.
|
8
8
|
|
9
|
-
# Offense count:
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
Metrics/ClassLength:
|
16
|
-
Max: 206
|
17
|
-
|
18
|
-
# Offense count: 3
|
19
|
-
Metrics/CyclomaticComplexity:
|
20
|
-
Max: 11
|
21
|
-
|
22
|
-
# Offense count: 237
|
23
|
-
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives.
|
24
|
-
# URISchemes: http, https
|
25
|
-
Metrics/LineLength:
|
26
|
-
Max: 146
|
9
|
+
# Offense count: 1
|
10
|
+
# Configuration parameters: Include.
|
11
|
+
# Include: **/*.gemspec
|
12
|
+
Gemspec/RequiredRubyVersion:
|
13
|
+
Exclude:
|
14
|
+
- 'grape-entity.gemspec'
|
27
15
|
|
28
16
|
# Offense count: 6
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
# Offense count: 2
|
34
|
-
Metrics/PerceivedComplexity:
|
35
|
-
Max: 13
|
17
|
+
Lint/BooleanSymbol:
|
18
|
+
Exclude:
|
19
|
+
- 'spec/grape_entity/exposure_spec.rb'
|
36
20
|
|
37
|
-
# Offense count:
|
38
|
-
|
39
|
-
|
21
|
+
# Offense count: 1
|
22
|
+
# Configuration parameters: EnforcedStyle.
|
23
|
+
# SupportedStyles: inline, group
|
24
|
+
Style/AccessModifierDeclarations:
|
25
|
+
Exclude:
|
26
|
+
- 'spec/grape_entity/entity_spec.rb'
|
40
27
|
|
41
28
|
# Offense count: 1
|
42
|
-
#
|
43
|
-
|
29
|
+
# Cop supports --auto-correct.
|
30
|
+
# Configuration parameters: IgnoredMethods.
|
31
|
+
# IgnoredMethods: respond_to, define_method
|
32
|
+
Style/SymbolProc:
|
44
33
|
Exclude:
|
45
|
-
- '
|
34
|
+
- 'spec/grape_entity/entity_spec.rb'
|
data/.travis.yml
CHANGED
@@ -1,26 +1,25 @@
|
|
1
|
-
sudo: false
|
2
|
-
|
3
1
|
language: ruby
|
4
2
|
|
5
|
-
|
6
|
-
|
3
|
+
before_install:
|
4
|
+
- gem install bundler
|
5
|
+
|
6
|
+
after_success:
|
7
|
+
- bundle exec danger
|
8
|
+
|
9
|
+
rvm:
|
10
|
+
- 2.5.8
|
11
|
+
- 2.6.6
|
12
|
+
- 2.7.1
|
13
|
+
- ruby-head
|
14
|
+
- jruby-head
|
7
15
|
|
8
16
|
matrix:
|
17
|
+
fast_finish: true
|
18
|
+
|
9
19
|
include:
|
10
|
-
- rvm: 2.
|
11
|
-
script:
|
12
|
-
- bundle exec danger
|
13
|
-
- rvm: 2.3.1
|
14
|
-
- rvm: 2.3.0
|
15
|
-
- rvm: 2.2
|
16
|
-
- rvm: 2.1
|
17
|
-
- rvm: ruby-head
|
18
|
-
- rvm: jruby-9.1.2.0
|
19
|
-
- rvm: jruby-head
|
20
|
-
- rvm: rbx-2
|
20
|
+
- rvm: 2.4.10
|
21
21
|
|
22
22
|
allow_failures:
|
23
|
+
- rvm: 2.4.10
|
23
24
|
- rvm: ruby-head
|
24
|
-
- rvm: jruby-9.1.2.0
|
25
25
|
- rvm: jruby-head
|
26
|
-
- rvm: rbx-2
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,69 @@
|
|
1
|
+
### Next
|
2
|
+
|
3
|
+
#### Features
|
4
|
+
|
5
|
+
* Your contribution here.
|
6
|
+
|
7
|
+
#### Fixes
|
8
|
+
|
9
|
+
* Your contribution here.
|
10
|
+
|
11
|
+
### 0.8.1 (2020-07-15)
|
12
|
+
|
13
|
+
#### Fixes
|
14
|
+
|
15
|
+
* [#336](https://github.com/ruby-grape/grape-entity/pull/336): Pass options to delegators when they accept it - [@dnesteryuk](https://github.com/dnesteryuk).
|
16
|
+
* [#333](https://github.com/ruby-grape/grape-entity/pull/333): Fix typo in CHANGELOG.md - [@eitoball](https://github.com/eitoball).
|
17
|
+
|
18
|
+
### 0.8.0 (2020-02-18)
|
19
|
+
|
20
|
+
#### Features
|
21
|
+
|
22
|
+
* [#307](https://github.com/ruby-grape/grape-entity/pull/307): Allow exposures to call methods defined in modules included in an entity - [@robertoz-01](https://github.com/robertoz-01).
|
23
|
+
* [#319](https://github.com/ruby-grape/grape-entity/pull/319): Support hashes with string keys - [@mhenrixon](https://github.com/mhenrixon).
|
24
|
+
* [#300](https://github.com/ruby-grape/grape-entity/pull/300): Loosens activesupport to 3 - [@ericschultz](https://github.com/ericschultz).
|
25
|
+
|
26
|
+
#### Fixes
|
27
|
+
|
28
|
+
* [#330](https://github.com/ruby-grape/grape-entity/pull/330): CI: use Ruby 2.5.7, 2.6.5, 2.7.0 - [@budnik](https://github.com/budnik).
|
29
|
+
* [#329](https://github.com/ruby-grape/grape-entity/pull/329): Option expose_nil doesn't work when block is passed - [@serbiant](https://github.com/serbiant).
|
30
|
+
* [#320](https://github.com/ruby-grape/grape-entity/pull/320): Gemspec: drop eol'd property rubyforge_project - [@olleolleolle](https://github.com/olleolleolle).
|
31
|
+
* [#307](https://github.com/ruby-grape/grape-entity/pull/307): Allow exposures to call methods defined in modules included in an entity - [@robertoz-01](https://github.com/robertoz-01).
|
32
|
+
|
33
|
+
### 0.7.1 (2018-01-30)
|
34
|
+
|
35
|
+
#### Features
|
36
|
+
|
37
|
+
* [#297](https://github.com/ruby-grape/grape-entity/pull/297): Introduce `override` option for expose (fixes [#286](https://github.com/ruby-grape/grape-entity/issues/296)) - [@DmitryTsepelev](https://github.com/DmitryTsepelev).
|
38
|
+
|
39
|
+
### 0.7.0 (2018-01-25)
|
40
|
+
|
41
|
+
#### Features
|
42
|
+
|
43
|
+
* [#287](https://github.com/ruby-grape/grape-entity/pull/287): Adds ruby 2.5, drops ruby 2.2 support - [@LeFnord](https://github.com/LeFnord).
|
44
|
+
* [#277](https://github.com/ruby-grape/grape-entity/pull/277): Provide grape::entity::options#dig - [@kachick](https://github.com/kachick).
|
45
|
+
* [#265](https://github.com/ruby-grape/grape-entity/pull/265): Adds ability to provide a proc to as: - [@james2m](https://github.com/james2m).
|
46
|
+
* [#264](https://github.com/ruby-grape/grape-entity/pull/264): Adds Rubocop config and todo list - [@james2m](https://github.com/james2m).
|
47
|
+
* [#255](https://github.com/ruby-grape/grape-entity/pull/255): Adds code coverage w/ coveralls - [@LeFnord](https://github.com/LeFnord).
|
48
|
+
* [#268](https://github.com/ruby-grape/grape-entity/pull/268): Loosens the version dependency for activesupport - [@anakinj](https://github.com/anakinj).
|
49
|
+
* [#293](https://github.com/ruby-grape/grape-entity/pull/293): Adds expose_nil option - [@b-boogaard](https://github.com/b-boogaard).
|
50
|
+
|
51
|
+
#### Fixes
|
52
|
+
|
53
|
+
* [#288](https://github.com/ruby-grape/grape-entity/pull/288): Fix wrong argument exception when `&:block` passed to the expose method - [@DmitryTsepelev](https://github.com/DmitryTsepelev).
|
54
|
+
* [#291](https://github.com/ruby-grape/grape-entity/pull/291): Refactor and simplify various classes and modules - [@DmitryTsepelev](https://github.com/DmitryTsepelev).
|
55
|
+
* [#292](https://github.com/ruby-grape/grape-entity/pull/292): Allow replace non-conditional non-nesting exposures in child classes (fixes [#286](https://github.com/ruby-grape/grape-entity/issues/286)) - [@DmitryTsepelev](https://github.com/DmitryTsepelev).
|
56
|
+
|
57
|
+
### 0.6.1 (2017-01-09)
|
58
|
+
|
59
|
+
#### Features
|
60
|
+
|
61
|
+
* [#253](https://github.com/ruby-grape/grape-entity/pull/253): Adds ruby 2.4.0 support, updates dependencies - [@LeFnord](https://github.com/LeFnord).
|
62
|
+
|
63
|
+
#### Fixes
|
64
|
+
|
65
|
+
* [#251](https://github.com/ruby-grape/grape-entity/pull/251): Avoid noise when code runs with Ruby warnings - [@cpetschnig](https://github.com/cpetschnig).
|
66
|
+
|
1
67
|
### 0.6.0 (2016-11-20)
|
2
68
|
|
3
69
|
#### Features
|
data/Dangerfile
CHANGED
data/Gemfile
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
source 'http://rubygems.org'
|
2
4
|
|
3
5
|
gemspec
|
4
6
|
|
5
|
-
if RUBY_VERSION < '2.2.2'
|
6
|
-
gem 'rack', '<2.0.0'
|
7
|
-
gem 'activesupport', '<5.0.0'
|
8
|
-
end
|
9
|
-
|
10
7
|
group :development, :test do
|
11
|
-
gem '
|
8
|
+
gem 'rubocop', '~> 0.88.0', require: false
|
12
9
|
end
|
13
10
|
|
14
11
|
group :test do
|
12
|
+
gem 'coveralls_reborn', require: false
|
13
|
+
gem 'growl'
|
15
14
|
gem 'guard'
|
16
|
-
gem 'guard-rspec'
|
17
15
|
gem 'guard-bundler'
|
16
|
+
gem 'guard-rspec'
|
18
17
|
gem 'rb-fsevent'
|
19
|
-
gem '
|
18
|
+
gem 'ruby-grape-danger', '~> 0.1.1', require: false
|
19
|
+
gem 'simplecov', require: false
|
20
20
|
end
|
data/Guardfile
CHANGED
@@ -1,11 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# A sample Guardfile
|
2
4
|
# More info at https://github.com/guard/guard#readme
|
3
5
|
|
4
6
|
guard 'rspec', version: 2 do
|
5
7
|
watch(%r{^spec/.+_spec\.rb$})
|
6
|
-
watch(%r{^lib/(.+)\.rb$})
|
8
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
7
9
|
watch(%r{^spec/support/shared_versioning_examples.rb$}) { |_m| 'spec/' }
|
8
|
-
watch('spec/spec_helper.rb')
|
10
|
+
watch('spec/spec_helper.rb') { 'spec/' }
|
9
11
|
end
|
10
12
|
|
11
13
|
guard 'bundler' do
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[![Gem Version](http://img.shields.io/gem/v/grape-entity.svg)](http://badge.fury.io/rb/grape-entity)
|
4
4
|
[![Build Status](http://img.shields.io/travis/ruby-grape/grape-entity.svg)](https://travis-ci.org/ruby-grape/grape-entity)
|
5
|
-
[![
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/github/ruby-grape/grape-entity/badge.svg?branch=master)](https://coveralls.io/github/ruby-grape/grape-entity?branch=master)
|
6
6
|
[![Code Climate](https://codeclimate.com/github/ruby-grape/grape-entity.svg)](https://codeclimate.com/github/ruby-grape/grape-entity)
|
7
7
|
|
8
8
|
## Introduction
|
@@ -220,7 +220,8 @@ class ExampleEntity < Grape::Entity
|
|
220
220
|
end
|
221
221
|
```
|
222
222
|
|
223
|
-
You have
|
223
|
+
You always have access to the presented instance (`object`) and the top-level
|
224
|
+
entity options (`options`).
|
224
225
|
|
225
226
|
```ruby
|
226
227
|
class ExampleEntity < Grape::Entity
|
@@ -229,7 +230,7 @@ class ExampleEntity < Grape::Entity
|
|
229
230
|
private
|
230
231
|
|
231
232
|
def formatted_value
|
232
|
-
"+ X #{object.value}"
|
233
|
+
"+ X #{object.value} #{options[:y]}"
|
233
234
|
end
|
234
235
|
end
|
235
236
|
```
|
@@ -255,6 +256,22 @@ class MailingAddress < UserData
|
|
255
256
|
end
|
256
257
|
```
|
257
258
|
|
259
|
+
#### Overriding exposures
|
260
|
+
|
261
|
+
If you want to add one more exposure for the field but don't want the first one to be fired (for instance, when using inheritance), you can use the `override` flag. For instance:
|
262
|
+
|
263
|
+
```ruby
|
264
|
+
class User < Grape::Entity
|
265
|
+
expose :name
|
266
|
+
end
|
267
|
+
|
268
|
+
class Employee < User
|
269
|
+
expose :name, as: :employee_name, override: true
|
270
|
+
end
|
271
|
+
```
|
272
|
+
|
273
|
+
`User` will return something like this `{ "name" : "John" }` while `Employee` will present the same data as `{ "employee_name" : "John" }` instead of `{ "name" : "John", "employee_name" : "John" }`.
|
274
|
+
|
258
275
|
#### Returning only the fields you want
|
259
276
|
|
260
277
|
After exposing the desired attributes, you can choose which one you need when representing some object or collection by using the only: and except: options. See the example:
|
@@ -320,7 +337,7 @@ module Entities
|
|
320
337
|
with_options(format_with: :iso_timestamp) do
|
321
338
|
expose :created_at
|
322
339
|
expose :updated_at
|
323
|
-
end
|
340
|
+
end
|
324
341
|
end
|
325
342
|
end
|
326
343
|
```
|
@@ -349,6 +366,86 @@ module Entities
|
|
349
366
|
end
|
350
367
|
```
|
351
368
|
|
369
|
+
#### Expose Nil
|
370
|
+
|
371
|
+
By default, exposures that contain `nil` values will be represented in the resulting JSON as `null`.
|
372
|
+
|
373
|
+
As an example, a hash with the following values:
|
374
|
+
|
375
|
+
```ruby
|
376
|
+
{
|
377
|
+
name: nil,
|
378
|
+
age: 100
|
379
|
+
}
|
380
|
+
```
|
381
|
+
|
382
|
+
will result in a JSON object that looks like:
|
383
|
+
|
384
|
+
```javascript
|
385
|
+
{
|
386
|
+
"name": null,
|
387
|
+
"age": 100
|
388
|
+
}
|
389
|
+
```
|
390
|
+
|
391
|
+
There are also times when, rather than displaying an attribute with a `null` value, it is more desirable to not display the attribute at all. Using the hash from above the desired JSON would look like:
|
392
|
+
|
393
|
+
```javascript
|
394
|
+
{
|
395
|
+
"age": 100
|
396
|
+
}
|
397
|
+
```
|
398
|
+
|
399
|
+
In order to turn on this behavior for an as-exposure basis, the option `expose_nil` can be used. By default, `expose_nil` is considered to be `true`, meaning that `nil` values will be represented in JSON as `null`. If `false` is provided, then attributes with `nil` values will be omitted from the resulting JSON completely.
|
400
|
+
|
401
|
+
```ruby
|
402
|
+
module Entities
|
403
|
+
class MyModel < Grape::Entity
|
404
|
+
expose :name, expose_nil: false
|
405
|
+
expose :age, expose_nil: false
|
406
|
+
end
|
407
|
+
end
|
408
|
+
```
|
409
|
+
|
410
|
+
`expose_nil` is per exposure, so you can suppress exposures from resulting in `null` or express `null` values on a per exposure basis as you need:
|
411
|
+
|
412
|
+
```ruby
|
413
|
+
module Entities
|
414
|
+
class MyModel < Grape::Entity
|
415
|
+
expose :name, expose_nil: false
|
416
|
+
expose :age # since expose_nil is omitted nil values will be rendered as null
|
417
|
+
end
|
418
|
+
end
|
419
|
+
```
|
420
|
+
|
421
|
+
It is also possible to use `expose_nil` with `with_options` if you want to add the configuration to multiple exposures at once.
|
422
|
+
|
423
|
+
```ruby
|
424
|
+
module Entities
|
425
|
+
class MyModel < Grape::Entity
|
426
|
+
# None of the exposures in the with_options block will render nil values as null
|
427
|
+
with_options(expose_nil: false) do
|
428
|
+
expose :name
|
429
|
+
expose :age
|
430
|
+
end
|
431
|
+
end
|
432
|
+
end
|
433
|
+
```
|
434
|
+
|
435
|
+
When using `with_options`, it is possible to again override which exposures will render `nil` as `null` by adding the option on a specific exposure.
|
436
|
+
|
437
|
+
```ruby
|
438
|
+
module Entities
|
439
|
+
class MyModel < Grape::Entity
|
440
|
+
# None of the exposures in the with_options block will render nil values as null
|
441
|
+
with_options(expose_nil: false) do
|
442
|
+
expose :name
|
443
|
+
expose :age, expose_nil: true # nil values would be rendered as null in the JSON
|
444
|
+
end
|
445
|
+
end
|
446
|
+
end
|
447
|
+
```
|
448
|
+
|
352
449
|
#### Documentation
|
353
450
|
|
354
451
|
Expose documentation with the field. Gets bubbled up when used with Grape and various API documentation systems.
|