public
Created

  • Download Gist
awesome_resources_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
module AwesomeResource
def initialize(attributes = {})
attributes.each do |key,val|
sym = key.to_sym
reader = :"#{sym}"
writer = :"#{sym}="
 
unless respond_to?(reader)
self.class.send(:attr_reader, sym)
end
 
unless respond_to?(writer)
self.class.send(:attr_writer, sym)
end
 
send(writer, val)
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

This is a bit shorter: https://gist.github.com/DNNX/5643769 .

I removed unnecessary conversion of strings to symbols and vice versa.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.