Created
May 24, 2013 19:37
-
-
Save russen/5645979 to your computer and use it in GitHub Desktop.
Responding to http://pivotallabs.com/method-modeling-a-refactoring/
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 AwesomeResource | |
attr_reader :awesome_attributes | |
def initialize(attributes={}) | |
@awesome_attributes = Struct.new(*attributes.keys.map(&:to_sym)).new *attributes.values | |
end | |
def method_missing(method_name, *args) | |
if args.any? | |
awesome_attributes.send(method_name, args.first) | |
else | |
awesome_attributes.send(method_name) | |
end | |
end | |
end | |
describe AwesomeResource do | |
it "creates readers and writers for any attributes passed in during initialization" do | |
article_class = Class.new do | |
include AwesomeResource | |
end | |
article = article_class.new("title" => "Fun") | |
article.title.should == "Fun" | |
article.title = 'Fun Times!' | |
article.title.should == 'Fun Times!' | |
expect { article.unknown_attribute }.to raise_exception | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment