freighthopper 0.1.9 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,142 @@
1
1
  h1. Freighthopper
2
2
 
3
- h2. Some monkeypatches for riding the Rails
3
+ h2. Some monkeypatches for riding the Rails
4
+
5
+ h3. Antonym Accessor
6
+
7
+ 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.
8
+
9
+ <pre><code>class Example
10
+ def private?() @private end
11
+ def private=(p) @private = p end
12
+ antonym_accessor :private => :public
13
+ end
14
+
15
+ e = Example.new
16
+ e.private = true
17
+ e.public? #=> false
18
+ e.public = true
19
+ e.private? #=> false
20
+ </code></pre>
21
+
22
+ h3. Define and alias
23
+
24
+ The @alias_method_chain@ pattern requires you to def the method and then separately alias_method_chain it. This pattern is not as dry as it could be. Hence, @define_and_alias@.
25
+
26
+ <pre><code>
27
+ # What would previously have been:
28
+ def foo_with_something(arg)
29
+ foo_without_something arg
30
+ 5.times {puts arg}
31
+ end
32
+ alias_method_chain :foo, :something
33
+
34
+ # Becomes:
35
+ define_and_alias :foo, :something do |arg|
36
+ foo_without_something arg
37
+ 5.times {puts arg}
38
+ end
39
+ </code></pre>
40
+
41
+ One less line, and more dry.
42
+
43
+ h3. Eval with options
44
+
45
+ This is a slight tweak to @with_options@. Here's the transformation in code:
46
+
47
+ <pre><code># What was:
48
+ map.with_options :name => 'freighthopper' do |fh|
49
+ fh.something
50
+ fh.that
51
+ end
52
+
53
+ # Becomes
54
+
55
+ map.eval_with_options :name => 'freighthopper' do
56
+ something
57
+ that
58
+ end
59
+ </code></pre>
60
+
61
+ h3. Is one of
62
+
63
+ <pre><code># What was
64
+ if object.is_a?(Foo) || object.is_a?(Bar) || object.is_a?(Bibble)
65
+ ...
66
+ end
67
+
68
+ #or maybe it was
69
+
70
+ case object
71
+ when Foo, Bar, Bibble
72
+ ...
73
+ end
74
+
75
+ #becomes
76
+ if object.is_one_of? Foo, Bar, Bibble
77
+ ...
78
+ end
79
+ </code></pre>
80
+
81
+ h3. Lazy alias
82
+
83
+ <pre><code># What was:
84
+ def to_s() name end
85
+ #this is not the same as alias_method, because if #name
86
+ #is defined through method missing / lazily, it won't
87
+ #work. Lazy alias is like alias_method, lazy-style
88
+
89
+ lazy_alias :to_s, :name
90
+ </code></pre>
91
+
92
+ h3. Or if blank
93
+
94
+ There are lots of implementations of this, but this is mine. If the object has a blank method, it is called. @or_if_blank@ can be called with an normal argument or a block.
95
+
96
+ <pre><code>"".or_if_blank(:something_else) #=> :something_else
97
+
98
+ [].or_if_blank { something_expensive_that_returns("hello") } #=> "hello"
99
+
100
+ "not blank".or_if_blank(42) #=> "not blank"
101
+ </code></pre>
102
+
103
+ h3. Soft send
104
+
105
+ This is like try. It sends the method if the object responsds to the method, otherwise it returns nil. Use like send.
106
+
107
+ h3. Stdout extensions
108
+
109
+ It bothers me that @p@, @puts@, @print@, and @pp@ all return @nil@. This patch makes them return their arguments, which allows you to wrap anything with @p(...)@ and it'll still work.
110
+
111
+ <pre><code>
112
+ 5 + p(5) #=> 10
113
+ # 5</code></pre>
114
+
115
+ Additionally, sometimes there's a puts left somewhere in your code and you'd really like to know where that stdout noise is coming from. Enter @Kernel.trace_output@:
116
+
117
+ <pre><code>Kernel.trace_output = true
118
+ puts "hello"
119
+ #test.rb:4:in `puts'
120
+ #hello
121
+ </code></pre>
122
+
123
+ h3. String extensions
124
+
125
+ TODO: documentation
126
+
127
+ h3. Hash extensions
128
+
129
+ TODO: documention
130
+
131
+ h3. Float extension
132
+
133
+ TODO: documentation
134
+
135
+ h3. Array extensions
136
+
137
+ TODO: documentation
138
+
139
+ h3. ActiveRecord extensions
140
+
141
+ TODO: documentation
142
+
@@ -2,5 +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
6
  }.each {|lib| require "freighthopper/#{lib}"}
6
7
 
@@ -1,3 +1,5 @@
1
+ require 'active_support'
2
+
1
3
  class Module
2
4
  def define_and_alias(target, feature, &blk)
3
5
  aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
@@ -0,0 +1,5 @@
1
+ class Fixnum
2
+ def of(&blk)
3
+ (0...self).map &blk
4
+ end
5
+ end
@@ -1,3 +1,5 @@
1
+ require 'freighthopper/soft_send'
2
+
1
3
  class Object
2
4
  def or_if_blank(val = nil)
3
5
  if soft_send :blank?
@@ -14,7 +14,7 @@ class HashForm < TestClass
14
14
  antonym_accessor :private => :public
15
15
  end
16
16
 
17
- class ArrayTest < Test::Unit::TestCase
17
+ class AntonymAccessorTest < Test::Unit::TestCase
18
18
  def self.should_act_like_antonym_accessor
19
19
  should 'define a public= method' do
20
20
  @instance.public = true
@@ -0,0 +1,14 @@
1
+ require File.expand_path("../test_helper", __FILE__)
2
+ require 'freighthopper/fixnum'
3
+
4
+ class FixnumTest < Test::Unit::TestCase
5
+ context 'of' do
6
+ should 'give me five of what the block returns' do
7
+ assert_equal %w(hello hello), 2.of { "hello" }
8
+ end
9
+
10
+ should 'yield the index' do
11
+ assert_equal %w( 0 1 2 3 ), 4.of {|i| i.to_s }
12
+ end
13
+ end
14
+ 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: 9
4
+ hash: 13
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 9
10
- version: 0.1.9
9
+ - 11
10
+ version: 0.1.11
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-06-24 00:00:00 -07:00
18
+ date: 2010-07-07 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -41,6 +41,7 @@ files:
41
41
  - lib/freighthopper/array/symbols.rb
42
42
  - lib/freighthopper/define_and_alias.rb
43
43
  - lib/freighthopper/eval_with_options.rb
44
+ - lib/freighthopper/fixnum.rb
44
45
  - lib/freighthopper/float/to_s.rb
45
46
  - lib/freighthopper/hash/map_keys.rb
46
47
  - lib/freighthopper/is_one_of.rb
@@ -57,6 +58,7 @@ files:
57
58
  - test/antonym_accessor_test.rb
58
59
  - test/array_test.rb
59
60
  - test/define_and_alias_test.rb
61
+ - test/fixnum_test.rb
60
62
  - test/float_test.rb
61
63
  - test/hash_test.rb
62
64
  - test/kernel_test.rb
@@ -102,6 +104,7 @@ test_files:
102
104
  - test/antonym_accessor_test.rb
103
105
  - test/array_test.rb
104
106
  - test/define_and_alias_test.rb
107
+ - test/fixnum_test.rb
105
108
  - test/float_test.rb
106
109
  - test/hash_test.rb
107
110
  - test/kernel_test.rb