Skip to content

Instantly share code, notes, and snippets.

@ta

ta/dry.rb

Last active Dec 21, 2018
Embed
What would you like to do?
require "dry-struct"
require "dry-types"
class Base < Dry::Struct
transform_keys(&:to_sym)
module Types
include Dry::Types.module
end
end
class MyStruct < Base
attribute :id, Types::Strict::Integer
attribute :data, Types::Hash.schema(
code: Types::Strict::Integer.optional.default(nil),
msg: Types::String.optional.default(nil),
)
end
puts MyStruct.new({"id" => 1, "data" => { "code" => 200 }}).inspect
# => #<MyStruct id=1 data={:code=>nil, :msg=>nil}>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.