ensure_param_exists 0.0.1 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/ensure_param_exists.rb +20 -5
  2. metadata +18 -2
@@ -1,11 +1,26 @@
1
+ require 'rails'
2
+
1
3
  module EnsureParamExists
2
- def define_ensure_param_exists_for(*expected_params)
3
- expected_params.each do |expected_param|
4
- method_name = "ensure_#{expected_param}_exists"
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ def self.define_ensure_param_exists_for(*expected_params)
8
+ expected_params.each do |expected_param|
9
+ method_name = "ensure_#{expected_param}_exists"
10
+ define_method(method_name) do
11
+ return unless params[expected_param.to_sym].blank?
12
+ render json: { success: false, message: "missing #{expected_param.to_s} parameter" }, status: 422
13
+ end
14
+ end
15
+ end
16
+
17
+ def self.define_ensure_any_param_exists_for(*expected_params)
18
+ method_name = "ensure_#{expected_params.join("_or_")}_exists"
5
19
  define_method(method_name) do
6
- return unless params[expected_param.to_sym].blank?
7
- render json: { success: false, message: "missing #{expected_param.to_s} parameter" }, status: 422
20
+ return unless expected_params.all? { |expected_param| params[expected_param.to_sym].blank? }
21
+ render json: { success: false, message: "missing #{expected_params.join(" or ")} parameter" }, status: 422
8
22
  end
9
23
  end
10
24
  end
25
+
11
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ensure_param_exists
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,23 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2013-10-01 00:00:00.000000000 Z
13
- dependencies: []
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 4.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 4.0.0
14
30
  description: A quick mixin that lets you quickly define methods to ensure a parameter
15
31
  exists on a request
16
32
  email: gavin@petrofeed.com