2929$LOAD_PATH. unshift ( File . expand_path ( "../lib" , __dir__ ) )
3030require "structure"
3131
32- # Test data with coercion
32+ # Test data with more coercions and nested structures
3333TEST_DATA = {
3434 "attr1" => "foo" ,
35- "attr2" => "123" ,
35+ "attr2" => "123" , # Integer coercion
3636 "attr3" => "bar" ,
37- "attr4" => "true" ,
37+ "attr4" => "true" , # Boolean coercion
3838 "attr5" => [ "a" , "b" , "c" ] ,
3939 "attr6" => { "x" => "y" } ,
4040 "attr8" => "optional" ,
41+ "attr9" => "3.14" , # Float coercion
42+ "attr10" => "42" , # Integer coercion
43+ "attr11" => "1" , # Boolean coercion
44+ "items" => [ # Array of nested structures
45+ { "name" => "Item 1" , "price" => "10.99" } ,
46+ { "name" => "Item 2" , "price" => "20.50" } ,
47+ { "name" => "Item 3" , "price" => "15.25" } ,
48+ ] ,
4149} . freeze
4250
4351# Define dry-struct models
@@ -48,6 +56,13 @@ module Types
4856 include Dry . Types ( )
4957 end
5058
59+ class Item < Dry ::Struct
60+ transform_keys ( &:to_sym )
61+
62+ attribute :name , Types ::String
63+ attribute :price , Types ::Coercible ::Float
64+ end
65+
5166 class Model < Dry ::Struct
5267 transform_keys ( &:to_sym )
5368
@@ -59,11 +74,20 @@ class Model < Dry::Struct
5974 attribute :attr6 , Types ::Hash
6075 attribute :attr7 , Types ::String . default ( "default" )
6176 attribute? :attr8 , Types ::String
77+ attribute :attr9 , Types ::Coercible ::Float
78+ attribute :attr10 , Types ::Coercible ::Integer
79+ attribute :attr11 , Types ::Params ::Bool
80+ attribute :items , Types ::Array . of ( Item )
6281 end
6382end
6483
6584# Define Structure models
6685module StructureModels
86+ Item = Structure . new do
87+ attribute ( :name , String )
88+ attribute ( :price , Float )
89+ end
90+
6791 Model = Structure . new do
6892 attribute ( :attr1 , String )
6993 attribute ( :attr2 , Integer )
@@ -73,6 +97,10 @@ module StructureModels
7397 attribute ( :attr6 )
7498 attribute ( :attr7 , String , default : "default" )
7599 attribute? ( :attr8 , String )
100+ attribute ( :attr9 , Float )
101+ attribute ( :attr10 , Integer )
102+ attribute ( :attr11 , :boolean )
103+ attribute ( :items , [ "Item" ] )
76104 end
77105end
78106
0 commit comments