subroutine 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9cd32153de5f8bf76c3a0c0ad71bd48d195cf0c3
4
- data.tar.gz: 7b5045cedc5c4e544e179ab3d3f45e64e9607a28
3
+ metadata.gz: 481c515ae237b913f46e2d14a675158516d83ab2
4
+ data.tar.gz: d57eb792cba74077af2c4dcbc4f500a941931808
5
5
  SHA512:
6
- metadata.gz: 91b76020520dc64668f1d8494cbc44daa98bdc116a597eae5b7aaabe135b25d3918cfbbdeb0696c4bd073ecf4b23527e8876a02a7f676d34c27a01821c4a6ab7
7
- data.tar.gz: 13454289c41dc9e69dd0a3d2cbb2e42f9ac211a46bc80ac2129db1adc2070b47218b2dd29962d752ccbd940473c93fe0f8dd6ad8e950642f4d428f4ba734ba64
6
+ metadata.gz: 7deb258fb423207af9997ca3900554492c0038ea891f81e0af696b367c4a6953ec58405977115ce42a07ac56cc34a689860b0ac56524b139a7560356e42a07fb
7
+ data.tar.gz: 32b8ca20632f2bcaba78b1fac42478d0589ccdc3c04eb4a08eb4eba018215610ce23c2fc1667fcd89494e2edfad69268cfcc7538d8fbae458e3b7d8cd507868b
data/README.md CHANGED
@@ -122,7 +122,7 @@ end
122
122
  ```
123
123
  ## Op Implementation
124
124
 
125
- Ops have some fluff, but not much. The `Subroutine::Op` class entire purpose in life is to validate user input and execute
125
+ Ops have some fluff, but not much. The `Subroutine::Op` class' entire purpose in life is to validate user input and execute
126
126
  a series of operations. To enable this we filter input params, type cast params (if desired), and execute validations. Only
127
127
  after these things are complete will the `Op` perform it's operation.
128
128
 
@@ -152,6 +152,7 @@ class MyOp < ::Subroutine::Op
152
152
  date :dob
153
153
  boolean :tos, :default => false
154
154
  end
155
+ ```
155
156
 
156
157
  #### Validations
157
158
 
@@ -222,8 +223,8 @@ Notice we do not declare `perform` as a public method. This is to ensure the "pu
222
223
 
223
224
  Reporting errors is very important in Subroutine Ops since these can be used as form objects. Errors can be reported a couple different ways:
224
225
 
225
- 1) `errors.add(:key, :error)` That is, the way you add errors to an ActiveModel object. Then either return false from your op OR raise an error like `raise ::Subroutine::Failure.new(this)`.
226
- 2) `inherit_errors(error_object_or_activemodel_object)` Same as `errors.add`, but it iterates an existing error hash and inherits the errors. As part of this iteration,
226
+ 1. `errors.add(:key, :error)` That is, the way you add errors to an ActiveModel object. Then either return false from your op OR raise an error like `raise ::Subroutine::Failure.new(this)`.
227
+ 2. `inherit_errors(error_object_or_activemodel_object)` Same as `errors.add`, but it iterates an existing error hash and inherits the errors. As part of this iteration,
227
228
  it checks whether the key in the provided error_object matches a field (or aka of a field) in our op. If there is a match, the error will be placed on
228
229
  that field, but if there is not, the error will be placed on `:base`. Again, after adding the errors to our op, we must return `false` from the perform method or raise a Subroutine::Failure.
229
230
 
data/lib/subroutine/op.rb CHANGED
@@ -151,6 +151,15 @@ module Subroutine
151
151
 
152
152
  protected
153
153
 
154
+ # ensure that our type caster has the opportunity to cast each key
155
+ def params
156
+ out = {}
157
+ @params.keys.each do |k|
158
+ out[k] = send(k)
159
+ end
160
+ out
161
+ end
162
+
154
163
  def type_caster
155
164
  @type_caster ||= ::Subroutine::TypeCaster.new
156
165
  end
@@ -101,13 +101,25 @@ module Subroutine
101
101
  end
102
102
 
103
103
  def _cast_hash(value)
104
+ return _cast_action_controller_query_params(value) if is_action_controller_query_params?(value)
104
105
  return value if value.is_a?(Hash)
105
106
  return {} if value.blank?
107
+ return value.to_hash if value.respond_to?(:to_hash)
106
108
  return value.to_h if value.respond_to?(:to_h)
107
109
  return ::Hash[value.to_a] if value.respond_to?(:to_a)
108
110
  {}
109
111
  end
110
112
 
113
+ def _cast_action_controller_query_params(value)
114
+ value = value.to_hash
115
+ value.each_pair{|k,v| value[k] = _cast_action_controller_query_params(v) if is_action_controller_query_params?(v) }
116
+ value
117
+ end
118
+
119
+ def is_action_controller_query_params?(value)
120
+ value.class.name == "ActionController::Parameters"
121
+ end
122
+
111
123
  def cast_array(value)
112
124
  return [] if value.blank?
113
125
  ::Array.wrap(value)
@@ -2,7 +2,7 @@ module Subroutine
2
2
 
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- PATCH = 1
5
+ PATCH = 2
6
6
  PRE = nil
7
7
 
8
8
  VERSION = [MAJOR, MINOR, PATCH, PRE].compact.join('.')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subroutine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Nelson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-11 00:00:00.000000000 Z
11
+ date: 2015-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel