Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add private_reader support

  • Loading branch information...
commit 2053d6bb4d04f65146fe679b38a24c88cca67c88 1 parent 67e5c62
@jimweirich authored
View
20 lib/rake/private_reader.rb
@@ -0,0 +1,20 @@
+module Rake
+
+ # Include PrivateReader to use +private_reader+.
+ module PrivateReader # :nodoc: all
+
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+
+ # Declare a list of private accessors
+ def private_reader(*names)
+ attr_reader(*names)
+ private(*names)
+ end
+ end
+
+ end
+end
View
7 lib/rake/thread_history_display.rb
@@ -1,8 +1,11 @@
+require 'rake/private_reader'
+
module Rake
class ThreadHistoryDisplay # :nodoc: all
- attr_reader :stats, :items, :threads
- private :stats, :items, :threads
+ include Rake::PrivateReader
+
+ private_reader :stats, :items, :threads
def initialize(stats)
@stats = stats
View
42 test/test_private_reader.rb
@@ -0,0 +1,42 @@
+require File.expand_path('../helper', __FILE__)
+require 'rake/private_reader'
+
+class TestPrivateAttrs < Rake::TestCase
+
+ class Sample
+ include Rake::PrivateReader
+
+ private_reader :reader, :a
+
+ def initialize
+ @reader = :RVALUE
+ end
+
+ def get_reader
+ reader
+ end
+
+ end
+
+ def setup
+ super
+ @sample = Sample.new
+ end
+
+ def test_private_reader_is_private
+ assert_private do @sample.reader end
+ assert_private do @sample.a end
+ end
+
+ def test_private_reader_returns_data
+ assert_equal :RVALUE, @sample.get_reader
+ end
+
+ private
+
+ def assert_private
+ ex = assert_raises(NoMethodError) do yield end
+ assert_match(/private/, ex.message)
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.