Last active
May 10, 2016 16:44
-
-
Save pnomolos/e4f585b4fb6da4f06f9e1e376555f369 to your computer and use it in GitHub Desktop.
dry-types => Mutable Struct
This file contains 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
module Dry | |
module Types | |
class Struct | |
# A variation on dry-types Struct that includes property writers | |
class Mutable < Struct | |
constructor_type :schema | |
def self.attributes(new_schema) | |
super | |
new_schema.keys.each do |key| | |
define_method("#{key}=") do |value| | |
# Courtesy of Andy Holland in the dry-rb gitter chat | |
instance_variable_set("@#{key}", self.class.schema[key][value]) | |
end | |
end | |
self | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment