ensure_param_exists 0.0.4 → 0.0.5

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 +23 -13
  2. metadata +1 -1
@@ -1,26 +1,36 @@
1
1
  require 'rails'
2
+ require 'action_controller'
2
3
 
3
4
  module EnsureParamExists
4
5
  extend ActiveSupport::Concern
5
6
 
6
7
  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
8
+ def self.ensure_param(expected_param, opts = {})
9
+ _ensure_param_exists_(:and, [expected_param, opts])
10
+ end
11
+
12
+ def self.ensure_any_params(*expected_params)
13
+ _ensure_param_exists_(:or, expected_params)
14
+ end
15
+
16
+ def self.ensure_all_params(*expected_params)
17
+ _ensure_param_exists_(:and, expected_params)
15
18
  end
16
19
 
17
- def self.define_ensure_any_param_exists_for(*expected_params)
18
- method_name = "ensure_#{expected_params.join("_or_")}_exists"
20
+ def self._ensure_param_exists_(operator, expected_params)
21
+ opts = expected_params.last.kind_of?(Hash) ? expected_params.pop : {}
22
+
23
+ method_name = "ensure_#{expected_params.join("_#{operator}_")}_exists"
19
24
  define_method(method_name) do
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
25
+ return if expected_params.send(_operator_map_[operator]) { |expected_param| params[expected_param.to_sym].present? }
26
+ render json: { success: false, message: "missing #{expected_params.join(" #{operator} ")} parameter" }, status: 422
22
27
  end
28
+
29
+ before_filter method_name.to_sym, opts
23
30
  end
24
- end
25
31
 
32
+ def _operator_map_
33
+ { and: 'all?', or: 'any?' }
34
+ end
35
+ end
26
36
  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.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: