ryana-this 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (9) hide show
  1. data/Gemfile +4 -0
  2. data/LICENSE +22 -0
  3. data/README +60 -0
  4. data/Rakefile +14 -0
  5. data/VERSION +1 -0
  6. data/lib/this.rb +20 -0
  7. data/test/this_test.rb +89 -0
  8. data/this.gemspec +47 -0
  9. metadata +70 -0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'shoulda', '2.10.3', :group => :test
4
+ gem 'ruby-debug', '0.10.3', :group => :test
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2010 Ryan Angilly
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,60 @@
1
+ This
2
+
3
+ Ruby should be able to have fun with private writer methods. That is what This is.
4
+
5
+ @chadfowler posted this ruby quiz on twitter on Mar 31, 2009:
6
+
7
+ Ruby quiz: in 140 chrs or less, why doesn't this work:
8
+ class Y; def a; self.x end; private; def x; puts "hi" end end; Y.new.a
9
+ (http://twitter.com/chadfowler/status/11390678834)
10
+
11
+ With whitespace:
12
+
13
+ class Y
14
+ def a
15
+ self.x
16
+ end
17
+
18
+ private
19
+ def x
20
+ puts "hi"
21
+ end
22
+ end
23
+
24
+ Y.new.a
25
+
26
+ results in:
27
+
28
+ NoMethodError: private method `x' called for #<Y:0x10f6af8>
29
+ from (irb):1:in `a'
30
+ from (irb):1
31
+
32
+ You get a NoMethodError because you can't call a private method on self.
33
+ Ruby doesn't check the caller when sending messages to the 'self' receiver,
34
+ so privacy reigns.
35
+
36
+ With This, we get:
37
+
38
+ require 'this'
39
+ include This
40
+
41
+ class Y
42
+ def a
43
+ this.x
44
+ end
45
+
46
+ private
47
+ def x
48
+ puts "hi"
49
+ end
50
+ end
51
+
52
+ Y.new.a
53
+
54
+ hi
55
+ => nil
56
+
57
+ Ahhh...
58
+
59
+
60
+ Copyright 2010 Ryan Angilly
@@ -0,0 +1,14 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "ryana-this"
5
+ gemspec.summary = "Add Object#this to stuff for private writers"
6
+ gemspec.description = "Add Object#this to stuff for private writers"
7
+ gemspec.email = "ryan@angilly.com"
8
+ gemspec.homepage = "http://github.com/ryana/this"
9
+ gemspec.authors = ["Ryan Angilly"]
10
+ end
11
+ Jeweler::GemcutterTasks.new
12
+ rescue LoadError
13
+ puts "Jeweler not available. Install it with: gem install jeweler"
14
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,20 @@
1
+ module This
2
+ def self.everywhere!
3
+ Object.send :include, self
4
+ end
5
+
6
+ class NSThisProxy
7
+ def initialize(obj)
8
+ @obj = obj
9
+ end
10
+
11
+ def method_missing(name, *args, &block)
12
+ @obj.send(name, *args, &block)
13
+ end
14
+ end
15
+
16
+ private
17
+ def this
18
+ @__this_proxy ||= NSThisProxy.new(self)
19
+ end
20
+ end
@@ -0,0 +1,89 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'this')
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ Bundler.require :default, :test
5
+
6
+ require 'shoulda'
7
+ require 'ruby-debug'
8
+
9
+ class ThisTest < Test::Unit::TestCase
10
+
11
+ class Base
12
+ def run
13
+ this.set = 0
14
+ this.set += 1
15
+ end
16
+
17
+ def public_set
18
+ set
19
+ end
20
+
21
+ def public_set= val
22
+ set = val
23
+ end
24
+
25
+ attr_accessor :set
26
+ private :set, :set=
27
+ end
28
+
29
+ class WithThis < Base
30
+ include This
31
+ end
32
+
33
+ class WithoutThis < Base
34
+ end
35
+
36
+ context "with a WithThis" do
37
+ setup do
38
+ @with_this = WithThis.new
39
+ end
40
+
41
+ should "have a private method #this" do
42
+ assert_raises NoMethodError do
43
+ @with_this.this
44
+ end
45
+
46
+ assert @with_this.respond_to?(:this, true)
47
+ end
48
+
49
+ should "run" do
50
+ assert_equal nil, @with_this.public_set
51
+ @with_this.run
52
+ assert_equal 1, @with_this.public_set
53
+ end
54
+
55
+ should "do the wrong set" do
56
+ @with_this.run
57
+ assert_equal 1, @with_this.public_set
58
+ @with_this.public_set = 10
59
+ assert_equal 1, @with_this.public_set
60
+ end
61
+ end
62
+
63
+ context "with a WithoutThis" do
64
+ setup do
65
+ @without_this = WithoutThis.new
66
+ end
67
+
68
+ should "not have a private method this" do
69
+ assert !@without_this.respond_to?(:this, true)
70
+ end
71
+
72
+ should "not run" do
73
+ assert_raises NameError do
74
+ @without_this.run
75
+ end
76
+ end
77
+
78
+ context "with everywhere!" do
79
+ setup do
80
+ This.everywhere!
81
+ end
82
+
83
+ should "run" do
84
+ @without_this.run
85
+ end
86
+ end
87
+ end
88
+
89
+ end
@@ -0,0 +1,47 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{this}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ryan Angilly"]
12
+ s.date = %q{2010-05-05}
13
+ s.email = %q{ryan@angilly.com}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README"
17
+ ]
18
+ s.files = [
19
+ "Gemfile",
20
+ "LICENSE",
21
+ "README",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "lib/this.rb",
25
+ "test/this_test.rb",
26
+ "this.gemspec"
27
+ ]
28
+ s.homepage = %q{http://github.com/ryana/this}
29
+ s.rdoc_options = ["--charset=UTF-8"]
30
+ s.require_paths = ["lib"]
31
+ s.rubygems_version = %q{1.3.6}
32
+ s.summary = %q{Add Object#this to stuff for private writers}
33
+ s.test_files = [
34
+ "test/this_test.rb"
35
+ ]
36
+
37
+ if s.respond_to? :specification_version then
38
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
42
+ else
43
+ end
44
+ else
45
+ end
46
+ end
47
+
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ryana-this
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Ryan Angilly
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-05 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Add Object#this to stuff for private writers
22
+ email: ryan@angilly.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README
30
+ files:
31
+ - Gemfile
32
+ - LICENSE
33
+ - README
34
+ - Rakefile
35
+ - VERSION
36
+ - lib/this.rb
37
+ - test/this_test.rb
38
+ - this.gemspec
39
+ has_rdoc: true
40
+ homepage: http://github.com/ryana/this
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --charset=UTF-8
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.6
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Add Object#this to stuff for private writers
69
+ test_files:
70
+ - test/this_test.rb