subroutine 0.8.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/subroutine/association.rb +4 -0
- data/lib/subroutine/fields.rb +7 -1
- data/lib/subroutine/type_caster.rb +12 -0
- data/lib/subroutine/version.rb +2 -2
- data/test/subroutine/association_test.rb +40 -0
- data/test/subroutine/fields_test.rb +22 -1
- data/test/subroutine/type_caster_test.rb +8 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ab6d4e06e0400af01060d41f08ba6b61695c24a
|
4
|
+
data.tar.gz: c6542be4a9832df325fe8f33ad33f297dfd8912c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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}"
|
data/lib/subroutine/fields.rb
CHANGED
@@ -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
|
-
|
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
|
data/lib/subroutine/version.rb
CHANGED
@@ -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
|
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
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
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.
|
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-
|
11
|
+
date: 2019-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|