grape-entity 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +8 -3
- data/CHANGELOG.md +100 -48
- data/Dangerfile +1 -0
- data/Gemfile +21 -2
- data/LICENSE +1 -1
- data/README.md +39 -8
- data/grape-entity.gemspec +0 -2
- data/lib/grape_entity/entity.rb +1 -1
- data/lib/grape_entity/exposure/base.rb +1 -1
- data/lib/grape_entity/exposure/nesting_exposure/output_builder.rb +5 -0
- data/lib/grape_entity/options.rb +4 -0
- data/lib/grape_entity/version.rb +1 -1
- data/spec/grape_entity/entity_spec.rb +63 -8
- data/spec/grape_entity/hash_spec.rb +40 -0
- data/spec/spec_helper.rb +0 -2
- metadata +6 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 37f12961d0bb27814ea3e9276b02b41ea1ed798b
|
4
|
+
data.tar.gz: 00a68c4665543223ecbd8e3d11f901981ef3960a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b034eb40a7a12229251c4d87e31cf365c7282d4b7016859baa8ca3409cb308191594d59ddb838b6ab2f1607cdb1d3e6478267eb735c41d3c63d3a612a4cf6418
|
7
|
+
data.tar.gz: 8c2d400de75c142e64d1bce531d0433c19f1312700893a49a5ff893888221ec73d0d00b193374b3f03d374cfa4af6424ab5af0fb7985cbd2f4dcf677fdba72f8
|
data/.travis.yml
CHANGED
@@ -4,18 +4,23 @@ language: ruby
|
|
4
4
|
|
5
5
|
cache: bundler
|
6
6
|
|
7
|
+
bundler_args: --without development
|
8
|
+
|
7
9
|
rvm:
|
8
|
-
-
|
10
|
+
- 2.3.1
|
9
11
|
- 2.3.0
|
10
12
|
- 2.2
|
11
13
|
- 2.1
|
12
14
|
- 2.0.0
|
13
|
-
-
|
14
|
-
- jruby-19mode
|
15
|
+
- ruby-head
|
15
16
|
- jruby-head
|
16
17
|
- rbx-2
|
17
18
|
|
18
19
|
matrix:
|
20
|
+
include:
|
21
|
+
- rvm: 2.3.1
|
22
|
+
script:
|
23
|
+
- bundle exec danger
|
19
24
|
allow_failures:
|
20
25
|
- rvm: ruby-head
|
21
26
|
- rvm: jruby-head
|
data/CHANGELOG.md
CHANGED
@@ -1,38 +1,74 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
### Next
|
2
|
+
|
3
|
+
#### Features
|
4
|
+
|
5
|
+
* Your contribution here.
|
6
|
+
|
7
|
+
#### Fixes
|
8
|
+
|
9
|
+
* Your contribution here.
|
10
|
+
|
11
|
+
### 0.5.2 (2016-11-14)
|
12
|
+
|
13
|
+
#### Features
|
14
|
+
|
15
|
+
* [#226](https://github.com/ruby-grape/grape-entity/pull/226): Added `fetch` from `opts_hash` - [@alanjcfs](https://github.com/alanjcfs).
|
16
|
+
* [#232](https://github.com/ruby-grape/grape-entity/pull/232), [#213](https://github.com/ruby-grape/grape-entity/issues/213): Added `#kind_of?` and `#is_a?` to `OutputBuilder` to get an exact class of an `output` object - [@avyy](https://github.com/avyy).
|
17
|
+
* [#234](https://github.com/ruby-grape/grape-entity/pull/234), [#233](https://github.com/ruby-grape/grape-entity/issues/233): Added ruby version checking in `Gemfile` to install needed gems versions for supporting old rubies too - [@avyy](https://github.com/avyy).
|
18
|
+
* [#237](https://github.com/ruby-grape/grape-entity/pull/237): Added Danger, PR linter - [@dblock](https://github.com/dblock).
|
19
|
+
|
20
|
+
#### Fixes
|
21
|
+
|
22
|
+
* [#215](https://github.com/ruby-grape/grape-entity/pull/217): `#delegate_attribute` no longer delegates to methods included with `Kernel` - [@maltoe](https://github.com/maltoe).
|
23
|
+
* [#219](https://github.com/ruby-grape/grape-entity/pull/219): Double pass options in `serializable_hash` - [@sbatykov](https://github.com/sbatykov).
|
24
|
+
* [#231](https://github.com/ruby-grape/grape-entity/pull/231), [#215](https://github.com/ruby-grape/grape-entity/issues/215): Allow `delegate_attribute` for derived entity - [@sbatykov](https://github.com/sbatykov).
|
25
|
+
|
26
|
+
### 0.5.1 (2016-4-4)
|
27
|
+
|
28
|
+
#### Features
|
3
29
|
|
4
|
-
* [#202](https://github.com/ruby-grape/grape-entity/pull/202): Fix: Reset `@using_class` memoization on `.setup` - [@rngtng](https://github.com/rngtng).
|
5
30
|
* [#203](https://github.com/ruby-grape/grape-entity/pull/203): `Grape::Entity::Exposure::NestingExposure::NestedExposures.delete_if` always returns exposures - [@rngtng](https://github.com/rngtng).
|
6
31
|
* [#204](https://github.com/ruby-grape/grape-entity/pull/204), [#138](https://github.com/ruby-grape/grape-entity/issues/138): Added ability to merge fields into hashes/root (`:merge` option for `.expose`) - [@avyy](https://github.com/avyy).
|
7
32
|
|
8
|
-
|
9
|
-
|
33
|
+
#### Fixes
|
34
|
+
|
35
|
+
* [#202](https://github.com/ruby-grape/grape-entity/pull/202): Reset `@using_class` memoization on `.setup` - [@rngtng](https://github.com/rngtng).
|
36
|
+
|
37
|
+
### 0.5.0 (2015-12-07)
|
38
|
+
|
39
|
+
#### Features
|
10
40
|
|
11
41
|
* [#139](https://github.com/ruby-grape/grape-entity/pull/139): Keep a track of attribute nesting path during condition check or runtime exposure - [@calfzhou](https://github.com/calfzhou).
|
12
42
|
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): `.exposures` is removed and substituted with `.root_exposures` array - [@marshall-lee](https://github.com/marshall-lee).
|
13
43
|
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): `.nested_exposures` is removed too - [@marshall-lee](https://github.com/marshall-lee).
|
14
44
|
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): `#should_return_attribute?`, `#only_fields` and `#except_fields` are moved to other classes - [@marshall-lee](https://github.com/marshall-lee).
|
15
|
-
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): Fix: double exposures with conditions does not rewrite previously defined now: [#56](https://github.com/ruby-grape/grape-entity/issues/56) - [@marshall-lee](https://github.com/marshall-lee).
|
16
|
-
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): Fix: nested exposures were flattened in `.documentation`: [#112](https://github.com/ruby-grape/grape-entity/issues/112) - [@marshall-lee](https://github.com/marshall-lee).
|
17
|
-
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): Fix: `@only_fields` and `@except_fields` memoization: [#149](https://github.com/ruby-grape/grape-entity/issues/149) - [@marshall-lee](https://github.com/marshall-lee).
|
18
|
-
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): Fix: `:unless` condition with `Hash` argument logic: [#150](https://github.com/ruby-grape/grape-entity/issues/150) - [@marshall-lee](https://github.com/marshall-lee).
|
19
45
|
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): Nested `unexpose` now raises an exception: [#152](https://github.com/ruby-grape/grape-entity/issues/152) - [@marshall-lee](https://github.com/marshall-lee).
|
20
|
-
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): Fix: `@documentation` memoization: [#153](https://github.com/ruby-grape/grape-entity/issues/153) - [@marshall-lee](https://github.com/marshall-lee).
|
21
|
-
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): Fix: serializing of deeply nested presenter exposures: [#155](https://github.com/ruby-grape/grape-entity/issues/155) - [@marshall-lee](https://github.com/marshall-lee).
|
22
|
-
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): Fix: deep projections (`:only`, `:except`) were unaware of nesting: [#156](https://github.com/ruby-grape/grape-entity/issues/156) - [@marshall-lee](https://github.com/marshall-lee).
|
23
46
|
|
24
|
-
|
25
|
-
|
47
|
+
#### Fixes
|
48
|
+
|
49
|
+
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): Double exposures with conditions does not rewrite previously defined now: [#56](https://github.com/ruby-grape/grape-entity/issues/56) - [@marshall-lee](https://github.com/marshall-lee).
|
50
|
+
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): Nested exposures were flattened in `.documentation`: [#112](https://github.com/ruby-grape/grape-entity/issues/112) - [@marshall-lee](https://github.com/marshall-lee).
|
51
|
+
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): `@only_fields` and `@except_fields` memoization: [#149](https://github.com/ruby-grape/grape-entity/issues/149) - [@marshall-lee](https://github.com/marshall-lee).
|
52
|
+
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): `:unless` condition with `Hash` argument logic: [#150](https://github.com/ruby-grape/grape-entity/issues/150) - [@marshall-lee](https://github.com/marshall-lee).
|
53
|
+
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): `@documentation` memoization: [#153](https://github.com/ruby-grape/grape-entity/issues/153) - [@marshall-lee](https://github.com/marshall-lee).
|
54
|
+
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): Serializing of deeply nested presenter exposures: [#155](https://github.com/ruby-grape/grape-entity/issues/155) - [@marshall-lee](https://github.com/marshall-lee).
|
55
|
+
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): Deep projections (`:only`, `:except`) were unaware of nesting: [#156](https://github.com/ruby-grape/grape-entity/issues/156) - [@marshall-lee](https://github.com/marshall-lee).
|
56
|
+
|
57
|
+
### 0.4.8 (2015-08-10)
|
58
|
+
|
59
|
+
#### Features
|
26
60
|
|
27
|
-
* [#167](https://github.com/ruby-grape/grape-entity/pull/167)
|
61
|
+
* [#167](https://github.com/ruby-grape/grape-entity/pull/167), [#166](https://github.com/ruby-grape/grape-entity/issues/166): Regression with global settings (exposures, formatters) on `Grape::Entity` - [@marshall-lee](https://github.com/marshall-lee).
|
28
62
|
|
29
|
-
0.4.7 (2015-08-03)
|
30
|
-
==================
|
63
|
+
### 0.4.7 (2015-08-03)
|
31
64
|
|
32
|
-
|
65
|
+
#### Features
|
33
66
|
|
34
|
-
|
35
|
-
|
67
|
+
* [#164](https://github.com/ruby-grape/grape-entity/pull/164): Regression: entity instance methods were exposed with `NoMethodError`: [#163](https://github.com/ruby-grape/grape-entity/issues/163) - [@marshall-lee](https://github.com/marshall-lee).
|
68
|
+
|
69
|
+
### 0.4.6 (2015-07-27)
|
70
|
+
|
71
|
+
#### Features
|
36
72
|
|
37
73
|
* [#114](https://github.com/ruby-grape/grape-entity/pull/114): Added 'only' option that selects which attributes should be returned - [@estevaoam](https://github.com/estevaoam).
|
38
74
|
* [#115](https://github.com/ruby-grape/grape-entity/pull/115): Allowing 'root' to be inherited from parent to child entities - [@guidoprincess](https://github.com/guidoprincess).
|
@@ -40,46 +76,61 @@
|
|
40
76
|
* [#134](https://github.com/ruby-grape/grape-entity/pull/134): Subclasses no longer affected in all cases by `unexpose` in parent - [@etehtsea](https://github.com/etehtsea).
|
41
77
|
* [#135](https://github.com/ruby-grape/grape-entity/pull/135): Added `except` option - [@dan-corneanu](https://github.com/dan-corneanu).
|
42
78
|
* [#136](https://github.com/ruby-grape/grape-entity/pull/136): Allow for strings in `only` and `except` options - [@bswinnerton](https://github.com/bswinnerton).
|
43
|
-
* [#147](https://github.com/ruby-grape/grape-entity/pull/147): Expose `safe` attributes as `nil` if they cannot be evaluated
|
44
|
-
* [#147](https://github.com/ruby-grape/grape-entity/pull/147):
|
45
|
-
* [#147](https://github.com/ruby-grape/grape-entity/pull/147):
|
46
|
-
|
79
|
+
* [#147](https://github.com/ruby-grape/grape-entity/pull/147), [#140](https://github.com/ruby-grape/grape-entity/issues/140): Expose `safe` attributes as `nil` if they cannot be evaluated - [@marshall-lee](https://github.com/marshall-lee).
|
80
|
+
* [#147](https://github.com/ruby-grape/grape-entity/pull/147): Remove catching of `NoMethodError` because it can occur deep inside in a method call so this exception does not mean that attribute not exist - [@marshall-lee](https://github.com/marshall-lee).
|
81
|
+
* [#147](https://github.com/ruby-grape/grape-entity/pull/147): The `valid_exposures` method was removed - [@marshall-lee](https://github.com/marshall-lee).
|
82
|
+
|
83
|
+
#### Fixes
|
84
|
+
|
85
|
+
* [#147](https://github.com/ruby-grape/grape-entity/pull/147), [#142](https://github.com/ruby-grape/grape-entity/pull/142): Private method values were not exposed with `safe` option - [@marshall-lee](https://github.com/marshall-lee).
|
47
86
|
|
48
|
-
0.4.5 (2015-03-10)
|
49
|
-
|
87
|
+
### 0.4.5 (2015-03-10)
|
88
|
+
|
89
|
+
#### Features
|
50
90
|
|
51
91
|
* [#109](https://github.com/ruby-grape/grape-entity/pull/109): Added `unexpose` method - [@jonmchan](https://github.com/jonmchan).
|
52
92
|
* [#98](https://github.com/ruby-grape/grape-entity/pull/98): Added nested conditionals - [@zbelzer](https://github.com/zbelzer).
|
53
93
|
* [#105](https://github.com/ruby-grape/grape-entity/pull/105): Specify which attribute is missing in which Entity - [@jhollinger](https://github.com/jhollinger).
|
54
|
-
* [#111](https://github.com/ruby-grape/grape-entity/pull/111): Fix: allow usage of attributes with name 'key' if `Hash` objects are used - [@croeck](https://github.com/croeck).
|
55
|
-
* [#110](https://github.com/ruby-grape/grape-entity/pull/110): Fix: safe exposure when using `Hash` models - [@croeck](https://github.com/croeck).
|
56
|
-
* [#91](https://github.com/ruby-grape/grape-entity/pull/91): Fix: OpenStruct serializing - [@etehtsea](https://github.com/etehtsea).
|
57
94
|
|
58
|
-
|
59
|
-
|
95
|
+
#### Fixes
|
96
|
+
|
97
|
+
* [#111](https://github.com/ruby-grape/grape-entity/pull/111): Allow usage of attributes with name 'key' if `Hash` objects are used - [@croeck](https://github.com/croeck).
|
98
|
+
* [#110](https://github.com/ruby-grape/grape-entity/pull/110): Safe exposure when using `Hash` models - [@croeck](https://github.com/croeck).
|
99
|
+
* [#91](https://github.com/ruby-grape/grape-entity/pull/91): OpenStruct serializing - [@etehtsea](https://github.com/etehtsea).
|
100
|
+
|
101
|
+
### 0.4.4 (2014-08-17)
|
102
|
+
|
103
|
+
#### Features
|
60
104
|
|
61
105
|
* [#85](https://github.com/ruby-grape/grape-entity/pull/85): Added `present_collection` to indicate that an `Entity` presents an entire Collection - [@dspaeth-faber](https://github.com/dspaeth-faber).
|
62
|
-
* [#85](https://
|
106
|
+
* [#85](https://github.com/ruby-grape/grape-entity/pull/85): Hashes can now be passed as object to be presented and the `Hash` keys can be referenced by expose - [@dspaeth-faber](https://github.com/dspaeth-faber).
|
63
107
|
|
64
|
-
0.4.3 (2014-06-12)
|
65
|
-
==================
|
108
|
+
### 0.4.3 (2014-06-12)
|
66
109
|
|
67
|
-
|
68
|
-
* [#76](https://github.com/ruby-grape/grape-entity/pull/76): Improve performance of entity serialization - [@justfalter](https://github.com/justfalter)
|
110
|
+
#### Features
|
69
111
|
|
70
|
-
|
71
|
-
|
112
|
+
* [#76](https://github.com/ruby-grape/grape-entity/pull/76): Improve performance of entity serialization - [@justfalter](https://github.com/justfalter).
|
113
|
+
|
114
|
+
#### Fixes
|
115
|
+
|
116
|
+
* [#77](https://github.com/ruby-grape/grape-entity/pull/77): Compatibility with Rspec 3 - [@justfalter](https://github.com/justfalter).
|
117
|
+
|
118
|
+
### 0.4.2 (2014-04-03)
|
119
|
+
|
120
|
+
#### Features
|
72
121
|
|
73
122
|
* [#60](https://github.com/ruby-grape/grape-entity/issues/59): Performance issues introduced by nested exposures - [@AlexYankee](https://github.com/AlexYankee).
|
74
123
|
* [#60](https://github.com/ruby-grape/grape-entity/issues/57): Nested exposure double-exposes a field - [@AlexYankee](https://github.com/AlexYankee).
|
75
124
|
|
76
|
-
0.4.1 (2014-02-13)
|
77
|
-
|
125
|
+
### 0.4.1 (2014-02-13)
|
126
|
+
|
127
|
+
#### Fixes
|
78
128
|
|
79
129
|
* [#54](https://github.com/ruby-grape/grape-entity/issues/54): Fix: undefined method `to_set` - [@aj0strow](https://github.com/aj0strow).
|
80
130
|
|
81
|
-
0.4.0 (2014-01-27)
|
82
|
-
|
131
|
+
### 0.4.0 (2014-01-27)
|
132
|
+
|
133
|
+
#### Features
|
83
134
|
|
84
135
|
* Ruby 1.8.x is no longer supported - [@dblock](https://github.com/dblock).
|
85
136
|
* [#36](https://github.com/ruby-grape/grape-entity/pull/36): Enforcing Ruby style guidelines via Rubocop - [@dblock](https://github.com/dblock).
|
@@ -96,19 +147,20 @@
|
|
96
147
|
* [#51](https://github.com/ruby-grape/grape-entity/pull/51): Raise `ArgumentError` if an unknown option is used with `expose` - [@aj0strow](https://github.com/aj0strow).
|
97
148
|
* [#51](https://github.com/ruby-grape/grape-entity/pull/51): Alias `:with` to `:using`, consistently with the Grape api endpoints - [@aj0strow](https://github.com/aj0strow).
|
98
149
|
|
99
|
-
0.3.0 (2013-03-29)
|
100
|
-
|
150
|
+
### 0.3.0 (2013-03-29)
|
151
|
+
|
152
|
+
#### Features
|
101
153
|
|
102
154
|
* [#9](https://github.com/ruby-grape/grape-entity/pull/9): Added `with_options` for block-level exposure setting - [@SegFaultAX](https://github.com/SegFaultAX).
|
103
155
|
* The `instance.entity` method now optionally accepts `options` - [@mbleigh](https://github.com/mbleigh).
|
104
156
|
* You can pass symbols to `:if` and `:unless` to simply check for truthiness/falsiness of the specified options key - [@mbleigh](https://github.com/mbleigh).
|
105
157
|
|
106
|
-
0.2.0 (2013-01-11)
|
107
|
-
|
158
|
+
### 0.2.0 (2013-01-11)
|
159
|
+
|
160
|
+
#### Features
|
108
161
|
|
109
162
|
* Moved the namespace back to `Grape::Entity` to preserve compatibility with Grape - [@dblock](https://github.com/dblock).
|
110
163
|
|
111
|
-
0.1.0 (2013-01-11)
|
112
|
-
==================
|
164
|
+
### 0.1.0 (2013-01-11)
|
113
165
|
|
114
166
|
* Initial public release - [@agileanimal](https://github.com/agileanimal).
|
data/Dangerfile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
danger.import_dangerfile(gem: 'ruby-grape-danger')
|
data/Gemfile
CHANGED
@@ -2,15 +2,34 @@ source 'http://rubygems.org'
|
|
2
2
|
|
3
3
|
gemspec
|
4
4
|
|
5
|
-
|
5
|
+
current_ruby_version = Gem::Version.new(RUBY_VERSION)
|
6
|
+
|
7
|
+
if Gem::Requirement.new('>= 2.2.2').satisfied_by? current_ruby_version
|
8
|
+
gem 'activesupport', '~> 5.0'
|
9
|
+
gem 'rack', '~> 2.0', group: [:development, :test]
|
10
|
+
else
|
11
|
+
gem 'activesupport', '~> 4.0'
|
12
|
+
gem 'rack', '< 2', group: [:development, :test]
|
13
|
+
end
|
14
|
+
|
15
|
+
gem 'json', '< 2', group: [:development, :test]
|
16
|
+
|
17
|
+
group :development do
|
6
18
|
gem 'pry'
|
7
19
|
gem 'guard'
|
8
20
|
gem 'guard-rspec'
|
9
21
|
gem 'guard-bundler'
|
10
22
|
gem 'rb-fsevent'
|
11
23
|
gem 'growl'
|
12
|
-
|
24
|
+
end
|
25
|
+
|
26
|
+
group :development, :test do
|
27
|
+
gem 'rake'
|
13
28
|
gem 'rspec'
|
14
29
|
gem 'rack-test', '~> 0.6.2', require: 'rack/test'
|
15
30
|
gem 'rubocop', '0.31.0'
|
16
31
|
end
|
32
|
+
|
33
|
+
group :test do
|
34
|
+
gem 'ruby-grape-danger', '~> 0.1.0', require: false
|
35
|
+
end
|
data/LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2010 Michael Bleigh
|
1
|
+
Copyright (c) 2010-2016 Michael Bleigh, Intridea, Inc., ruby-grape and Contributors.
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining
|
4
4
|
a copy of this software and associated documentation files (the
|
data/README.md
CHANGED
@@ -212,7 +212,7 @@ on the object the entity wraps.
|
|
212
212
|
class ExampleEntity < Grape::Entity
|
213
213
|
expose :attr_not_on_wrapped_object
|
214
214
|
# ...
|
215
|
-
private
|
215
|
+
private
|
216
216
|
|
217
217
|
def attr_not_on_wrapped_object
|
218
218
|
42
|
@@ -226,7 +226,7 @@ You have always access to the presented instance with `object`
|
|
226
226
|
class ExampleEntity < Grape::Entity
|
227
227
|
expose :formatted_value
|
228
228
|
# ...
|
229
|
-
private
|
229
|
+
private
|
230
230
|
|
231
231
|
def formatted_value
|
232
232
|
"+ X #{object.value}"
|
@@ -311,10 +311,41 @@ expose :replies, using: API::Entities::Status, as: :responses
|
|
311
311
|
Apply a formatter before exposing a value.
|
312
312
|
|
313
313
|
```ruby
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
314
|
+
module Entities
|
315
|
+
class MyModel < Grape::Entity
|
316
|
+
format_with(:iso_timestamp) do |date|
|
317
|
+
date.iso8601
|
318
|
+
end
|
319
|
+
|
320
|
+
with_options(format_with: :iso_timestamp) do
|
321
|
+
expose :created_at
|
322
|
+
expose :updated_at
|
323
|
+
end
|
324
|
+
end
|
325
|
+
end
|
326
|
+
```
|
327
|
+
|
328
|
+
Defining a reusable formatter between multiples entities:
|
329
|
+
|
330
|
+
```ruby
|
331
|
+
module ApiHelpers
|
332
|
+
extend Grape::API::Helpers
|
333
|
+
|
334
|
+
Grape::Entity.format_with :utc do |date|
|
335
|
+
date.utc if date
|
336
|
+
end
|
337
|
+
end
|
338
|
+
```
|
339
|
+
|
340
|
+
```ruby
|
341
|
+
module Entities
|
342
|
+
class MyModel < Grape::Entity
|
343
|
+
expose :updated_at, format_with: :utc
|
344
|
+
end
|
345
|
+
|
346
|
+
class AnotherModel < Grape::Entity
|
347
|
+
expose :created_at, format_with: :utc
|
348
|
+
end
|
318
349
|
end
|
319
350
|
```
|
320
351
|
|
@@ -413,7 +444,7 @@ The above will automatically create a `Status::Entity` class and define properti
|
|
413
444
|
|
414
445
|
### Using Entities
|
415
446
|
|
416
|
-
With Grape, once an entity is defined, it can be used within endpoints, by calling `present`. The `present` method accepts two arguments, the `object` to be presented and the `options` associated with it. The options hash must always include `:with`, which defines the entity to expose.
|
447
|
+
With Grape, once an entity is defined, it can be used within endpoints, by calling `present`. The `present` method accepts two arguments, the `object` to be presented and the `options` associated with it. The options hash must always include `:with`, which defines the entity to expose (unless namespaced entity classes are used, see [next section](#entity-organization)).
|
417
448
|
If the entity includes documentation it can be included in an endpoint's description.
|
418
449
|
|
419
450
|
```ruby
|
@@ -516,4 +547,4 @@ MIT License. See [LICENSE](LICENSE) for details.
|
|
516
547
|
|
517
548
|
## Copyright
|
518
549
|
|
519
|
-
Copyright (c) 2010-
|
550
|
+
Copyright (c) 2010-2016 Michael Bleigh, Intridea, Inc., ruby-grape and Contributors.
|
data/grape-entity.gemspec
CHANGED
@@ -14,10 +14,8 @@ Gem::Specification.new do |s|
|
|
14
14
|
|
15
15
|
s.rubyforge_project = 'grape-entity'
|
16
16
|
|
17
|
-
s.add_runtime_dependency 'activesupport'
|
18
17
|
s.add_runtime_dependency 'multi_json', '>= 1.3.2'
|
19
18
|
|
20
|
-
s.add_development_dependency 'rake'
|
21
19
|
s.add_development_dependency 'maruku'
|
22
20
|
s.add_development_dependency 'yard'
|
23
21
|
s.add_development_dependency 'rspec', '~> 2.9'
|
data/lib/grape_entity/entity.rb
CHANGED
@@ -63,7 +63,7 @@ module Grape
|
|
63
63
|
partial_output = valid_value(entity, options)
|
64
64
|
|
65
65
|
if partial_output.respond_to?(:serializable_hash)
|
66
|
-
partial_output.serializable_hash
|
66
|
+
partial_output.serializable_hash
|
67
67
|
elsif partial_output.is_a?(Array) && partial_output.all? { |o| o.respond_to?(:serializable_hash) }
|
68
68
|
partial_output.map(&:serializable_hash)
|
69
69
|
elsif partial_output.is_a?(Hash)
|
data/lib/grape_entity/options.rb
CHANGED
data/lib/grape_entity/version.rb
CHANGED
@@ -905,6 +905,7 @@ describe Grape::Entity do
|
|
905
905
|
|
906
906
|
representation = subject.represent(4.times.map { Object.new }, serializable: true)
|
907
907
|
expect(representation).to be_kind_of(Grape::Entity::Exposure::NestingExposure::OutputBuilder)
|
908
|
+
expect(representation).to be_kind_of(Hash)
|
908
909
|
expect(representation).to have_key :my_items
|
909
910
|
expect(representation[:my_items]).to be_kind_of Array
|
910
911
|
expect(representation[:my_items].size).to be 4
|
@@ -1523,12 +1524,13 @@ describe Grape::Entity do
|
|
1523
1524
|
expect(subject.value_for(:fantasies)).to eq ['Nessy', 'Double Rainbows', 'Unicorns']
|
1524
1525
|
end
|
1525
1526
|
|
1526
|
-
|
1527
|
+
context 'delegate_attribute' do
|
1527
1528
|
module EntitySpec
|
1528
1529
|
class DelegatingEntity < Grape::Entity
|
1529
1530
|
root 'friends', 'friend'
|
1530
1531
|
expose :name
|
1531
1532
|
expose :email
|
1533
|
+
expose :system
|
1532
1534
|
|
1533
1535
|
private
|
1534
1536
|
|
@@ -1538,14 +1540,33 @@ describe Grape::Entity do
|
|
1538
1540
|
end
|
1539
1541
|
end
|
1540
1542
|
|
1541
|
-
|
1542
|
-
|
1543
|
-
|
1544
|
-
|
1543
|
+
it 'tries instance methods on the entity first' do
|
1544
|
+
friend = double('Friend', name: 'joe', email: 'joe@example.com')
|
1545
|
+
rep = EntitySpec::DelegatingEntity.new(friend)
|
1546
|
+
expect(rep.value_for(:name)).to eq 'cooler name'
|
1547
|
+
expect(rep.value_for(:email)).to eq 'joe@example.com'
|
1545
1548
|
|
1546
|
-
|
1547
|
-
|
1548
|
-
|
1549
|
+
another_friend = double('Friend', email: 'joe@example.com')
|
1550
|
+
rep = EntitySpec::DelegatingEntity.new(another_friend)
|
1551
|
+
expect(rep.value_for(:name)).to eq 'cooler name'
|
1552
|
+
end
|
1553
|
+
|
1554
|
+
it 'does not delegate Kernel methods' do
|
1555
|
+
foo = double 'Foo', system: 'System'
|
1556
|
+
rep = EntitySpec::DelegatingEntity.new foo
|
1557
|
+
expect(rep.value_for(:system)).to eq 'System'
|
1558
|
+
end
|
1559
|
+
|
1560
|
+
module EntitySpec
|
1561
|
+
class DerivedEntity < DelegatingEntity
|
1562
|
+
end
|
1563
|
+
end
|
1564
|
+
|
1565
|
+
it 'derived entity get methods from base entity' do
|
1566
|
+
foo = double 'Foo', name: 'joe'
|
1567
|
+
rep = EntitySpec::DerivedEntity.new foo
|
1568
|
+
expect(rep.value_for(:name)).to eq 'cooler name'
|
1569
|
+
end
|
1549
1570
|
end
|
1550
1571
|
|
1551
1572
|
context 'using' do
|
@@ -1717,5 +1738,39 @@ describe Grape::Entity do
|
|
1717
1738
|
end
|
1718
1739
|
end
|
1719
1740
|
end
|
1741
|
+
|
1742
|
+
describe Grape::Entity::Options do
|
1743
|
+
module EntitySpec
|
1744
|
+
class Crystalline
|
1745
|
+
attr_accessor :prop1, :prop2
|
1746
|
+
|
1747
|
+
def initialize
|
1748
|
+
@prop1 = 'value1'
|
1749
|
+
@prop2 = 'value2'
|
1750
|
+
end
|
1751
|
+
end
|
1752
|
+
|
1753
|
+
class CrystallineEntity < Grape::Entity
|
1754
|
+
expose :prop1, if: ->(_, options) { options.fetch(:signal) }
|
1755
|
+
expose :prop2, if: ->(_, options) { options.fetch(:beam, 'destructive') == 'destructive' }
|
1756
|
+
end
|
1757
|
+
end
|
1758
|
+
|
1759
|
+
context '#fetch' do
|
1760
|
+
it 'without passing in a required option raises KeyError' do
|
1761
|
+
expect { EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new).as_json }.to raise_error KeyError
|
1762
|
+
end
|
1763
|
+
|
1764
|
+
it 'passing in a required option will expose the values' do
|
1765
|
+
crystalline_entity = EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new, signal: true)
|
1766
|
+
expect(crystalline_entity.as_json).to eq(prop1: 'value1', prop2: 'value2')
|
1767
|
+
end
|
1768
|
+
|
1769
|
+
it 'with an option that is not default will not expose that value' do
|
1770
|
+
crystalline_entity = EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new, signal: true, beam: 'intermittent')
|
1771
|
+
expect(crystalline_entity.as_json).to eq(prop1: 'value1')
|
1772
|
+
end
|
1773
|
+
end
|
1774
|
+
end
|
1720
1775
|
end
|
1721
1776
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Grape::Entity do
|
4
|
+
it 'except option for nested entity' do
|
5
|
+
module EntitySpec
|
6
|
+
class Address < Grape::Entity
|
7
|
+
expose :post, if: :full
|
8
|
+
expose :city
|
9
|
+
expose :street
|
10
|
+
expose :house
|
11
|
+
end
|
12
|
+
|
13
|
+
class Company < Grape::Entity
|
14
|
+
expose :full_name, if: :full
|
15
|
+
expose :name
|
16
|
+
expose :address do |c, o|
|
17
|
+
Address.represent c[:address], Grape::Entity::Options.new(o.opts_hash.except(:full))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
company = {
|
23
|
+
full_name: 'full_name',
|
24
|
+
name: 'name',
|
25
|
+
address: {
|
26
|
+
post: '123456',
|
27
|
+
city: 'city',
|
28
|
+
street: 'street',
|
29
|
+
house: 'house',
|
30
|
+
something_else: 'something_else'
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
expect(EntitySpec::Company.represent(company).serializable_hash).to eq \
|
35
|
+
company.slice(:name).merge(address: company[:address].slice(:city, :street, :house))
|
36
|
+
|
37
|
+
expect(EntitySpec::Company.represent(company, full: true).serializable_hash).to eq \
|
38
|
+
company.slice(:full_name, :name).merge(address: company[:address].slice(:city, :street, :house))
|
39
|
+
end
|
40
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-entity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Bleigh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: activesupport
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: multi_json
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,20 +24,6 @@ dependencies:
|
|
38
24
|
- - ">="
|
39
25
|
- !ruby/object:Gem::Version
|
40
26
|
version: 1.3.2
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rake
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
27
|
- !ruby/object:Gem::Dependency
|
56
28
|
name: maruku
|
57
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -124,6 +96,7 @@ files:
|
|
124
96
|
- ".yardopts"
|
125
97
|
- CHANGELOG.md
|
126
98
|
- CONTRIBUTING.md
|
99
|
+
- Dangerfile
|
127
100
|
- Gemfile
|
128
101
|
- Guardfile
|
129
102
|
- LICENSE
|
@@ -163,6 +136,7 @@ files:
|
|
163
136
|
- spec/grape_entity/exposure/nesting_exposure/nested_exposures_spec.rb
|
164
137
|
- spec/grape_entity/exposure/represent_exposure_spec.rb
|
165
138
|
- spec/grape_entity/exposure_spec.rb
|
139
|
+
- spec/grape_entity/hash_spec.rb
|
166
140
|
- spec/spec_helper.rb
|
167
141
|
homepage: https://github.com/ruby-grape/grape-entity
|
168
142
|
licenses:
|
@@ -184,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
184
158
|
version: '0'
|
185
159
|
requirements: []
|
186
160
|
rubyforge_project: grape-entity
|
187
|
-
rubygems_version: 2.
|
161
|
+
rubygems_version: 2.6.7
|
188
162
|
signing_key:
|
189
163
|
specification_version: 4
|
190
164
|
summary: A simple facade for managing the relationship between your model and API.
|
@@ -193,5 +167,5 @@ test_files:
|
|
193
167
|
- spec/grape_entity/exposure/nesting_exposure/nested_exposures_spec.rb
|
194
168
|
- spec/grape_entity/exposure/represent_exposure_spec.rb
|
195
169
|
- spec/grape_entity/exposure_spec.rb
|
170
|
+
- spec/grape_entity/hash_spec.rb
|
196
171
|
- spec/spec_helper.rb
|
197
|
-
has_rdoc:
|