freighthopper 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README +0 -0
- data/Rakefile +29 -0
- data/VERSION +1 -0
- data/bin/convert_to_should_syntax +3 -0
- data/freighthopper.gemspec +75 -0
- data/init.rb +1 -0
- data/lib/freighthopper.rb +130 -0
- data/test/array_test.rb +48 -0
- data/test/define_and_alias_test.rb +48 -0
- data/test/float_test.rb +18 -0
- data/test/hash_test.rb +27 -0
- data/test/kernel_test.rb +34 -0
- data/test/lazy_alias_test.rb +34 -0
- data/test/object_test.rb +68 -0
- data/test/string_test.rb +58 -0
- data/test/test_helper.rb +17 -0
- data/vendor/gems/cache/shoulda-2.10.2.gem +0 -0
- data/vendor/gems/cache/test-rig-0.0.1.gem +0 -0
- metadata +88 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Jacob Rothstein
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "freighthopper"
|
8
|
+
gem.summary = %Q{Some extensions for riding the rails}
|
9
|
+
gem.description = %Q{More core ext}
|
10
|
+
gem.email = "github@jacobrothstein.com"
|
11
|
+
gem.homepage = "http://github.com/jbr/freighthopper"
|
12
|
+
gem.authors = ["Jacob Rothstein"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/*_test.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
task :test => :check_dependencies
|
28
|
+
|
29
|
+
task :default => :test
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,75 @@
|
|
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{freighthopper}
|
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 = ["Jacob Rothstein"]
|
12
|
+
s.date = %q{2010-02-03}
|
13
|
+
s.default_executable = %q{convert_to_should_syntax}
|
14
|
+
s.description = %q{More core ext}
|
15
|
+
s.email = %q{github@jacobrothstein.com}
|
16
|
+
s.executables = ["convert_to_should_syntax"]
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE",
|
19
|
+
"README"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".gitignore",
|
23
|
+
"Gemfile",
|
24
|
+
"LICENSE",
|
25
|
+
"README",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"bin/convert_to_should_syntax",
|
29
|
+
"freighthopper.gemspec",
|
30
|
+
"init.rb",
|
31
|
+
"lib/freighthopper.rb",
|
32
|
+
"pkg/.gitkeep",
|
33
|
+
"test/array_test.rb",
|
34
|
+
"test/define_and_alias_test.rb",
|
35
|
+
"test/float_test.rb",
|
36
|
+
"test/hash_test.rb",
|
37
|
+
"test/kernel_test.rb",
|
38
|
+
"test/lazy_alias_test.rb",
|
39
|
+
"test/object_test.rb",
|
40
|
+
"test/string_test.rb",
|
41
|
+
"test/test_helper.rb",
|
42
|
+
"vendor/gems/cache/shoulda-2.10.2.gem",
|
43
|
+
"vendor/gems/cache/test-rig-0.0.1.gem",
|
44
|
+
"vendor/gems/doc/.gitkeep",
|
45
|
+
"vendor/gems/gems/.gitkeep",
|
46
|
+
"vendor/gems/specifications/.gitkeep"
|
47
|
+
]
|
48
|
+
s.homepage = %q{http://github.com/jbr/freighthopper}
|
49
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
50
|
+
s.require_paths = ["lib"]
|
51
|
+
s.rubygems_version = %q{1.3.5}
|
52
|
+
s.summary = %q{Some extensions for riding the rails}
|
53
|
+
s.test_files = [
|
54
|
+
"test/array_test.rb",
|
55
|
+
"test/define_and_alias_test.rb",
|
56
|
+
"test/float_test.rb",
|
57
|
+
"test/hash_test.rb",
|
58
|
+
"test/kernel_test.rb",
|
59
|
+
"test/lazy_alias_test.rb",
|
60
|
+
"test/object_test.rb",
|
61
|
+
"test/string_test.rb",
|
62
|
+
"test/test_helper.rb"
|
63
|
+
]
|
64
|
+
|
65
|
+
if s.respond_to? :specification_version then
|
66
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
67
|
+
s.specification_version = 3
|
68
|
+
|
69
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
70
|
+
else
|
71
|
+
end
|
72
|
+
else
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'freighthopper'
|
@@ -0,0 +1,130 @@
|
|
1
|
+
class Module
|
2
|
+
def define_and_alias(target, feature, &blk)
|
3
|
+
aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
|
4
|
+
define_method :"#{aliased_target}_with_#{feature}#{punctuation}", &blk
|
5
|
+
alias_method_chain target, feature
|
6
|
+
end
|
7
|
+
|
8
|
+
def lazy_alias(to, from)
|
9
|
+
define_method(to){|*args| send from, *args}
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Array
|
14
|
+
def singular?() size == 1 end
|
15
|
+
def singular() singular?? first : nil end
|
16
|
+
def singular!() singular or raise "not singular" end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Object
|
20
|
+
def soft_send(method, *args)
|
21
|
+
send method, *args if respond_to? method
|
22
|
+
end
|
23
|
+
|
24
|
+
def or_if_blank(val = nil)
|
25
|
+
if soft_send :blank?
|
26
|
+
val || (block_given? ? yield : nil)
|
27
|
+
else
|
28
|
+
self
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def is_one_of?(*args)
|
33
|
+
args.flatten.any?{|klass| is_a? klass}
|
34
|
+
end
|
35
|
+
|
36
|
+
def eval_with_options(*args, &blk)
|
37
|
+
with_options *args do |map|
|
38
|
+
map.instance_eval(&blk)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class String
|
44
|
+
def strip(what = /\s/)
|
45
|
+
gsub /^#{what}*|#{what}*$/, ''
|
46
|
+
end
|
47
|
+
|
48
|
+
def /(num)
|
49
|
+
scan /.{1,#{(size / num.to_f).ceil}}/
|
50
|
+
end
|
51
|
+
|
52
|
+
def unindent(options = {})
|
53
|
+
tablength = options[:tablength] || 2
|
54
|
+
lines = gsub("\t", " " * tablength).split("\n")
|
55
|
+
|
56
|
+
whitespace = lines.map do |line|
|
57
|
+
line.match(/^(\s+)/).captures.first
|
58
|
+
end.min{ |l, r| l.length <=> r.length }
|
59
|
+
|
60
|
+
lines.map{ |l| l.gsub /^#{whitespace}/, ''}.join("\n")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
class Hash
|
65
|
+
def map_keys(key_hash)
|
66
|
+
inject({}) do |h, (k, v)|
|
67
|
+
h.merge((key_hash[k] || k) => v)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class Float
|
73
|
+
define_and_alias :to_s, :format do |*args|
|
74
|
+
format = args.first
|
75
|
+
return to_s_without_format if format.nil?
|
76
|
+
format = "$%0.2f" if format == :usd
|
77
|
+
(format.is_a?(Fixnum) ? "%0.#{format}f" : format) % self
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
require 'pp'
|
82
|
+
module Kernel
|
83
|
+
mattr_accessor :trace_output
|
84
|
+
%w(pp p puts).each do |method|
|
85
|
+
define_and_alias method, :source_and_passthrough do |*args|
|
86
|
+
puts_without_source_and_passthrough caller.first if Kernel.trace_output
|
87
|
+
send :"#{method}_without_source_and_passthrough", *args
|
88
|
+
puts_without_source_and_passthrough if Kernel.trace_output
|
89
|
+
return *args
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# Kernel.trace_output = true
|
95
|
+
|
96
|
+
if defined?(ActionMailer)
|
97
|
+
class ActionMailer::Base
|
98
|
+
class << self
|
99
|
+
define_and_alias :method_missing, :delayed_deliver do |method_name, *args|
|
100
|
+
if method_name.to_s =~ /^delayed_(deliver_[a-z_]+)$/
|
101
|
+
self.send_later $1, *args
|
102
|
+
else
|
103
|
+
method_missing_without_delayed_deliver(method_name, *args)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
if defined?(ActiveRecord)
|
111
|
+
module ActiveRecord
|
112
|
+
class Errors
|
113
|
+
def clear_on(attribute)
|
114
|
+
@errors.delete attribute.to_s
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
class Base
|
119
|
+
extend ActiveSupport::Memoizable
|
120
|
+
|
121
|
+
def cache_key
|
122
|
+
[self.class.to_s.underscore, to_param, soft_send(:cached_at).try(:to_i) || soft_send(:updated_at).try(:to_i)].compact.join("/")
|
123
|
+
end
|
124
|
+
|
125
|
+
def element_id
|
126
|
+
"#{self.class.to_s.underscore}_#{self.to_param}"
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
data/test/array_test.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.instance_eval { expand_path join(dirname(__FILE__), 'test_helper') }
|
2
|
+
require 'freighthopper'
|
3
|
+
|
4
|
+
class ArrayTest < Test::Unit::TestCase
|
5
|
+
context 'singular' do
|
6
|
+
should 'return the singular object if there is indeed only one' do
|
7
|
+
assert_singular 5, [5]
|
8
|
+
end
|
9
|
+
|
10
|
+
should 'return nil if the array is empty' do
|
11
|
+
assert_singular nil, []
|
12
|
+
end
|
13
|
+
|
14
|
+
should 'return nil if the array has > 1 item' do
|
15
|
+
assert_singular nil, [1, 2]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'singular questionmark' do
|
20
|
+
should 'return true if the array has only one member' do
|
21
|
+
assert_singular [5]
|
22
|
+
end
|
23
|
+
|
24
|
+
should 'return false if the array is empty' do
|
25
|
+
assert_not_singular []
|
26
|
+
end
|
27
|
+
|
28
|
+
should 'return false if the array has more than one member' do
|
29
|
+
assert_not_singular [1, 2]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'singular bang' do
|
34
|
+
should 'return the item if there is just one' do
|
35
|
+
assert_equal 5, [5].singular!
|
36
|
+
end
|
37
|
+
|
38
|
+
should 'raise if there are no items' do
|
39
|
+
e = assert_raise(RuntimeError){ [].singular! }
|
40
|
+
assert_message 'not singular', e
|
41
|
+
end
|
42
|
+
|
43
|
+
should 'raise if there are more than one item' do
|
44
|
+
e = assert_raise(RuntimeError){ [1, 2].singular! }
|
45
|
+
assert_message 'not singular', e
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.instance_eval { expand_path join(dirname(__FILE__), 'test_helper') }
|
2
|
+
require 'freighthopper'
|
3
|
+
|
4
|
+
class DefineAndAliasTest < Test::Unit::TestCase
|
5
|
+
class SomeClass
|
6
|
+
attr_reader :cache
|
7
|
+
def foo() 5 end
|
8
|
+
|
9
|
+
define_and_alias(:foo, :bar) {foo_without_bar + 5}
|
10
|
+
|
11
|
+
def even?(number) number % 2 == 0 end
|
12
|
+
|
13
|
+
define_and_alias :even?, :caching do |number|
|
14
|
+
(@cache ||= {})[number] ||= even_without_caching?(number)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'with a SomeClass' do
|
19
|
+
setup {@object = SomeClass.new}
|
20
|
+
|
21
|
+
should 'respond to foo_with_bar' do
|
22
|
+
assert @object.respond_to?('foo_with_bar')
|
23
|
+
end
|
24
|
+
|
25
|
+
should 'respond to foo_without_bar' do
|
26
|
+
assert @object.respond_to?('foo_without_bar')
|
27
|
+
end
|
28
|
+
|
29
|
+
should 'return 5 for foo_without_bar' do
|
30
|
+
assert_foo_without_bar 5, @object
|
31
|
+
end
|
32
|
+
|
33
|
+
should 'return 10 for foo' do
|
34
|
+
assert_foo 10, @object
|
35
|
+
end
|
36
|
+
|
37
|
+
should 'handle questionmark methods' do
|
38
|
+
%w(even? even_without_caching? even_with_caching?).each do |method|
|
39
|
+
assert @object.respond_to?(method)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
should 'pass arguments' do
|
44
|
+
assert @object.even?(4)
|
45
|
+
assert_length 1, @object.cache
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/test/float_test.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.instance_eval { expand_path join(dirname(__FILE__), 'test_helper') }
|
2
|
+
require 'freighthopper'
|
3
|
+
|
4
|
+
class FloatTest < Test::Unit::TestCase
|
5
|
+
context 'to_s with format' do
|
6
|
+
should 'work as before' do
|
7
|
+
assert_to_s '1.1', 1.1
|
8
|
+
end
|
9
|
+
|
10
|
+
should 'allow specification of a format string' do
|
11
|
+
assert_equal "1.2", 1.23.to_s("%.1f")
|
12
|
+
end
|
13
|
+
|
14
|
+
should 'allow specification of a precision' do
|
15
|
+
assert_equal '1.2', 1.23.to_s(1)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/test/hash_test.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.instance_eval { expand_path join(dirname(__FILE__), 'test_helper') }
|
2
|
+
require 'freighthopper'
|
3
|
+
|
4
|
+
class HashTest < Test::Unit::TestCase
|
5
|
+
context 'map_keys' do
|
6
|
+
should 'replace keys with their mapped replacement' do
|
7
|
+
base = {:hello => :world}
|
8
|
+
expected = {:howdy => :world}
|
9
|
+
assert_equal expected, base.map_keys(:hello => :howdy)
|
10
|
+
end
|
11
|
+
|
12
|
+
should 'not modify keys that are not specified' do
|
13
|
+
base = {:a => 1, :b => 2}
|
14
|
+
mapping = {:a => :hello}
|
15
|
+
expected = {:hello => 1, :b => 2}
|
16
|
+
|
17
|
+
assert_equal expected, base.map_keys(mapping)
|
18
|
+
end
|
19
|
+
|
20
|
+
should 'ignore mappings that do not exist' do
|
21
|
+
base = {:a => 1, :b => 2}
|
22
|
+
mapping = {:q => :r}
|
23
|
+
|
24
|
+
assert_equal base, base.map_keys(mapping)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/test/kernel_test.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.instance_eval { expand_path join(dirname(__FILE__), 'test_helper') }
|
2
|
+
require 'freighthopper'
|
3
|
+
|
4
|
+
class KernelTest < Test::Unit::TestCase
|
5
|
+
context 'trace output' do
|
6
|
+
context 'without Kernel trace_output' do
|
7
|
+
should 'function as normal' do
|
8
|
+
output = capture(false) { puts 'hello' }
|
9
|
+
assert_equal "hello\n", output
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'with Kernel trace_output' do
|
14
|
+
should 'include the line that printed' do
|
15
|
+
output = capture(true) { puts 'hello' }
|
16
|
+
assert_equal "#{__FILE__}:15:in `puts'\nhello\n\n", output
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def capture(trace_output, &block)
|
23
|
+
Kernel.trace_output = trace_output
|
24
|
+
original_stdout = $stdout
|
25
|
+
$stdout = fake = StringIO.new
|
26
|
+
begin
|
27
|
+
yield
|
28
|
+
ensure
|
29
|
+
$stdout = original_stdout
|
30
|
+
end
|
31
|
+
Kernel.trace_output = false
|
32
|
+
fake.string
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.instance_eval { expand_path join(dirname(__FILE__), 'test_helper') }
|
2
|
+
require 'freighthopper'
|
3
|
+
|
4
|
+
class DefineAndAliasTest < Test::Unit::TestCase
|
5
|
+
class SomeClass
|
6
|
+
attr_reader :last_method_missing, :last_args
|
7
|
+
lazy_alias :this_method_is_called, :this_hits_method_missing
|
8
|
+
|
9
|
+
def method_missing(*args)
|
10
|
+
@last_method_missing = args.shift
|
11
|
+
@last_args = args
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'with a SomeClass' do
|
16
|
+
setup {@object = SomeClass.new}
|
17
|
+
|
18
|
+
should 'respond to this_method_is_called' do
|
19
|
+
assert @object.respond_to?(:this_method_is_called)
|
20
|
+
end
|
21
|
+
|
22
|
+
context "after calling this_method_is_called" do
|
23
|
+
setup {@object.this_method_is_called :arg1, :arg2}
|
24
|
+
|
25
|
+
should 'call whatever method is the second argument' do
|
26
|
+
assert_last_method_missing :this_hits_method_missing, @object
|
27
|
+
end
|
28
|
+
|
29
|
+
should 'pass through args' do
|
30
|
+
assert_last_args [:arg1, :arg2], @object
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/test/object_test.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require File.instance_eval { expand_path join(dirname(__FILE__), 'test_helper') }
|
2
|
+
require 'freighthopper'
|
3
|
+
|
4
|
+
class ObjectTest < Test::Unit::TestCase
|
5
|
+
context 'soft_send' do
|
6
|
+
should 'send the method if the object responds to it' do
|
7
|
+
assert_equal 1, [1].soft_send(:first)
|
8
|
+
end
|
9
|
+
|
10
|
+
should 'return nil if the object does not' do
|
11
|
+
assert_nil 5.soft_send(:hello)
|
12
|
+
end
|
13
|
+
|
14
|
+
should 'pass args' do
|
15
|
+
assert_equal 10, 5.soft_send(:*, 2)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'eval_with_options' do
|
20
|
+
setup do
|
21
|
+
def foo(arg1, options = {})
|
22
|
+
[arg1, options]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
should 'pass options to any method called in the block' do
|
27
|
+
returned = nil
|
28
|
+
|
29
|
+
eval_with_options :wibble => :baz do
|
30
|
+
returned = foo(:squee)
|
31
|
+
end
|
32
|
+
|
33
|
+
assert_equal [:squee, {:wibble => :baz}], returned
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'or_if_blank' do
|
38
|
+
should 'return nil if there is no block and the argument is nil' do
|
39
|
+
assert_nil "".or_if_blank(nil)
|
40
|
+
end
|
41
|
+
|
42
|
+
should 'return the callee if it is not blank' do
|
43
|
+
assert_equal "hello", "hello".or_if_blank(5)
|
44
|
+
end
|
45
|
+
|
46
|
+
should 'return the first arg if the callee is blank' do
|
47
|
+
assert_equal 5, "".or_if_blank(5)
|
48
|
+
end
|
49
|
+
|
50
|
+
should 'accept a block' do
|
51
|
+
assert_equal 5, "".or_if_blank{5}
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'is_one_of?' do
|
56
|
+
should 'check if the object is one of the arguments' do
|
57
|
+
assert "hello".is_one_of?(Fixnum, Symbol, Proc, String)
|
58
|
+
end
|
59
|
+
|
60
|
+
should 'work with an array as well' do
|
61
|
+
assert :symbol.is_one_of?([Fixnum, Symbol])
|
62
|
+
end
|
63
|
+
|
64
|
+
should 'return false if there is no match' do
|
65
|
+
assert_not :symbol.is_one_of?(Proc, String)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/test/string_test.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require File.instance_eval { expand_path join(dirname(__FILE__), 'test_helper') }
|
2
|
+
require 'freighthopper'
|
3
|
+
|
4
|
+
class StringTest < Test::Unit::TestCase
|
5
|
+
context 'strip' do
|
6
|
+
should 'still allow normal strip' do
|
7
|
+
assert_strip "hello", " hello \t\n"
|
8
|
+
end
|
9
|
+
|
10
|
+
should 'also allow a string' do
|
11
|
+
assert_equal "hello", "---hello---".strip("-")
|
12
|
+
end
|
13
|
+
|
14
|
+
should 'also allow a regular expression' do
|
15
|
+
assert_equal "hello", "123hello456".strip(/[0-9]/)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'division operator' do
|
20
|
+
context 'with an even division' do
|
21
|
+
should 'split the string into that many even strings' do
|
22
|
+
divided = ('-' * 10) / 2
|
23
|
+
assert_size 2, divided
|
24
|
+
assert_equal [5, 5], divided.map{|s| s.length}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'with an uneven division' do
|
29
|
+
should 'take the difference out of the last one' do
|
30
|
+
divided = ('-' * 10) / 3
|
31
|
+
assert_size 3, divided
|
32
|
+
assert_equal [4,4,2], divided.map(&:length)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'unindent' do
|
38
|
+
should 'remove the least common leading whitespace' do
|
39
|
+
unindented = (<<-END).unindent
|
40
|
+
a
|
41
|
+
b
|
42
|
+
c
|
43
|
+
END
|
44
|
+
|
45
|
+
assert_equal "a\n b\n c", unindented
|
46
|
+
end
|
47
|
+
|
48
|
+
should 'replace tabs with tablength' do
|
49
|
+
unindented = "\ttest\n\t\ttest".unindent :tablength => 4
|
50
|
+
assert_equal "test\n test", unindented
|
51
|
+
end
|
52
|
+
|
53
|
+
should 'replace tabs with tablength (defaulting to 2)' do
|
54
|
+
unindented = "\ttest\n\t\ttest".unindent
|
55
|
+
assert_equal "test\n test", unindented
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
begin
|
2
|
+
require File.instance_eval { expand_path join(dirname(__FILE__), '..', 'vendor', 'gems', 'environment')}
|
3
|
+
rescue LoadError
|
4
|
+
puts "Bundling Gems\n\nHang in there, this only has to happen once...\n\n"
|
5
|
+
system 'gem bundle'
|
6
|
+
retry
|
7
|
+
end
|
8
|
+
|
9
|
+
Bundler.require_env :test
|
10
|
+
|
11
|
+
$:.unshift File.instance_eval { expand_path join(dirname(__FILE__), "..", "lib") }
|
12
|
+
require 'test/unit'
|
13
|
+
|
14
|
+
|
15
|
+
class Test::Unit::TestCase
|
16
|
+
include TestRig
|
17
|
+
end
|
Binary file
|
Binary file
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: freighthopper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jacob Rothstein
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-02-03 00:00:00 -08:00
|
13
|
+
default_executable: convert_to_should_syntax
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: More core ext
|
17
|
+
email: github@jacobrothstein.com
|
18
|
+
executables:
|
19
|
+
- convert_to_should_syntax
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- Gemfile
|
28
|
+
- LICENSE
|
29
|
+
- README
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- bin/convert_to_should_syntax
|
33
|
+
- freighthopper.gemspec
|
34
|
+
- init.rb
|
35
|
+
- lib/freighthopper.rb
|
36
|
+
- pkg/.gitkeep
|
37
|
+
- test/array_test.rb
|
38
|
+
- test/define_and_alias_test.rb
|
39
|
+
- test/float_test.rb
|
40
|
+
- test/hash_test.rb
|
41
|
+
- test/kernel_test.rb
|
42
|
+
- test/lazy_alias_test.rb
|
43
|
+
- test/object_test.rb
|
44
|
+
- test/string_test.rb
|
45
|
+
- test/test_helper.rb
|
46
|
+
- vendor/gems/cache/shoulda-2.10.2.gem
|
47
|
+
- vendor/gems/cache/test-rig-0.0.1.gem
|
48
|
+
- vendor/gems/doc/.gitkeep
|
49
|
+
- vendor/gems/gems/.gitkeep
|
50
|
+
- vendor/gems/specifications/.gitkeep
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/jbr/freighthopper
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options:
|
57
|
+
- --charset=UTF-8
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.3.5
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Some extensions for riding the rails
|
79
|
+
test_files:
|
80
|
+
- test/array_test.rb
|
81
|
+
- test/define_and_alias_test.rb
|
82
|
+
- test/float_test.rb
|
83
|
+
- test/hash_test.rb
|
84
|
+
- test/kernel_test.rb
|
85
|
+
- test/lazy_alias_test.rb
|
86
|
+
- test/object_test.rb
|
87
|
+
- test/string_test.rb
|
88
|
+
- test/test_helper.rb
|