Created
October 31, 2021 14:53
-
-
Save Morozzzko/d2b357d6865dbd79f93aeca8ba40da2d to your computer and use it in GitHub Desktop.
Using dry-types & dry-struct ADT to automap data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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