Skip to content

Instantly share code, notes, and snippets.

@Morozzzko
Created October 31, 2021 14:53
Show Gist options
  • Select an option

  • Save Morozzzko/d2b357d6865dbd79f93aeca8ba40da2d to your computer and use it in GitHub Desktop.

Select an option

Save Morozzzko/d2b357d6865dbd79f93aeca8ba40da2d to your computer and use it in GitHub Desktop.
Using dry-types & dry-struct ADT to automap data
# frozen_string_literal: true
require 'bundler/inline'
require 'irb'
gemfile do
gem 'dry-types', '~> 1.0'
gem 'dry-struct', '~> 1.0'
end
require 'dry-types'
require 'dry-struct'
module Types
include Dry.Types
end
class UserRegistered < Dry::Struct
attribute :type, Types.Value('user_registered')
attribute :name, Types::String
end
class OrganizationRemoved < Dry::Struct
attribute :type, Types.Value('organization_removed')
attribute :reason, Types::String
end
Event = UserRegistered | OrganizationRemoved
user_registered = Event[type: 'user_registered', name: 'John Doe']
organization_removed = Event[type: 'organization_removed', reason: 'Bankrupt']
binding.irb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment