Skip to content

Instantly share code, notes, and snippets.

@flash-gordon
Created October 23, 2017 21:47
Show Gist options
  • Save flash-gordon/b36479b54b4c9131712106bd27e65bed to your computer and use it in GitHub Desktop.
Save flash-gordon/b36479b54b4c9131712106bd27e65bed to your computer and use it in GitHub Desktop.
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