Skip to content

Commit f335fe3

Browse files
aj0strowdblock
authored andcommitted
Raise ArgumentError on invalid option and alias with to using.
1 parent c438fe1 commit f335fe3

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Next Release
1212
* [#43](https://github.com/intridea/grape-entity/pull/43): Call procs in context of entity instance - [@joelvh](https://github.com/joelvh).
1313
* [#47](https://github.com/intridea/grape-entity/pull/47): Support nested exposures - [@wyattisimo](https://github.com/wyattisimo).
1414
* [#46](https://github.com/intridea/grape-entity/issues/46), [#50](https://github.com/intridea/grape-entity/pull/50): Added support for specifying the presenter class in `using` in string format - [@larryzhao](https://github.com/larryzhao).
15+
* [#51](https://github.com/intridea/grape-entity/pull/51): Raise `ArgumentError` if an unknown option is used with `expose` - [@aj0strow](https://github.com/aj0strow).
16+
* [#51](https://github.com/intridea/grape-entity/pull/51): Alias `:with` to `:using`, consistently with the Grape api endpoints - [@aj0strow](https://github.com/aj0strow).
1517
* Your contribution here.
1618

1719
0.3.0 (2013-03-29)

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ module API
275275
end
276276
```
277277

278+
Also note that an `ArgumentError` is raised when unknown options are passed to either `expose` or `with_options`.
279+
278280
## Installation
279281

280282
Add this line to your application's Gemfile:

lib/grape_entity/entity.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def self.expose(*args, &block)
161161
# end
162162
# end
163163
def self.with_options(options)
164-
(@block_options ||= []).push(options)
164+
(@block_options ||= []).push(valid_options(options))
165165
yield
166166
@block_options.pop
167167
end
@@ -479,6 +479,11 @@ def conditions_met?(exposure_options, options)
479479

480480
private
481481

482+
# All supported options.
483+
OPTIONS = [
484+
:as, :if, :unless, :using, :with, :proc, :documentation, :format_with, :safe, :if_extras, :unless_extras
485+
].to_set.freeze
486+
482487
# Merges the given options with current block options.
483488
#
484489
# @param options [Hash] Exposure options.
@@ -504,7 +509,20 @@ def self.merge_options(options)
504509
@block_options ||= []
505510
opts.merge @block_options.inject({}) { |final, step|
506511
final.merge(step, &merge_logic)
507-
}.merge(options, &merge_logic)
512+
}.merge(valid_options(options), &merge_logic)
513+
end
514+
515+
# Raises an error if the given options include unknown keys.
516+
# Renames aliased options.
517+
#
518+
# @param options [Hash] Exposure options.
519+
def self.valid_options(options)
520+
options.keys.each do |key|
521+
raise ArgumentError, "#{key.inspect} is not a valid option." unless OPTIONS.include?(key)
522+
end
523+
524+
options[:using] = options.delete(:with) if options.has_key?(:with)
525+
options
508526
end
509527
end
510528
end

spec/grape_entity/entity_spec.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
end
1616

1717
it 'sets the same options for all exposures passed' do
18-
subject.expose :name, :email, :location, foo: :bar
19-
subject.exposures.values.each { |v| v.should == { foo: :bar } }
18+
subject.expose :name, :email, :location, documentation: true
19+
subject.exposures.values.each { |v| v.should == { documentation: true } }
2020
end
2121
end
2222

@@ -29,6 +29,10 @@
2929
it 'makes sure that :format_with as a proc cannot be used with a block' do
3030
expect { subject.expose :name, format_with: proc {} {} }.to raise_error ArgumentError
3131
end
32+
33+
it 'makes sure unknown options are not silently ignored' do
34+
expect { subject.expose :name, unknown: nil }.to raise_error ArgumentError
35+
end
3236
end
3337

3438
context 'with a block' do
@@ -203,6 +207,16 @@ class BogusEntity < Grape::Entity
203207
end
204208

205209
describe '.with_options' do
210+
it 'raises an error for unknown options' do
211+
block = proc do
212+
with_options(unknown: true) do
213+
expose :awesome_thing
214+
end
215+
end
216+
217+
expect { subject.class_eval(&block) }.to raise_error ArgumentError
218+
end
219+
206220
it 'applies the options to all exposures inside' do
207221
subject.class_eval do
208222
with_options(if: { awesome: true }) do
@@ -295,6 +309,15 @@ class BogusEntity < Grape::Entity
295309
subject.exposures[:awesome_thing].should == { using: 'SomethingElse' }
296310
end
297311

312+
it 'aliases :with option to :using option' do
313+
subject.class_eval do
314+
with_options(using: 'Something') do
315+
expose :awesome_thing, with: 'SomethingElse'
316+
end
317+
end
318+
subject.exposures[:awesome_thing].should == { using: 'SomethingElse' }
319+
end
320+
298321
it 'overrides nested :proc option' do
299322
match_proc = lambda { |obj, opts| 'more awesomer' }
300323

0 commit comments

Comments
 (0)