Created
May 28, 2018 16:09
-
-
Save alebian/0cff0ca5a97d4de61430a5c04a083d3c 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 BasicStruct | |
ATTRIBUTES = %i[] | |
def initialize(options = {}) | |
attributes = options.with_indifferent_access | |
self.class::ATTRIBUTES.each do |attribute| | |
instance_variable_set("@#{attribute}", attributes[attribute]) | |
class_eval { attr_reader attribute } | |
end | |
end | |
def to_h | |
self.class::ATTRIBUTES.each_with_object({}) do |attribute, hash| | |
hash[attribute] = instance_variable_get("@#{attribute}") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment