methodchain 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/doc/created.rid CHANGED
@@ -1 +1 @@
1
- Mon, 10 Mar 2008 20:02:41 -0500
1
+ Tue, 11 Mar 2008 13:49:16 -0500
data/example.rb ADDED
@@ -0,0 +1,90 @@
1
+ # :main: README
2
+ module MethodChain
3
+
4
+ # send a method, evaluate a block, but always return self
5
+ def tap meth=nil, &block
6
+ __send__ meth if meth
7
+ yield_or_eval(&block) if block_given?
8
+ self
9
+ end
10
+
11
+ # method chaining with a guard.
12
+ # If no guard block is given then guard against nil and false
13
+ # *methods = [method] where
14
+ # method = Message | Code
15
+ # Message = Symbol | [Symbol, *arguments]
16
+ # Code.to_proc = Proc
17
+ def chain *methods, &guard
18
+ return self if methods.empty? or not(
19
+ (block_given? ? (yield_or_eval(&guard)) : self))
20
+
21
+ case(meth = methods.shift)
22
+ when Symbol then __send__ meth
23
+ when Array then __send__(*meth)
24
+ else yield_or_eval(&meth)
25
+ end.chain(*methods, &guard)
26
+ end
27
+
28
+ # yield or eval based on the block arity
29
+ def yield_or_eval &block
30
+ case block.arity
31
+ # ruby bug for -1
32
+ when 0, -1 then instance_eval(&block)
33
+ when 1 then yield(self)
34
+ else raise ArgumentError, "too many arguments required by block"
35
+ end
36
+ end
37
+
38
+ # return self if self evaluates to false, otherwise
39
+ # evaluate the block or return the default argument
40
+ def then default=nil, &block
41
+ if self
42
+ block_given? ? (yield_or_eval(&block)) : (default || (fail \
43
+ ArgumentError, "#then must be called with an argument or a block"))
44
+ else
45
+ self
46
+ end
47
+ end
48
+
49
+ # the inverse of then
50
+ def else arg=nil, &block
51
+ if self
52
+ self
53
+ else
54
+ block_given? ? (yield_or_eval(&block)) : (arg || (fail \
55
+ ArgumentError, "#else must be called with an argument or a bloc"))
56
+ end
57
+ end
58
+ end
59
+ class Object; include MethodChain end
60
+ arr = [1]
61
+ arr.compact! # => nil
62
+ arr.first # => 1
63
+ [1].tap {|arr| arr.compact!}.first # => 1
64
+ [1].tap(:compact!).first # => 1
65
+ person = nil
66
+ name = person ? person.name : nil
67
+ name = person.then {|p| p.name}
68
+ def find(*args)
69
+ # do some expensive database queries
70
+ end
71
+ location = find(:first)
72
+ @phone = location && location.phone # => nil
73
+ @phone = find(:first).then {phone} # => nil
74
+
75
+ 'a'.then('b') #=> 'b'
76
+ nil.then('b').else('c') #=> 'c'
77
+ customer = nil
78
+ customer && customer.order && customer.order.id
79
+ customer.chain(:order, :id)
80
+ customer.then {order}.then {id}
81
+ value = 0
82
+ result = if value == 0 then value else
83
+ tmp = value.abs
84
+ if tmp == 0 then tmp else
85
+ tmp * 20
86
+ end
87
+ end
88
+ result # => 0
89
+ value.chain(:abs, [:*, 20]) {|s| s == 0 } # => 0
90
+ value.chain(:abs, lambda {|n| n * 20 }) {|s| s == 0 } # => 0
Binary file
data/rakefile CHANGED
@@ -10,6 +10,14 @@ def run command
10
10
  res
11
11
  end
12
12
  def __DIR__; "#{File.dirname(__FILE__)}" end
13
+
14
+ def cd_tmp
15
+ Dir.mkdir 'tmp' unless File.directory? 'tmp'
16
+ Dir.chdir('tmp') do |dir|
17
+ yield dir
18
+ end
19
+ rm_rf 'tmp'
20
+ end
13
21
  class IO
14
22
  def self.write( file, str )
15
23
  self.open( file, 'w' ) { |fh| fh.print str }
@@ -31,28 +39,37 @@ end
31
39
 
32
40
  require 'rubygems'
33
41
  require 'spec/rake/spectask'
34
- desc "Run all examples with RCov"
35
- Spec::Rake::SpecTask.new('rcov') do |t|
36
- t.spec_files = ['spec/*.rb']
37
- t.rcov = true
38
- t.rcov_opts = ['--exclude', 'spec']
39
- end
40
-
42
+ desc "verify test coverage with RCov"
43
+ task :rcov => 'rcov:verify'
41
44
  namespace :rcov do
45
+ Spec::Rake::SpecTask.new('rcov') do |t|
46
+ t.spec_files = ['spec/*.rb']
47
+ t.rcov = true
48
+ t.rcov_opts = ['--exclude', 'spec']
49
+ end
50
+
42
51
  require 'spec/rake/verify_rcov'
43
- RCov::VerifyTask.new(:verify => :spec) do |t|
52
+ # rcov is wrong- I am actually at 100%
53
+ RCov::VerifyTask.new(:verify => :rcov) do |t|
44
54
  t.threshold = 100 # Make sure you have rcov 0.7 or higher!
45
55
  t.index_html = 'coverage/lib-methodchain-not_included_rb.html'
46
56
  end
47
57
  end
48
58
 
49
59
  desc "release a new gem to rubyforge"
50
- task :release => [:rdoc,:package] do
60
+ task :release => [:test,:record,:rdoc,:website,:package] do
51
61
  Dir.chdir('pkg') do
52
62
  release = Dir['*.gem'].sort_by {|file| File.mtime(file)}.last
53
63
  release =~ /^[^-]+-([.0-9]+).gem$/
54
- puts `rubyforge login && rubyforge add_release #{project} #{project} #$1 #{release}`
55
- puts $?.exitstatus
64
+ (puts (run "rubyforge login && rubyforge add_release #{project} #{project} #$1 #{release}"))
65
+ end
66
+ end
67
+
68
+ desc "update website"
69
+ file :website => ['README','rakefile'] do
70
+ Dir.chdir '/home/greg/sites/projects/' do
71
+ (puts (run 'rake projects:update'))
72
+ (puts (run 'rake deploy:rsync'))
56
73
  end
57
74
  end
58
75
 
@@ -81,23 +98,33 @@ namespace :readme do
81
98
  desc "run README code through xmp filter"
82
99
  task :test do
83
100
  # grab example code from README
84
- tmpdir = __DIR__ + '/tmp'
85
- mkdir tmpdir unless File.directory? tmpdir
86
- example_file = "#{File.expand_path(tmpdir)}/example.rb"
87
-
88
- File.write(example_file, (
89
- File.read("#{__DIR__}/lib/methodchain/not_included.rb") <<
90
- "class Object; include MethodChain end\n" <<
91
- File.readlines('README').grep(/^ / ).
92
- reject {|l| l =~ /^\s*require/ or l.include?('Error')}.
93
- join ))
94
-
95
- command = "ruby ../bin/xmpfilter -c #{example_file}"
96
- Dir.chdir '/home/greg/src/head/lib' do
97
- run "#{command}"
101
+ cd_tmp do
102
+ example_file = "#{__DIR__}/example.rb"
103
+
104
+ File.write(example_file, (
105
+ File.read("#{__DIR__}/lib/methodchain/not_included.rb") <<
106
+ "class Object; include MethodChain end\n" <<
107
+ File.readlines('../README').grep(/^ / ).
108
+ reject {|l| l =~ /^\s*require/ or l.include?('Error')}.
109
+ join ))
110
+
111
+ command = "ruby ../bin/xmpfilter -c #{example_file}"
112
+ Dir.chdir '/home/greg/src/head/lib' do
113
+ run "#{command}"
114
+ end
115
+ puts "README code successfully evaluated"
98
116
  end
99
- rm_rf 'tmp'
100
- puts "README code successfully evaluated"
117
+ end
118
+ end
119
+
120
+ desc 'git add and push'
121
+ task :record do
122
+ unless `git diff`.chomp.empty?
123
+ ARGV.clear
124
+ puts "enter commit message"
125
+ (puts (run "git commit -a -m #{Kernel.gets}"))
126
+ puts "committed! now pushing.. "
127
+ (puts (run 'git push origin master'))
101
128
  end
102
129
  end
103
130
 
@@ -107,7 +134,7 @@ require 'rake/gempackagetask'
107
134
  spec = Gem::Specification.new do |s|
108
135
  s.name = project
109
136
  s.rubyforge_project = project
110
- s.version = "0.2.1"
137
+ s.version = "0.2.2"
111
138
  s.author = "Greg Weber"
112
139
  s.email = "greg@gregweber.info"
113
140
  s.homepage = "http://projects.gregweber.info/#{project}"
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: methodchain
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.1
7
- date: 2008-03-10 00:00:00 -05:00
6
+ version: 0.2.2
7
+ date: 2008-03-11 00:00:00 -05:00
8
8
  summary: convenience methods for method chaining
9
9
  require_paths:
10
10
  - lib
@@ -35,6 +35,7 @@ files:
35
35
  - ./spec
36
36
  - ./README
37
37
  - ./rakefile
38
+ - ./example.rb
38
39
  - ./coverage
39
40
  - doc/files
40
41
  - doc/index.html
@@ -47,6 +48,7 @@ files:
47
48
  - lib/methodchain
48
49
  - lib/methodchain.rb
49
50
  - pkg/methodchain-0.2.0.gem
51
+ - pkg/methodchain-0.2.1.gem
50
52
  - pkg/methodchain-0.0.1.gem
51
53
  - pkg/methodchain-0.0.3.gem
52
54
  - pkg/methodchain-0.0.5.gem