Created
October 23, 2017 21:47
-
-
Save flash-gordon/b36479b54b4c9131712106bd27e65bed to your computer and use it in GitHub Desktop.
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
class User < Dry::Struct | |
MissingAttribute = Class.new(NameError) | |
def name | |
[first_name, last_name].join(' ') | |
end | |
attribute :name, Dry::Types['strict.string'] | |
attribute :first_name, Dry::Types['strict.string'] | |
private | |
def method_missing(*) | |
super | |
rescue NameError => error | |
raise MissingAttribute.new("#{ error.message } (not loaded attribute?)") | |
end | |
end | |
user = User.new(first_name: 'John', name: 'John Doe') | |
user.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment