freighthopper 0.1.11 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,25 @@ h1. Freighthopper
2
2
 
3
3
  h2. Some monkeypatches for riding the Rails
4
4
 
5
+ h3. Eigenclass & define_eigenmethod
6
+
7
+ Quick access to the eigenclass.
8
+
9
+ <pre><code>class Example
10
+ def private?() false end
11
+ end
12
+
13
+ e = Example.new
14
+ e.define_eigenmethod(:private?) {true}
15
+ e.private? #=> true
16
+ e.eigenclass #=> #<Class:#<Example:0x18b684c>>
17
+ e.eigenclass.instance_methods.include? :private? #=> true
18
+ e.eigenmethod :private? #=> #<UnboundMethod: Example#private?>
19
+
20
+ Example.new.private? #=> false
21
+
22
+ </code></pre>
23
+
5
24
  h3. Antonym Accessor
6
25
 
7
26
  Antonym accessor provides some simple ruby sugar for defining inverse relationships, like between private and public, enabled and disabled, on and off, etc. I found myself defining these often enough I figured I'd extract the functionality.
@@ -120,6 +139,12 @@ puts "hello"
120
139
  #hello
121
140
  </code></pre>
122
141
 
142
+ h3. Fixnum extensions
143
+
144
+ <pre><code>5.of {rand}</code></pre>
145
+
146
+ This gives you five random numbers. It's like @Fixnum.times@, but maps the return values instead of returning the number.
147
+
123
148
  h3. String extensions
124
149
 
125
150
  TODO: documentation
@@ -2,6 +2,6 @@
2
2
  activerecord array float/to_s hash/map_keys string
3
3
  antonym_accessor array define_and_alias eval_with_options
4
4
  is_one_of lazy_alias or_if_blank soft_send stdout_extensions
5
- fixnum
5
+ fixnum eigenclass
6
6
  }.each {|lib| require "freighthopper/#{lib}"}
7
7
 
@@ -0,0 +1,15 @@
1
+ class Object
2
+ def eigenclass
3
+ class << self
4
+ self
5
+ end
6
+ end
7
+
8
+ def define_eigenmethod(method_name, &blk)
9
+ eigenclass.send :define_method, method_name, &blk
10
+ end
11
+
12
+ def eigenmethod(method_name)
13
+ eigenclass.instance_method(method_name)
14
+ end
15
+ end
@@ -0,0 +1,39 @@
1
+ require File.expand_path("../test_helper", __FILE__)
2
+ require 'freighthopper/eigenclass'
3
+
4
+ class AntonymAccessorTest < Test::Unit::TestCase
5
+ context 'defining a method on the eigenclass' do
6
+ setup do
7
+ @instance = "test"
8
+ @instance.eigenclass.send(:define_method, :reverse) { "reversed" }
9
+ end
10
+
11
+ should 'define the method on just that instance' do
12
+ assert_reverse "reversed", @instance
13
+ end
14
+
15
+ should 'not define the method on any other instance' do
16
+ assert_reverse "tset", "test"
17
+ end
18
+ end
19
+
20
+ context 'the define_eigenmethod shortcut' do
21
+ setup do
22
+ @instance = "test"
23
+ @instance.define_eigenmethod(:reverse) {"reversed"}
24
+ end
25
+
26
+ should 'define the method on that instance' do
27
+ assert_reverse "reversed", @instance
28
+ end
29
+
30
+ should 'not define the method on any other instance' do
31
+ assert_reverse "tset", "test"
32
+ end
33
+
34
+ should 'be accessible with eigenmethod' do
35
+ assert_equal @instance.eigenclass.instance_method(:reverse),
36
+ @instance.eigenmethod(:reverse)
37
+ end
38
+ end
39
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: freighthopper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 3
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 11
10
- version: 0.1.11
9
+ - 12
10
+ version: 0.1.12
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jacob Rothstein
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-07 00:00:00 -07:00
18
+ date: 2010-07-28 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -40,6 +40,7 @@ files:
40
40
  - lib/freighthopper/array/singular.rb
41
41
  - lib/freighthopper/array/symbols.rb
42
42
  - lib/freighthopper/define_and_alias.rb
43
+ - lib/freighthopper/eigenclass.rb
43
44
  - lib/freighthopper/eval_with_options.rb
44
45
  - lib/freighthopper/fixnum.rb
45
46
  - lib/freighthopper/float/to_s.rb
@@ -58,6 +59,7 @@ files:
58
59
  - test/antonym_accessor_test.rb
59
60
  - test/array_test.rb
60
61
  - test/define_and_alias_test.rb
62
+ - test/eigenclass_test.rb
61
63
  - test/fixnum_test.rb
62
64
  - test/float_test.rb
63
65
  - test/hash_test.rb
@@ -104,6 +106,7 @@ test_files:
104
106
  - test/antonym_accessor_test.rb
105
107
  - test/array_test.rb
106
108
  - test/define_and_alias_test.rb
109
+ - test/eigenclass_test.rb
107
110
  - test/fixnum_test.rb
108
111
  - test/float_test.rb
109
112
  - test/hash_test.rb