Created
February 1, 2016 13:27
-
-
Save Fryie/667791e574a1ae3cb47d to your computer and use it in GitHub Desktop.
easy keyword classes
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 KClass | |
def self.[](*keywords) | |
klass = Class.new | |
klass.send(:define_method, :initialize) do |hash| | |
keywords.each do |kw| | |
instance_variable_set("@#{kw}", hash[kw]) | |
end | |
end | |
keywords.each do |kw| | |
klass.send :attr_reader, kw | |
end | |
klass | |
end | |
end | |
PaymentProcessor = KClass[:order, :user] | |
class PaymentProcessor | |
def put_user | |
puts user | |
end | |
end | |
PaymentProcessor.new(order: 'foo', user: 'bar').put_user |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment