its-it 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,14 @@
2
2
 
3
3
  == Overview
4
4
 
5
- This gem defines kernel methods <code>its</code> and <code>it</code> that extend the Symbol#to_proc idiom to support chaining multiple methods.
5
+ This gem defines kernel methods <code>its</code> and <code>it</code> that
6
+ queue and defer method calls. This is handy for list enumeration and case
7
+ statements.
8
+
9
+ == List Enumeration
10
+
11
+ <code>its</code> and <code>it</code> extend the Symbol#to_proc idiom to
12
+ support chaining multiple methods.
6
13
 
7
14
  The pure Ruby way to chain methods when enumerating a list would be:
8
15
 
@@ -16,11 +23,66 @@ And using <code>its</code>, this becomes becomes simpler still:
16
23
 
17
24
  users.collect(&its.contact.last_name.capitalize)
18
25
 
26
+ Note that <code>its</code> captures arguments and blocks, allowing constructs like this,
27
+ which will select users whose names include any non-hyphenated word that's more than 10
28
+ letters long:
29
+ users.select(&its.name.split(/ /).reject{|word| word =~ /-/}.collect(&:length).max > 10)
30
+
19
31
  <code>it</code> is an alias for <code>its</code>, to use with methods that
20
32
  describe actions rather than posessives. For example:
21
33
 
22
34
  items.collect(&it.to_s.capitalize)
23
35
 
36
+ == Case statements
37
+
38
+ <code>its</code> and <code>it</code> likewise extend Ruby's
39
+ <code>code</code> statement to support testing arbitrary methods,
40
+ minimizing the need to create temporary variables and use
41
+ <code>if-elsif</code> constructs.
42
+
43
+ In pure Ruby, doing comparisons on computed values would be done something
44
+ like this:
45
+
46
+ maxlen = arrays.collect(&size).max
47
+ if maxlen > 10000
48
+ "too big"
49
+ elsif maxlen < 10
50
+ "too small"
51
+ else
52
+ "okay"
53
+ end
54
+
55
+ But using <code>it</code> this becomes:
56
+
57
+ case arrays.collect(&size).max
58
+ when it > 1000
59
+ "too big"
60
+ when it < 10
61
+ "too small"
62
+ else
63
+ "okay"
64
+ end
65
+
66
+ Of course method chanining can be used here too:
67
+
68
+ case users.first
69
+ when its.name == "Gimme Cookie" then ...
70
+ when its.name.length > 10 then ...
71
+ else ...
72
+ end
73
+
74
+ == Under the hood
75
+
76
+ The <code>ItsIt::It</code> class starts from <code>BlankSlate</code>, and
77
+ uses <code>method_missing</code> to capture and queue up all methods and
78
+ their arguments, with the exception of <code>:to_proc</code> and <code>:===</code>
79
+ (and also excepting <code>:respond_to? :to_proc</code> and
80
+ <code>:respond_to? :===</code>).
81
+
82
+ <code>:to_proc</code> returns a proc that will evaluate the method queue on
83
+ a given argument. <code>:===</code> takes an argument and evaluates that
84
+ proc, returning the result.
85
+
24
86
  == Installation
25
87
 
26
88
  Install from http://rubygems.org via
@@ -37,8 +99,9 @@ Tested with <b>ruby 1.8.7</b> and <b>ruby 1.9.2</b>
37
99
 
38
100
  == History
39
101
 
40
- This gem is a clone of Jay Philips'
41
- methodphitamine[https://github.com/jicksta/methodphitamine] gem, updated to
42
- be compatible with ruby 1.9.2 and gemspec, renamed its-it, and installed on
43
- rubygems.org[http://rubygems.org]. Unlike methodphitamine, this gem includes only
44
- <code>its</code> and <code>it</code>, not the "maybe" monad.
102
+ This gem is orignally based on Jay Philips'
103
+ methodphitamine[https://github.com/jicksta/methodphitamine] gem. It has
104
+ been updated to be compatible with ruby 1.9.2 and gemspec, renamed its-it,
105
+ and installed on rubygems.org[http://rubygems.org]. Unlike
106
+ methodphitamine, this gem includes only <code>its</code> and
107
+ <code>it</code>, not the "maybe" monad.
@@ -5,19 +5,21 @@ Gem::Specification.new do |s|
5
5
  s.name = 'its-it'
6
6
  s.version = ItsIt::VERSION
7
7
  s.platform = Gem::Platform::RUBY
8
- s.date = "2011-04-23"
8
+ s.date = "2011-04-26"
9
9
  s.authors = ['Ronen Barzel']
10
10
  s.email = 'ronen@barzel.org'
11
11
  s.homepage = 'http://github.com/ronen/its-it'
12
- s.summary = %q{Defines its() and it() method-chain proc proxies.}
12
+ s.summary = %q{Defines its() and it() method-chain proxies.}
13
13
  s.description = %q{
14
- This gem defines the Kernel method "it", which extends the Symbol#to_proc
15
- idiom to support chaining multiple methods. For example,
16
- items.collect(&it.to_s.capitalize). The method is also aliased as "its",
17
- for methods that describe possessives rather than actions, such as
18
- items.collect(&its.name.capitalize)
14
+ This gem defines the Kernel method "it" that queue and defer method calls.
15
+ This extends the Symbol#to_proc idiom to support chaining multiple methods.
16
+ For example, items.collect(&it.to_s.capitalize). This also allows
17
+ conditionals in case statements, such as: case ... when it > 3 then [etc.].
18
+ The method is also aliased as "its", for methods that describe possessives
19
+ rather than actions, such as items.collect(&its.name.capitalize)
19
20
 
20
- [This gem is clone of Jay Philips' "methodphitamine" gem, updated for ruby 1.9 and gemspec compatibility.]
21
+ [This gem is an extension of Jay Philips' "methodphitamine" gem, updated
22
+ for ruby 1.9 and gemspec compatibility and adding the case statement functionality.]
21
23
  }
22
24
  s.extra_rdoc_files = [
23
25
  "LICENSE.txt",
@@ -17,7 +17,7 @@ module ItsIt
17
17
  end
18
18
 
19
19
  def method_missing(*args, &block)
20
- @methods << [args, block] unless args == [:respond_to?, :to_proc]
20
+ @methods << [args, block] unless args.first == :respond_to? and [:to_proc, :===].include?(args[1])
21
21
  self
22
22
  end
23
23
 
@@ -29,6 +29,10 @@ module ItsIt
29
29
  end
30
30
  end
31
31
 
32
+ def ===(obj)
33
+ to_proc.call(obj)
34
+ end
35
+
32
36
  # Used for testing. This method is hidden but can be revealed using
33
37
  # ItsIt::It.reveal(:method_queue)
34
38
  def method_queue
@@ -1,3 +1,3 @@
1
1
  module ItsIt
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -47,10 +47,29 @@ describe "An It instance" do
47
47
  it "should respond to to_proc()" do
48
48
  @it.should respond_to(:to_proc)
49
49
  end
50
+
51
+ it "should respond to ===" do
52
+ @it.should respond_to(:===)
53
+ end
54
+
55
+ it "should work in a case statement" do
56
+ [0,1,2].each do |i|
57
+ case i
58
+ when it < 1 then i.should == 0
59
+ when it == 1 then i.should == 1
60
+ else i.should == 2
61
+ end
62
+ end
63
+ end
50
64
 
51
65
  it "should not queue the method respond_to? when given :to_proc as an arg" do
52
66
  @it.respond_to? :to_proc
53
67
  @it.method_queue.should be_empty
54
68
  end
69
+
70
+ it "should not queue the method respond_to? when given :=== as an arg" do
71
+ @it.respond_to? :to_proc
72
+ @it.method_queue.should be_empty
73
+ end
55
74
 
56
75
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: its-it
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.1
5
+ version: 1.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ronen Barzel
@@ -10,7 +10,8 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-23 00:00:00 Z
13
+ date: 2011-04-26 00:00:00 -07:00
14
+ default_executable:
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency
16
17
  name: blankslate
@@ -79,12 +80,14 @@ dependencies:
79
80
  prerelease: false
80
81
  version_requirements: *id006
81
82
  description: "\n\
82
- This gem defines the Kernel method \"it\", which extends the Symbol#to_proc\n\
83
- idiom to support chaining multiple methods. For example,\n\
84
- items.collect(&it.to_s.capitalize). The method is also aliased as \"its\",\n\
85
- for methods that describe possessives rather than actions, such as\n\
86
- items.collect(&its.name.capitalize)\n\n\
87
- [This gem is clone of Jay Philips' \"methodphitamine\" gem, updated for ruby 1.9 and gemspec compatibility.]\n"
83
+ This gem defines the Kernel method \"it\" that queue and defer method calls.\n\
84
+ This extends the Symbol#to_proc idiom to support chaining multiple methods.\n\
85
+ For example, items.collect(&it.to_s.capitalize). This also allows\n\
86
+ conditionals in case statements, such as: case ... when it > 3 then [etc.].\n\
87
+ The method is also aliased as \"its\", for methods that describe possessives\n\
88
+ rather than actions, such as items.collect(&its.name.capitalize)\n\n\
89
+ [This gem is an extension of Jay Philips' \"methodphitamine\" gem, updated\n\
90
+ for ruby 1.9 and gemspec compatibility and adding the case statement functionality.]\n"
88
91
  email: ronen@barzel.org
89
92
  executables: []
90
93
 
@@ -109,6 +112,7 @@ files:
109
112
  - spec/it_spec.rb
110
113
  - spec/rspec_compatibility_spec.rb
111
114
  - spec/spec_helper.rb
115
+ has_rdoc: true
112
116
  homepage: http://github.com/ronen/its-it
113
117
  licenses: []
114
118
 
@@ -122,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
122
126
  requirements:
123
127
  - - ">="
124
128
  - !ruby/object:Gem::Version
125
- hash: 143375
129
+ hash: -664830529632804485
126
130
  segments:
127
131
  - 0
128
132
  version: "0"
@@ -135,10 +139,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
139
  requirements: []
136
140
 
137
141
  rubyforge_project:
138
- rubygems_version: 1.7.2
142
+ rubygems_version: 1.6.2
139
143
  signing_key:
140
144
  specification_version: 3
141
- summary: Defines its() and it() method-chain proc proxies.
145
+ summary: Defines its() and it() method-chain proxies.
142
146
  test_files:
143
147
  - spec/it_spec.rb
144
148
  - spec/rspec_compatibility_spec.rb