Skip to content

Instantly share code, notes, and snippets.

@pnomolos
Last active May 10, 2016 16:44
Show Gist options
  • Save pnomolos/e4f585b4fb6da4f06f9e1e376555f369 to your computer and use it in GitHub Desktop.
Save pnomolos/e4f585b4fb6da4f06f9e1e376555f369 to your computer and use it in GitHub Desktop.
dry-types => Mutable Struct
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