Last active
December 24, 2015 02:59
-
-
Save davejachimiak/6734187 to your computer and use it in GitHub Desktop.
FromHashable
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 FromHashable | |
def initialize hash | |
@hash = hash | |
set_ivars | |
set_methods | |
set_as_hash | |
end | |
private | |
def set_ivars | |
@hash.each do |k, v| | |
instance_variable_set :"@#{k}", v | |
end | |
end | |
def set_methods | |
@hash.each { |k, v| define_singleton_method k.to_sym, ->{ v } } | |
end | |
def set_as_hash | |
define_singleton_method :as_hash, ->{ @hash } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment