forked from garybernhardt/cls
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3de871
commit 273e101
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Cls | ||
def self.included(base) | ||
base.send :extend, ClassMethods | ||
end | ||
|
||
module ClassMethods | ||
def takes(*args) | ||
define_initialize(args) | ||
end | ||
|
||
def define_initialize(args) | ||
assignments = args.map { |a| "@#{a} = #{a}" }.join("\n") | ||
self.class_eval %{ | ||
def initialize(#{args.join(", ")}) | ||
#{assignments} | ||
end | ||
} | ||
end | ||
|
||
def let(name, &block) | ||
define_method(name, &block) | ||
end | ||
end | ||
end | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require "./cls" | ||
|
||
class NamePresenter | ||
include Cls | ||
attr_reader :name | ||
takes(:name) | ||
let(:yelled_name) { @name.upcase } | ||
let(:name_with_argument) { |other| } | ||
let(:increment) { @value ||= 0; @value += 1 } | ||
end | ||
|
||
describe Cls do | ||
describe "initializers" do | ||
it "takes initializer arguments" do | ||
NamePresenter.new("Bob").name.should == "Bob" | ||
end | ||
|
||
it "errors when there are too many initializer arguments" do | ||
expect do | ||
NamePresenter.new("Bob", "Smith") | ||
end.to raise_error(ArgumentError) | ||
end | ||
|
||
it "errors when there are too few initializer arguments" do | ||
expect do | ||
NamePresenter.new | ||
end.to raise_error(ArgumentError) | ||
end | ||
end | ||
|
||
describe "let methods" do | ||
let(:presenter) { NamePresenter.new("Bob") } | ||
|
||
it "defines methods" do | ||
presenter.yelled_name.should == "BOB" | ||
end | ||
|
||
it "does not cache method return values" do | ||
presenter.increment.should_not == presenter.increment | ||
end | ||
|
||
it "errors when there are too many method arguments" do | ||
expect do | ||
presenter.yelled_name("extra argument") | ||
end.to raise_error(ArgumentError) | ||
end | ||
|
||
it "errors when there are too few method arguments" do | ||
expect do | ||
presenter.name_with_argument | ||
end.to raise_error(ArgumentError) | ||
end | ||
end | ||
|
||
it "doesn't pollute other classes" do | ||
expect do | ||
Class.new { takes :name } | ||
end.to raise_error(NameError) | ||
end | ||
end | ||
|