subroutine 0.8.1 → 0.9.0

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: 2703769996395de8478c8c16b7eb8f2826c903b3
4
- data.tar.gz: 42bf18f9555f5977ffcb93508468d399fe62d993
3
+ metadata.gz: 6ab6d4e06e0400af01060d41f08ba6b61695c24a
4
+ data.tar.gz: c6542be4a9832df325fe8f33ad33f297dfd8912c
5
5
  SHA512:
6
- metadata.gz: d7be70968a4c5a9bbc85feabdcef6a1ded8fd04b797acd62266bdab558a51d64cafb93229d220222e5515978c187f328c24f337c0572c135b5b6f6f20e7e9c16
7
- data.tar.gz: 6c04f8721942a5ff271d06065a4b9ebce38f830b5727d0c3b31d7734da379ba613ecab9ee6a319794442fa641ed5c5d7383cb7a07ba5b6814c5c54e5a26f06be
6
+ metadata.gz: 6331a42ddc585caf91166eb7871e96637712ff09ece5794735c525c044650cf87fb337aa36edb6bb31f084a21d6655df4198932d79b0d5f64b25c5138b34ac28
7
+ data.tar.gz: 6d68a2bccecfef89ea3a42689cc565a28b15a4584da2436182fa4ca8cbc25c4e4658e99022b10190622fc57387bfdbd54f028f590a66771a53d9b527f4829d76
@@ -92,6 +92,10 @@ module Subroutine
92
92
  params['#{foreign_key_method}'] = r.nil? ? nil : r.id
93
93
  r
94
94
  end
95
+
96
+ def #{as}_field_provided?
97
+ field_provided?('#{foreign_key_method}')#{poly ? "&& field_provided?('#{foreign_type_method}')" : ""}
98
+ end
95
99
  EV
96
100
 
97
101
  alias_method :"#{as}_without_association", :"#{as}"
@@ -103,6 +103,8 @@ module Subroutine
103
103
 
104
104
  # check if a specific field was provided
105
105
  def field_provided?(key)
106
+ return send(:"#{key}_field_provided?") if respond_to?(:"#{key}_field_provided?", true)
107
+
106
108
  @params.key?(key)
107
109
  end
108
110
 
@@ -113,7 +115,11 @@ module Subroutine
113
115
  _fields.each_pair do |field, config|
114
116
  next unless inputs.key?(field)
115
117
 
116
- out[field] = ::Subroutine::TypeCaster.cast(inputs[field], config)
118
+ begin
119
+ out[field] = ::Subroutine::TypeCaster.cast(inputs[field], config)
120
+ rescue ::Subroutine::TypeCaster::TypeCastError => e
121
+ raise ::Subroutine::TypeCaster::TypeCastError, "Error for field `#{field}`: #{e}"
122
+ end
117
123
  end
118
124
 
119
125
  out
@@ -10,6 +10,15 @@ require 'active_support/core_ext/array/wrap'
10
10
 
11
11
  module Subroutine
12
12
  module TypeCaster
13
+
14
+ class TypeCastError < StandardError
15
+
16
+ def initialize(message)
17
+ super(message)
18
+ end
19
+
20
+ end
21
+
13
22
  def self.casters
14
23
  @casters ||= {}
15
24
  end
@@ -28,6 +37,9 @@ module Subroutine
28
37
  return value unless caster
29
38
 
30
39
  caster.call(value, options)
40
+
41
+ rescue StandardError => e
42
+ raise ::Subroutine::TypeCaster::TypeCastError, e.to_s, e.backtrace
31
43
  end
32
44
  end
33
45
  end
@@ -2,8 +2,8 @@
2
2
 
3
3
  module Subroutine
4
4
  MAJOR = 0
5
- MINOR = 8
6
- PATCH = 1
5
+ MINOR = 9
6
+ PATCH = 0
7
7
  PRE = nil
8
8
 
9
9
  VERSION = [MAJOR, MINOR, PATCH, PRE].compact.join('.')
@@ -102,5 +102,45 @@ module Subroutine
102
102
  assert_equal 'AdminUser', op.admin_type
103
103
  assert_equal doug.id, op.admin_id
104
104
  end
105
+
106
+ def test_it_provides_answers_to_field_provided
107
+ ::User.expects(:all).never
108
+
109
+ op = SimpleAssociationOp.new user: doug
110
+ assert_equal true, op.field_provided?(:user)
111
+ assert_equal true, op.field_provided?(:user_id)
112
+ assert_equal false, op.field_provided?(:user_type)
113
+
114
+ op = SimpleAssociationOp.new user_id: doug.id
115
+ assert_equal true, op.field_provided?(:user)
116
+ assert_equal true, op.field_provided?(:user_id)
117
+ assert_equal false, op.field_provided?(:user_type)
118
+
119
+ op = SimpleAssociationOp.new
120
+ assert_equal false, op.field_provided?(:user)
121
+ assert_equal false, op.field_provided?(:user_id)
122
+ assert_equal false, op.field_provided?(:user_type)
123
+
124
+ op = PolymorphicAssociationOp.new admin: doug
125
+ assert_equal true, op.field_provided?(:admin)
126
+ assert_equal true, op.field_provided?(:admin_id)
127
+ assert_equal true, op.field_provided?(:admin_type)
128
+
129
+ op = PolymorphicAssociationOp.new admin_type: doug.class.name, admin_id: doug.id
130
+ assert_equal true, op.field_provided?(:admin)
131
+ assert_equal true, op.field_provided?(:admin_id)
132
+ assert_equal true, op.field_provided?(:admin_type)
133
+
134
+ op = PolymorphicAssociationOp.new admin_id: doug.id
135
+ assert_equal false, op.field_provided?(:admin)
136
+ assert_equal true, op.field_provided?(:admin_id)
137
+ assert_equal false, op.field_provided?(:admin_type)
138
+
139
+ op = PolymorphicAssociationOp.new
140
+ assert_equal false, op.field_provided?(:admin)
141
+ assert_equal false, op.field_provided?(:admin_id)
142
+ assert_equal false, op.field_provided?(:admin_type)
143
+
144
+ end
105
145
  end
106
146
  end
@@ -12,6 +12,7 @@ module Subroutine
12
12
 
13
13
  string :foo, default: "foo"
14
14
  integer :bar, default: -> { 3 }
15
+ date :baz
15
16
 
16
17
  def initialize(options = {})
17
18
  setup_fields(options)
@@ -20,9 +21,10 @@ module Subroutine
20
21
  end
21
22
 
22
23
  def test_fields_are_configured
23
- assert_equal 2, Whatever._fields.size
24
+ assert_equal 3, Whatever._fields.size
24
25
  assert_equal :string, Whatever._fields[:foo][:type]
25
26
  assert_equal :integer, Whatever._fields[:bar][:type]
27
+ assert_equal :date, Whatever._fields[:baz][:type]
26
28
  end
27
29
 
28
30
  def test_field_defaults_are_handled
@@ -43,6 +45,25 @@ module Subroutine
43
45
  assert_equal false, instance.field_provided?(:bar)
44
46
  end
45
47
 
48
+ def test_field_provided
49
+
50
+ instance = Whatever.new(foo: "abc")
51
+ assert_equal true, instance.field_provided?(:foo)
52
+ assert_equal false, instance.field_provided?(:bar)
53
+
54
+ instance = DefaultsOp.new
55
+ assert_equal false, instance.field_provided?(:foo)
56
+
57
+ instance = DefaultsOp.new(foo: 'foo')
58
+ assert_equal true, instance.field_provided?(:foo)
59
+ end
60
+
61
+ def test_invalid_typecast
62
+ assert_raises "Error for field `baz`: invalid date" do
63
+ Whatever.new(baz: "2015-13-01")
64
+ end
65
+ end
66
+
46
67
  def test_params
47
68
  instance = Whatever.new(foo: "abc")
48
69
  assert_equal({ "foo" => "abc" }, instance.params)
@@ -252,18 +252,14 @@ module Subroutine
252
252
  op.file_input.unlink
253
253
  end
254
254
 
255
- def test_field_provided
256
- op = ::SignupOp.new
257
- assert_equal false, op.send(:field_provided?, :email)
258
-
259
- op = ::SignupOp.new(email: 'foo')
260
- assert_equal true, op.send(:field_provided?, :email)
261
-
262
- op = ::DefaultsOp.new
263
- assert_equal false, op.send(:field_provided?, :foo)
264
-
265
- op = ::DefaultsOp.new(foo: 'foo')
266
- assert_equal true, op.send(:field_provided?, :foo)
255
+ def test_when_a_type_cast_fails_a_type_cast_error_is_raised
256
+ assert_raises Subroutine::TypeCaster::TypeCastError do
257
+ op.date_input = "2015-13-01"
258
+ end
259
+
260
+ assert_raises "invalid date" do
261
+ op.date_input = "2015-13-01"
262
+ end
267
263
  end
268
264
  end
269
265
  end
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.8.1
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Nelson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-03 00:00:00.000000000 Z
11
+ date: 2019-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel