File tree Expand file tree Collapse file tree 2 files changed +28
-5
lines changed Expand file tree Collapse file tree 2 files changed +28
-5
lines changed Original file line number Diff line number Diff line change @@ -47,11 +47,25 @@ def default_attributes
47
47
def build_params ( params )
48
48
raw_params = ( params || { } ) . with_indifferent_access
49
49
defaults = default_attributes . with_indifferent_access
50
- @params = defaults . deep_merge ( raw_params )
50
+ @params = cast_types ( defaults . deep_merge ( raw_params ) )
51
51
end
52
52
53
53
def build_structure
54
54
@structure = OpenStruct . new ( @attrs )
55
55
end
56
+
57
+ # A naive attempt to cast the attribute types of the incoming mock data
58
+ # based on any available type information provided in :default_attributes
59
+ def cast_types ( params )
60
+ default_attributes . each do |key , value |
61
+ params [ key ] = case true
62
+ when value . is_a? ( String ) then String ( params [ key ] )
63
+ when value . is_a? ( Float ) then Float ( params [ key ] )
64
+ when value . is_a? ( Integer ) then Integer ( params [ key ] )
65
+ else params [ key ]
66
+ end
67
+ end
68
+ params
69
+ end
56
70
end
57
71
end
Original file line number Diff line number Diff line change 11
11
Class . new ( my_temp_resource ) do
12
12
def self . default_attributes
13
13
{
14
+ id : 1 ,
15
+ price : 0.0 ,
14
16
qux : 'qux' ,
15
17
_embedded : {
16
18
embedded_resource : {
@@ -24,7 +26,9 @@ def self.default_attributes
24
26
25
27
let ( :params ) do
26
28
{
27
- foo : 1 ,
29
+ id : '1' ,
30
+ foo : 'bar' ,
31
+ price : '0.0' ,
28
32
bar : 'baz' ,
29
33
_embedded : {
30
34
embedded_resource : {
@@ -52,16 +56,21 @@ def self.default_attributes
52
56
subject { super ( ) . properties }
53
57
54
58
it 'returns a hash of available properties' do
55
- expect ( subject ) . to include 'foo' => 'fixnum'
59
+ expect ( subject ) . to include 'id' => 'fixnum'
60
+ expect ( subject ) . to include 'price' => 'float'
61
+ expect ( subject ) . to include 'foo' => 'string'
62
+ expect ( subject ) . to include 'foo' => 'string'
56
63
expect ( subject ) . to include 'bar' => 'string'
57
64
end
58
65
end
59
66
60
67
describe '#attributes' do
61
68
subject { super ( ) . attributes }
62
69
63
- it 'returns a hash of the resources attributes' do
64
- expect ( subject ) . to include 'foo' => 1
70
+ it 'returns a hash of the resources type-casted attributes' do
71
+ expect ( subject ) . to include 'id' => 1
72
+ expect ( subject ) . to include 'price' => 0.0
73
+ expect ( subject ) . to include 'foo' => 'bar'
65
74
expect ( subject ) . to include 'bar' => 'baz'
66
75
expect ( subject ) . to include 'qux' => 'qux'
67
76
expect ( subject [ '_embedded' ] [ 'embedded_resource' ] ) . to include 'plugh' => 'xyzzy'
You can’t perform that action at this time.
0 commit comments