Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Subjects can be accessed by a private method of

the same name.
  • Loading branch information...
commit 822048b646668cd590faef2bf7313f7c8c041776 1 parent 85b0d3f
@fnando authored
Showing with 51 additions and 21 deletions.
  1. +15 −20 Gemfile.lock
  2. +12 −1 lib/simple_presenter/base.rb
  3. +24 −0 spec/base_spec.rb
View
35 Gemfile.lock
@@ -6,28 +6,23 @@ PATH
GEM
remote: http://rubygems.org/
specs:
- coderay (0.9.8)
+ coderay (1.0.7)
diff-lcs (1.1.3)
- method_source (0.6.7)
- ruby_parser (>= 2.3.1)
- pry (0.9.7.4)
- coderay (~> 0.9.8)
- method_source (~> 0.6.7)
- ruby_parser (>= 2.3.1)
- slop (~> 2.1.0)
+ method_source (0.8)
+ pry (0.9.10)
+ coderay (~> 1.0.5)
+ method_source (~> 0.8)
+ slop (~> 3.3.1)
rake (0.9.2.2)
- rspec (2.8.0)
- rspec-core (~> 2.8.0)
- rspec-expectations (~> 2.8.0)
- rspec-mocks (~> 2.8.0)
- rspec-core (2.8.0)
- rspec-expectations (2.8.0)
- diff-lcs (~> 1.1.2)
- rspec-mocks (2.8.0)
- ruby_parser (2.3.1)
- sexp_processor (~> 3.0)
- sexp_processor (3.0.10)
- slop (2.1.0)
+ rspec (2.11.0)
+ rspec-core (~> 2.11.0)
+ rspec-expectations (~> 2.11.0)
+ rspec-mocks (~> 2.11.0)
+ rspec-core (2.11.1)
+ rspec-expectations (2.11.2)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.11.1)
+ slop (3.3.2)
PLATFORMS
ruby
View
13 lib/simple_presenter/base.rb
@@ -12,9 +12,20 @@ class Base
# expose :title, :with => :post # will expose post.title through CommentPresenter#post_title
# end
#
+ # Subjects can be accessed by a private name with the same name.
+ # So the above subjects can be accessed internally by the methods +comment+ and +post+.
+ #
+ # If you're not setting any special name, then you can access it using the +subject+ method.
+ #
def self.subjects(*names)
@subjects ||= [:subject]
- @subjects = names unless names.empty?
+
+ unless names.empty?
+ @subjects = names
+ attr_reader *names
+ private *names
+ end
+
@subjects
end
View
24 spec/base_spec.rb
@@ -66,6 +66,14 @@
its(:name) { should == "John Doe" }
its(:email) { should == "john@doe.com" }
+
+ it "responds to private subject method" do
+ expect(subject.private_methods).to include(:subject)
+ end
+
+ it "returns subject" do
+ expect(subject.send(:subject)).to eql(user)
+ end
end
context "specifying several subjects" do
@@ -77,6 +85,22 @@
its(:body) { should == "Some comment" }
its(:post_title) { should == "Some post" }
its(:user_name) { should == "John Doe" }
+
+ it "responds to private comment method" do
+ expect(subject.private_methods).to include(:comment)
+ end
+
+ it "responds to private post method" do
+ expect(subject.private_methods).to include(:post)
+ end
+
+ it "returns comment subject" do
+ expect(subject.send(:comment)).to eql(comment)
+ end
+
+ it "returns post subject" do
+ expect(subject.send(:post)).to eql(post)
+ end
end
context "when subjects are nil" do
Please sign in to comment.
Something went wrong with that request. Please try again.