grape-entity 0.4.8 → 0.5.0

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.
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Grape::Entity::Exposure::NestingExposure::NestedExposures do
4
+ subject { described_class.new([]) }
5
+
6
+ describe '#deep_complex_nesting?' do
7
+ it 'is reset when additional exposure is added' do
8
+ subject << Grape::Entity::Exposure.new(:x, {})
9
+ expect(subject.instance_variable_get(:@deep_complex_nesting)).to be_nil
10
+ subject.deep_complex_nesting?
11
+ expect(subject.instance_variable_get(:@deep_complex_nesting)).to_not be_nil
12
+ subject << Grape::Entity::Exposure.new(:y, {})
13
+ expect(subject.instance_variable_get(:@deep_complex_nesting)).to be_nil
14
+ end
15
+
16
+ it 'is reset when exposure is deleted' do
17
+ subject << Grape::Entity::Exposure.new(:x, {})
18
+ expect(subject.instance_variable_get(:@deep_complex_nesting)).to be_nil
19
+ subject.deep_complex_nesting?
20
+ expect(subject.instance_variable_get(:@deep_complex_nesting)).to_not be_nil
21
+ subject.delete_by(:x)
22
+ expect(subject.instance_variable_get(:@deep_complex_nesting)).to be_nil
23
+ end
24
+
25
+ it 'is reset when exposures are cleared' do
26
+ subject << Grape::Entity::Exposure.new(:x, {})
27
+ expect(subject.instance_variable_get(:@deep_complex_nesting)).to be_nil
28
+ subject.deep_complex_nesting?
29
+ expect(subject.instance_variable_get(:@deep_complex_nesting)).to_not be_nil
30
+ subject.clear
31
+ expect(subject.instance_variable_get(:@deep_complex_nesting)).to be_nil
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ describe Grape::Entity::Exposure do
4
+ let(:fresh_class) { Class.new(Grape::Entity) }
5
+ let(:model) { double(attributes) }
6
+ let(:attributes) do
7
+ {
8
+ name: 'Bob Bobson',
9
+ email: 'bob@example.com',
10
+ birthday: Time.gm(2012, 2, 27),
11
+ fantasies: ['Unicorns', 'Double Rainbows', 'Nessy'],
12
+ characteristics: [
13
+ { key: 'hair_color', value: 'brown' }
14
+ ],
15
+ friends: [
16
+ double(name: 'Friend 1', email: 'friend1@example.com', characteristics: [], fantasies: [], birthday: Time.gm(2012, 2, 27), friends: []),
17
+ double(name: 'Friend 2', email: 'friend2@example.com', characteristics: [], fantasies: [], birthday: Time.gm(2012, 2, 27), friends: [])
18
+ ]
19
+ }
20
+ end
21
+ let(:entity) { fresh_class.new(model) }
22
+ subject { fresh_class.find_exposure(:name) }
23
+
24
+ describe '#key' do
25
+ it 'returns the attribute if no :as is set' do
26
+ fresh_class.expose :name
27
+ expect(subject.key).to eq :name
28
+ end
29
+
30
+ it 'returns the :as alias if one exists' do
31
+ fresh_class.expose :name, as: :nombre
32
+ expect(subject.key).to eq :nombre
33
+ end
34
+ end
35
+
36
+ describe '#conditions_met?' do
37
+ it 'only passes through hash :if exposure if all attributes match' do
38
+ fresh_class.expose :name, if: { condition1: true, condition2: true }
39
+
40
+ expect(subject.conditions_met?(entity, {})).to be false
41
+ expect(subject.conditions_met?(entity, condition1: true)).to be false
42
+ expect(subject.conditions_met?(entity, condition1: true, condition2: true)).to be true
43
+ expect(subject.conditions_met?(entity, condition1: false, condition2: true)).to be false
44
+ expect(subject.conditions_met?(entity, condition1: true, condition2: true, other: true)).to be true
45
+ end
46
+
47
+ it 'looks for presence/truthiness if a symbol is passed' do
48
+ fresh_class.expose :name, if: :condition1
49
+
50
+ expect(subject.conditions_met?(entity, {})).to be false
51
+ expect(subject.conditions_met?(entity, condition1: true)).to be true
52
+ expect(subject.conditions_met?(entity, condition1: false)).to be false
53
+ expect(subject.conditions_met?(entity, condition1: nil)).to be false
54
+ end
55
+
56
+ it 'looks for absence/falsiness if a symbol is passed' do
57
+ fresh_class.expose :name, unless: :condition1
58
+
59
+ expect(subject.conditions_met?(entity, {})).to be true
60
+ expect(subject.conditions_met?(entity, condition1: true)).to be false
61
+ expect(subject.conditions_met?(entity, condition1: false)).to be true
62
+ expect(subject.conditions_met?(entity, condition1: nil)).to be true
63
+ end
64
+
65
+ it 'only passes through proc :if exposure if it returns truthy value' do
66
+ fresh_class.expose :name, if: ->(_, opts) { opts[:true] }
67
+
68
+ expect(subject.conditions_met?(entity, true: false)).to be false
69
+ expect(subject.conditions_met?(entity, true: true)).to be true
70
+ end
71
+
72
+ it 'only passes through hash :unless exposure if any attributes do not match' do
73
+ fresh_class.expose :name, unless: { condition1: true, condition2: true }
74
+
75
+ expect(subject.conditions_met?(entity, {})).to be true
76
+ expect(subject.conditions_met?(entity, condition1: true)).to be true
77
+ expect(subject.conditions_met?(entity, condition1: true, condition2: true)).to be false
78
+ expect(subject.conditions_met?(entity, condition1: false, condition2: true)).to be true
79
+ expect(subject.conditions_met?(entity, condition1: true, condition2: true, other: true)).to be false
80
+ expect(subject.conditions_met?(entity, condition1: false, condition2: false)).to be true
81
+ end
82
+
83
+ it 'only passes through proc :unless exposure if it returns falsy value' do
84
+ fresh_class.expose :name, unless: ->(_, opts) { opts[:true] == true }
85
+
86
+ expect(subject.conditions_met?(entity, true: false)).to be true
87
+ expect(subject.conditions_met?(entity, true: true)).to be false
88
+ end
89
+ end
90
+ end
metadata CHANGED
@@ -1,111 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-entity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.8
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bleigh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-10 00:00:00.000000000 Z
11
+ date: 2015-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: multi_json
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: 1.3.2
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: 1.3.2
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: maruku
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: yard
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - '>='
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ~>
88
88
  - !ruby/object:Gem::Version
89
89
  version: '2.9'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
94
+ - - ~>
95
95
  - !ruby/object:Gem::Version
96
96
  version: '2.9'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: bundler
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ">="
101
+ - - '>='
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ">="
108
+ - - '>='
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  description: Extracted from Grape, A Ruby framework for rapid API development with
@@ -116,12 +116,12 @@ executables: []
116
116
  extensions: []
117
117
  extra_rdoc_files: []
118
118
  files:
119
- - ".gitignore"
120
- - ".rspec"
121
- - ".rubocop.yml"
122
- - ".rubocop_todo.yml"
123
- - ".travis.yml"
124
- - ".yardopts"
119
+ - .gitignore
120
+ - .rspec
121
+ - .rubocop.yml
122
+ - .rubocop_todo.yml
123
+ - .travis.yml
124
+ - .yardopts
125
125
  - CHANGELOG.md
126
126
  - CONTRIBUTING.md
127
127
  - Gemfile
@@ -134,6 +134,11 @@ files:
134
134
  - grape-entity.gemspec
135
135
  - lib/grape-entity.rb
136
136
  - lib/grape_entity.rb
137
+ - lib/grape_entity/condition.rb
138
+ - lib/grape_entity/condition/base.rb
139
+ - lib/grape_entity/condition/block_condition.rb
140
+ - lib/grape_entity/condition/hash_condition.rb
141
+ - lib/grape_entity/condition/symbol_condition.rb
137
142
  - lib/grape_entity/delegator.rb
138
143
  - lib/grape_entity/delegator/base.rb
139
144
  - lib/grape_entity/delegator/fetchable_object.rb
@@ -141,8 +146,20 @@ files:
141
146
  - lib/grape_entity/delegator/openstruct_object.rb
142
147
  - lib/grape_entity/delegator/plain_object.rb
143
148
  - lib/grape_entity/entity.rb
149
+ - lib/grape_entity/exposure.rb
150
+ - lib/grape_entity/exposure/base.rb
151
+ - lib/grape_entity/exposure/block_exposure.rb
152
+ - lib/grape_entity/exposure/delegator_exposure.rb
153
+ - lib/grape_entity/exposure/formatter_block_exposure.rb
154
+ - lib/grape_entity/exposure/formatter_exposure.rb
155
+ - lib/grape_entity/exposure/nesting_exposure.rb
156
+ - lib/grape_entity/exposure/nesting_exposure/nested_exposures.rb
157
+ - lib/grape_entity/exposure/represent_exposure.rb
158
+ - lib/grape_entity/options.rb
144
159
  - lib/grape_entity/version.rb
145
160
  - spec/grape_entity/entity_spec.rb
161
+ - spec/grape_entity/exposure/nesting_exposure/nested_exposures_spec.rb
162
+ - spec/grape_entity/exposure_spec.rb
146
163
  - spec/spec_helper.rb
147
164
  homepage: https://github.com/ruby-grape/grape-entity
148
165
  licenses:
@@ -154,19 +171,23 @@ require_paths:
154
171
  - lib
155
172
  required_ruby_version: !ruby/object:Gem::Requirement
156
173
  requirements:
157
- - - ">="
174
+ - - '>='
158
175
  - !ruby/object:Gem::Version
159
176
  version: '0'
160
177
  required_rubygems_version: !ruby/object:Gem::Requirement
161
178
  requirements:
162
- - - ">="
179
+ - - '>='
163
180
  - !ruby/object:Gem::Version
164
181
  version: '0'
165
182
  requirements: []
166
183
  rubyforge_project: grape-entity
167
- rubygems_version: 2.4.6
184
+ rubygems_version: 2.4.5
168
185
  signing_key:
169
186
  specification_version: 4
170
187
  summary: A simple facade for managing the relationship between your model and API.
171
- test_files: []
188
+ test_files:
189
+ - spec/grape_entity/entity_spec.rb
190
+ - spec/grape_entity/exposure/nesting_exposure/nested_exposures_spec.rb
191
+ - spec/grape_entity/exposure_spec.rb
192
+ - spec/spec_helper.rb
172
193
  has_rdoc: