give4each 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,10 +1,44 @@
1
1
 
2
2
  = Give4each
3
3
 
4
+ https://github.com/pasberth/give4each
5
+
4
6
  == install
5
7
 
6
8
  $ gem install give4each
7
9
 
8
- == example
10
+ == description
11
+
12
+ ああああああ
13
+ もうブロックを書くのはうんざりだ!!!
14
+
15
+ (1..5).map { |i| i ** 2 }
16
+
17
+ だからこう書けるようにした
18
+
19
+ (1..5).map &:**.with(2) # => [1, 4, 9, 16, 25]
20
+
21
+ ああああああ
22
+ こういうのももう嫌だ!!!
23
+
24
+ def initialize
25
+ @stack = []
26
+ end
27
+
28
+ def foo *xs
29
+ xs.each { |x| @stack.push x }
30
+ end
31
+
32
+ だからこう書けるように(ry
33
+
34
+ xs.each &:push.to(@array)
35
+
36
+
37
+ こういうのも(ry
38
+
39
+ f = "hello %s world"
40
+ %w[ruby python].map { |a| f % a }
41
+
42
+ だからこう(ry
9
43
 
10
- :include: examples/example.rb
44
+ %w[ruby python].map &:%.in("hello %s world")
data/lib/give4each.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  $:.unshift File.dirname(__FILE__)
2
2
 
3
3
  module Give4Each
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
6
6
 
7
7
  require "give4each/private_helpers"
@@ -1,17 +1,22 @@
1
+
1
2
  class Symbol
2
3
 
3
- # Create new Give4Each::MethodChain with the Symbol and +args+ and +block+.
4
- # *example*:
5
- # :include: examples/with.rb
6
- def with *args, &block
7
- Give4Each::MethodChain.new self, *args, &block
8
- end
4
+ # :method: a
5
+ # see also Give4Each::MethodChain#a
6
+
7
+ # :method: an
8
+ # see also Give4Each::MethodChain#an
9
+
10
+ # :method: with
11
+ # see also Give4Each::MethodChain#with
12
+
13
+ # :method: in
14
+ # see also Give4Each::MethodChain#in
15
+
16
+ # :method: to
17
+ # see also Give4Each::MethodChain#to
9
18
 
10
- # Examples::
11
- # *of_\**:
12
- # :include: examples/of.rb
13
- # *and_\**:
14
- # :include: examples/and.rb
19
+ # see also Give4Each::MethodChain#method_missing
15
20
  def method_missing f, *a, &b
16
21
  if Give4Each.allowing_method? f
17
22
  Give4Each::MethodChain.new(self).send f, *a, &b
@@ -3,7 +3,7 @@ require 'give4each/private_helpers'
3
3
 
4
4
  class Give4Each::MethodChain
5
5
 
6
- HasArgs = Struct.new :method, :args, :block
6
+ HasArgs = Struct.new :method, :args, :block, :callback
7
7
 
8
8
  # for example,
9
9
  # Give4Eeach::MethodChain.new :any, *args, &block
@@ -11,45 +11,102 @@ class Give4Each::MethodChain
11
11
  # :any.with *args, &block
12
12
  def initialize method, *args, &block
13
13
  raise TypeError, "#{self.class} need to the symbol of the method." unless method.respond_to? :to_sym
14
- @callings = [].push HasArgs.new method.to_sym, args, block
14
+ @current = natural method, *args, &block
15
+ @callings = [@current]
15
16
  end
16
17
 
17
18
  # Examples::
18
19
  # *of_\**:
19
- # :include: examples/of.rb
20
+ # %w[c++ lisp].map &:upcase.of_concat.with("er") # => ["C++ER", "LISPER"]
20
21
  # *and_\**:
21
- # :include: examples/and.rb
22
+ # %w[c++ lisp].map &:upcase.and_concat.with("er") # => ["C++er", "LISPer"]
22
23
  # You can do the same as +with+ if you pass the +args+.
23
24
  # %w[c++ lisp].map &:upcase.and_concat("er") # => ["C++er", "LISPer"]
24
25
  def method_missing method, *args, &block
25
26
  case method.to_s
26
27
  when /^of_(.*)$/
27
- @current = HasArgs.new $1, args, block
28
+ @current = natural $1, *args, &block
28
29
  @callings.unshift @current
30
+ return self
29
31
  when /^and_(.*)$/
30
- @current = HasArgs.new $1, args, block
32
+ @current = natural $1, *args, &block
31
33
  @callings.push @current
32
- else super
34
+ return self
33
35
  end
34
- self
36
+
37
+ return to_proc.send method, *args, &block if Proc.instance_methods.include? method
38
+
39
+ super
40
+ end
41
+
42
+ def natural method, *args, &block
43
+ HasArgs.new method.to_sym, args, block, lambda { |o, has| o.send has.method, *has.args, &has.block }
35
44
  end
45
+
46
+ private :natural
36
47
 
37
48
  # example:
38
- # :include: examples/with.rb
49
+ # # (1..5).map do |i|
50
+ # # i ** 2
51
+ # # end
52
+ # p (1..5).map &:**.with(2) # => [1, 4, 9, 16, 25]
53
+ #
54
+ # # %w[c++ lisp].map do |a|
55
+ # # a.concat("er").upcase
56
+ # # end
57
+ # p %w[c++ lisp].map &:upcase.of_concat.with("er") # => ["C++ER", "LISPER"]
58
+ #
59
+ # # %w[c++ lisp].map do |a|
60
+ # # a.upcase.concat("er")
61
+ # # end
62
+ # p %w[c++ lisp].map &:upcase.and_concat.with("er") # => ["C++er", "LISPer"]
39
63
  def with *args, &block
40
64
  @current.args = args
41
65
  @current.block = block
42
66
  self
43
67
  end
44
68
 
69
+ alias a with
70
+ alias an with
71
+ alias the with
72
+
73
+ # *example*:
74
+ # a = []
75
+ # # => []
76
+ # (1..10).each &:push.to(a)
77
+ # # => 1..10
78
+ # a
79
+ # # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
80
+ def to *receivers
81
+ @current.callback = lambda do |o, has|
82
+ receivers.each &has.method.with(o, *has.args, &has.block); o
83
+ end
84
+ self
85
+ end
86
+
87
+ # *example*:
88
+ # receiver = "hello %s world"
89
+ # %w[ruby python].map &:%.in(receiver) # => ["hello ruby world", "hello python world"]
90
+ # *method chain*:
91
+ # %w[ruby python].map &:%.in(receiver).and_upcase # => ["HELLO RUBY WORLD", "HELLO PYTHON WORLD"]
92
+ # You should not use #to for that.
93
+ # receiver = "hello %s world"
94
+ # %w[ruby python].map &:%.to(receiver) # => ["ruby", "python"]
95
+ def in receiver
96
+ @current.callback = lambda do |o, has|
97
+ receiver.send has.method, o, *has.args, &has.block
98
+ end
99
+ self
100
+ end
101
+
45
102
  def to_sym
46
103
  @method
47
104
  end
48
105
 
49
106
  def to_proc
50
- proc do |o, *a, &b|
107
+ lambda do |o, &b|
51
108
  @callings.inject o do |o, has|
52
- o.send has.method, *has.args, &has.block
109
+ has.callback.call o, has
53
110
  end
54
111
  end
55
112
  end
@@ -3,8 +3,8 @@ module Give4Each; end
3
3
  module Give4Each::PrivateHelpers # :nodoc: all
4
4
 
5
5
  def allowing_method? f
6
- [/^of_.*$/, /^and_.*$/].any? { |a| f.to_s =~ a } or
7
- [:with].include? f.to_sym
6
+ [:with, :a, :an, :the, :to, :in].include? f.to_sym or
7
+ [/^of_.*$/, /^and_.*$/].any?(&:=~.with(f.to_s))
8
8
  end
9
9
 
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: give4each
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-18 00:00:00.000000000 Z
12
+ date: 2012-02-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdoc
16
- requirement: &70132470706940 !ruby/object:Gem::Requirement
16
+ requirement: &70239049844320 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.10'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70132470706940
24
+ version_requirements: *70239049844320
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: hoe
27
- requirement: &70132470706320 !ruby/object:Gem::Requirement
27
+ requirement: &70239049843680 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,8 +32,13 @@ dependencies:
32
32
  version: '2.13'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70132470706320
36
- description: ''
35
+ version_requirements: *70239049843680
36
+ description: ! "ああああああ\nもうブロックを書くのはうんざりだ!!!\n\n (1..5).map { |i| i ** 2 }\n\nだからこう書けるようにした\n
37
+ \ \n (1..5).map &:**.with(2) # => [1, 4, 9, 16, 25]\n\nああああああ\nこういうのももう嫌だ!!!\n\n
38
+ \ def initialize\n @stack = []\n end\n\n def foo *xs\n xs.each { |x| @stack.push
39
+ x }\n end\n\nだからこう書けるように(ry\n\n xs.each &:push.to(@array)\n\n\nこういうのも(ry\n\n
40
+ \ f = \"hello %s world\"\n %w[ruby python].map { |a| f % a }\n\nだからこう(ry\n\n %w[ruby
41
+ python].map &:%.in(\"hello %s world\")"
37
42
  email:
38
43
  - pasberth@gmail.com
39
44
  executables: []
@@ -53,7 +58,7 @@ files:
53
58
  - examples/of.rb
54
59
  - examples/with.rb
55
60
  - .gemtest
56
- homepage:
61
+ homepage: https://github.com/pasberth/give4each
57
62
  licenses: []
58
63
  post_install_message:
59
64
  rdoc_options:
@@ -78,5 +83,9 @@ rubyforge_project: give4each
78
83
  rubygems_version: 1.8.10
79
84
  signing_key:
80
85
  specification_version: 3
81
- summary: ''
86
+ summary: ! 'ああああああ もうブロックを書くのはうんざりだ!!! (1..5).map { |i| i ** 2 } だからこう書けるようにした (1..5).map
87
+ &:**.with(2) # => [1, 4, 9, 16, 25] ああああああ こういうのももう嫌だ!!! def initialize @stack
88
+ = [] end def foo *xs xs.each { |x| @stack.push x } end だからこう書けるように(ry xs.each
89
+ &:push.to(@array) こういうのも(ry f = "hello %s world" %w[ruby python].map { |a| f
90
+ % a } だからこう(ry %w[ruby python].map &:%.in("hello %s world")'
82
91
  test_files: []