Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implementation of #let

  • Loading branch information...
commit f1140c46c0f11f2eacd8513d805f2f7dea2899c9 0 parents
Stephen Caudill authored
9 Rakefile
@@ -0,0 +1,9 @@
+require 'spec/rake/spectask'
+
+task :default => :spec
+
+desc "Run specs"
+Spec::Rake::SpecTask.new do |t|
+ t.spec_files = FileList['spec/**/*_spec.rb']
+ t.spec_opts = %w(-fs --color)
+end
19 lib/resourceful.rb
@@ -0,0 +1,19 @@
+module Resourceful
+ extend self
+ def let(name, &block)
+ define_method name do
+ @__resources__ ||= {}
+ @__resources__[name] ||= if block_given?
+ instance_eval(&block)
+ else
+ class_for(name).find(params["#{name}_id"] || params['id'])
+ end
+ end
+ helper_method name
+ hide_action name
+ end
+
+ def class_for(name)
+ name.to_s.classify.constantize
+ end
+end
2  spec/helper.rb
@@ -0,0 +1,2 @@
+require 'mocha'
+require File.join(File.dirname(__FILE__), '..', 'lib', 'resourceful')
95 spec/lib/resourceful_spec.rb
@@ -0,0 +1,95 @@
+require File.join(File.dirname(__FILE__), '..', 'helper')
+
+class Quacker
+ extend Resourceful
+ def self.helper_method(*args); end
+ def self.hide_action(*args); end
+ def self.find(*args); end
+ def memoizable(*args); args; end
+ def params; {'proxy_id' => 42}; end
+ let(:proxy)
+ let(:quack){ memoizable('quack!') }
+end
+
+describe Resourceful do
+ context "classes extending Resourceful" do
+ it "respond to :let" do
+ Quacker.respond_to?(:let).should be_true
+ end
+ end
+
+ context "#let" do
+ let(:instance){ Quacker.new }
+
+ it "creates a method with the given name" do
+ instance.methods.should include('quack')
+ end
+
+ it "prevents the method from being a callable action" do
+ Quacker.expects(:hide_action).with(:blerg)
+ class Quacker
+ let(:blerg){ 'ehm' }
+ end
+ end
+
+ it "declares the method as a helper method" do
+ Quacker.stubs(:hide_action)
+ Quacker.expects(:helper_method).with(:blarg)
+ class Quacker
+ let(:blarg){ 'uhm' }
+ end
+ end
+
+ it "returns the value of the method" do
+ instance.quack.should == %w(quack!)
+ end
+
+ it "memoizes the value of the created method" do
+ instance.expects(:memoizable).once.returns('value')
+ instance.quack
+ instance.quack
+ end
+
+ context "when no block is given" do
+ before do
+ instance.stubs(:class_for).returns(Quacker)
+ end
+ it "attempts to guess the class of the resource to let" do
+ instance.expects(:class_for).with(:proxy).returns(Quacker)
+ instance.proxy
+ end
+ it "calls find with {resource}_id on the resources class" do
+ Quacker.expects(:find).with(42)
+ instance.proxy
+ end
+ context "and there is no {resource}_id" do
+ before do
+ class Quacker
+ def params; {'id' => 24}; end
+ end
+ end
+ it "calls find with params[:id] on the resources class" do
+ Quacker.expects(:find).with(24)
+ instance.proxy
+ end
+ end
+ end
+ end
+
+ describe '#class_for' do
+ let(:name){ 'quacker' }
+ let(:classified_name){ 'Quacker' }
+ before do
+ name.stubs(:to_s => name, :classify => classified_name)
+ classified_name.stubs(:constantize => Quacker)
+ end
+ it 'retrieves a string representation of the class name' do
+ name.expects(:classify).returns(classified_name)
+ Quacker.class_for(name)
+ end
+ it 'returns the string representation of the name as a constant' do
+ classified_name.expects(:constantize)
+ Quacker.class_for(name)
+ end
+ end
+end
2  spec/spec.opts
@@ -0,0 +1,2 @@
+--colour
+--format=specdoc
Please sign in to comment.
Something went wrong with that request. Please try again.