tourmaline 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2c1eb9fca6b97c2e8a848edc7db6c97dd2e96b19
4
- data.tar.gz: 2d6241798fc1e10e64334d139a1bd41bf716a762
3
+ metadata.gz: 3273045b4b0edf87d5c5e1537d45d9408534b333
4
+ data.tar.gz: 60b95db6bd5ae041d6206aaf52d17c9ccb6abb5a
5
5
  SHA512:
6
- metadata.gz: d368ad91534446e2611979fa58c9d462c4422260c7bb4513164e0a13a109ac74976fc42e7a024a9749b873431b0c9251b97f076788876905d316440227a6aaa6
7
- data.tar.gz: e596d013e1e2eaae2a50d9eba5368f88b61b09727c1a0176e8ebc53ee7f8b860a2a7b7b4383ed068e8d502da74697ec66df7a4f7347a5252b8e1c5042b7a5ed1
6
+ metadata.gz: 11d5a487be6b59bf40a799351cf48b3aa69f862463d2ab620ba85017439f9f558d60949d34ecfdd60d676c05669f612ed972a8123a86d27b3737fb5426d081c3
7
+ data.tar.gz: bff1451ffc18a4241a44c401368cea93afc34b06c4354c04c7e430677ab068ca91e8a2ffedcb1fd92f55d590f8ad5ce37f04746a24e27534def6ec54fb6b24ed
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  #Tourmaline
2
2
  [![Gem Version](https://badge.fury.io/rb/tourmaline.svg)](http://badge.fury.io/rb/tourmaline)
3
3
 
4
- ![Tourmaline logo](https://raw.githubusercontent.com/demonh3x/tourmaline/master/img/logo200.jpg)
4
+ ![Tourmaline logo](https://raw.githubusercontent.com/mateuadsuara/tourmaline/master/img/logo200.jpg)
5
5
 
6
6
  A configurable ruby interpreter for the command line
7
7
 
@@ -18,13 +18,22 @@ def execute_plugin
18
18
  end
19
19
  ```
20
20
 
21
- And with that, you will be able to run `rb 'execute_plugin'`
21
+ Given that, you will be able to run `rb 'execute_plugin'`
22
22
 
23
23
  ### Examples
24
- `ping www.google.com | rb 'lines.include?("time=").split("time=")[1].puts.each'`
25
24
 
26
- `echo -e '1\n5\n3\n7\n' | rb 'puts lines.to_i.take(3).inject(:+)'`
25
+ Show only the time of each ping:
27
26
 
28
- `rb 'lines.upcase.take_while{|l| !l.include?("QUIT")}.puts.each'`
27
+ `ping www.google.com | rb 'lines.include?("time=").split("time=")[1].puts'`
29
28
 
30
- `rb 'doing{rand(1..6)}.print.each'`
29
+ Sum the first three numbers:
30
+
31
+ `echo -e '1\n5\n3\n7\n' | rb 'lines.take(3).to_i.inject(:+).puts'`
32
+
33
+ Echo until "quit" is entered:
34
+
35
+ `rb 'lines.take_while{|l|l!="quit"}.puts'`
36
+
37
+ Play rock, paper, scissors:
38
+
39
+ `rb 'doing{["rock", "paper", "scissors"].sample}.take(2).puts'`
data/bin/rb CHANGED
@@ -8,7 +8,8 @@ def lines
8
8
  end
9
9
 
10
10
  begin
11
- eval(ARGV.join(" "))
11
+ res = eval(ARGV.join(" "))
12
+ res.each if res.class == FluentEnumerator
12
13
  rescue Errno::EPIPE
13
14
  rescue Interrupt
14
15
  end
data/lib/doing.rb CHANGED
@@ -1,67 +1,20 @@
1
+ require_relative 'doing/fluent_enumerator'
2
+
1
3
  module Doing
2
- def doing(stop_value = nil, &producer)
3
- enumerator = Enumerator.new do |yielder|
4
- loop do
5
- value = producer.call
6
- break if value.eql? stop_value
7
- yielder.yield value
8
- end
9
- end
4
+ def doing(stop_value = nil, &block)
5
+ raise ArgumentError.new('No block given') unless block_given?
10
6
 
11
- FluentEnumerator.new(enumerator)
7
+ FluentEnumerator.new(build_enumerator(stop_value, block))
12
8
  end
13
9
 
14
10
  private
15
- class FluentEnumerator
16
- def initialize(base_enumerator)
17
- @enum = Enumerator::Lazy.new(base_enumerator) do |yielder, *values|
18
- yielder.yield *values
19
- end
20
- end
21
-
22
- def next
23
- @enum.next
24
- end
25
-
26
- def each(&block)
27
- block ||= Proc.new{}
28
- @enum.each(&block)
29
- end
30
-
31
- def inject(*args, &block)
32
- @enum.send(:inject, *args, &block)
33
- end
34
-
35
- def select(*args, &block)
36
- self.class.new(@enum.send(:select, *args, &block))
37
- end
38
-
39
- def map(*args, &block)
40
- self.class.new(@enum.send(:map, *args, &block))
41
- end
42
-
43
- def take(*args, &block)
44
- self.class.new(@enum.send(:take, *args, &block))
45
- end
46
-
47
- def take_while(*args, &block)
48
- self.class.new(@enum.send(:take_while, *args, &block))
49
- end
50
-
51
- def method_missing(method, *args, &block)
52
- return select{|e| e.send(method, *args, &block)} if predicate?(method)
53
- map do |e|
54
- if e.respond_to?(method)
55
- e.send(method, *args, &block)
56
- else
57
- Kernel.send(method, *args.clone.unshift(e), &block)
58
- end
11
+ def build_enumerator(stop_value, block)
12
+ Enumerator.new do |caller|
13
+ loop do
14
+ value = block.call
15
+ break if value.eql? stop_value
16
+ caller.yield value
59
17
  end
60
18
  end
61
-
62
- private
63
- def predicate?(method)
64
- method.to_s.end_with?("?")
65
- end
66
19
  end
67
20
  end
@@ -0,0 +1,11 @@
1
+ module Doing
2
+ module EnumeratorWrapper
3
+ def wrap(*method_names)
4
+ method_names.each do |method_name|
5
+ define_method(method_name.to_s) do |*args, &block|
6
+ self.class.new(enumerator.send(method_name, *args, &block))
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,66 @@
1
+ require 'forwardable'
2
+ require_relative 'enumerator_wrapper'
3
+ require_relative 'fluent_value'
4
+
5
+ module Doing
6
+ class FluentEnumerator
7
+ extend EnumeratorWrapper
8
+ extend Forwardable
9
+
10
+ def_delegators :enumerator, :next
11
+ wrap :take_while, :take, :map, :select
12
+
13
+ def initialize(enumerator)
14
+ @enumerator = enumerator.lazy
15
+ end
16
+
17
+ def each(&block)
18
+ block ||= Proc.new{}
19
+ enumerator.each(&block)
20
+ end
21
+
22
+ def method_missing(method, *args, &block)
23
+ if predicate?(method)
24
+ select_elements(method, *args, &block)
25
+ else
26
+ map_elements(method, *args, &block)
27
+ end
28
+ end
29
+
30
+ def inject(*args, &block)
31
+ FluentValue.new(enumerator.send(:inject, *args, &block))
32
+ end
33
+ alias_method :reduce, :inject
34
+
35
+ private
36
+ attr_reader :enumerator
37
+
38
+ def predicate?(method)
39
+ method.to_s.end_with?("?")
40
+ end
41
+
42
+ def select_elements(method, *args, &block)
43
+ select do |e|
44
+ if e.respond_to?(method)
45
+ e.send(method, *args, &block)
46
+ elsif e.respond_to?(remove_question_marks(method))
47
+ e.send(remove_question_marks(method), *args, &block)
48
+ end
49
+ end
50
+ end
51
+
52
+ def remove_question_marks(method)
53
+ method.to_s.gsub(/\?/, "")
54
+ end
55
+
56
+ def map_elements(method, *args, &block)
57
+ map do |e|
58
+ if e.respond_to?(method)
59
+ e.send(method, *args, &block)
60
+ else
61
+ Kernel.send(method, *args.clone.unshift(e), &block)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,18 @@
1
+ module Doing
2
+ class FluentValue < BasicObject
3
+ attr_reader :it
4
+
5
+ def initialize(it)
6
+ @it = it
7
+ end
8
+
9
+ def method_missing(method, *args, &block)
10
+ result = if it.respond_to?(method)
11
+ it.send(method, *args, &block)
12
+ else
13
+ ::Kernel.send(method, *args.clone.unshift(it), &block)
14
+ end
15
+ FluentValue.new(result)
16
+ end
17
+ end
18
+ end
data/tourmaline.gemspec CHANGED
@@ -4,12 +4,12 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "tourmaline"
7
- gem.version = "0.1.2"
7
+ gem.version = "0.1.3"
8
8
  gem.authors = ["Mateu Adsuara"]
9
9
  gem.email = ["mateuadsuara@gmail.com"]
10
10
 
11
11
  gem.summary = %q{A configurable ruby interpreter for the command line}
12
- gem.homepage = "https://github.com/demonh3x/tourmaline"
12
+ gem.homepage = "https://github.com/mateuadsuara/tourmaline"
13
13
  gem.license = "MIT"
14
14
 
15
15
  gem.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tourmaline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateu Adsuara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-14 00:00:00.000000000 Z
11
+ date: 2016-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  description:
@@ -37,8 +37,11 @@ files:
37
37
  - bin/rb
38
38
  - img/logo200.jpg
39
39
  - lib/doing.rb
40
+ - lib/doing/enumerator_wrapper.rb
41
+ - lib/doing/fluent_enumerator.rb
42
+ - lib/doing/fluent_value.rb
40
43
  - tourmaline.gemspec
41
- homepage: https://github.com/demonh3x/tourmaline
44
+ homepage: https://github.com/mateuadsuara/tourmaline
42
45
  licenses:
43
46
  - MIT
44
47
  metadata: {}
@@ -48,19 +51,18 @@ require_paths:
48
51
  - lib
49
52
  required_ruby_version: !ruby/object:Gem::Requirement
50
53
  requirements:
51
- - - ">="
54
+ - - '>='
52
55
  - !ruby/object:Gem::Version
53
56
  version: '0'
54
57
  required_rubygems_version: !ruby/object:Gem::Requirement
55
58
  requirements:
56
- - - ">="
59
+ - - '>='
57
60
  - !ruby/object:Gem::Version
58
61
  version: '0'
59
62
  requirements: []
60
63
  rubyforge_project:
61
- rubygems_version: 2.4.6
64
+ rubygems_version: 2.0.14
62
65
  signing_key:
63
66
  specification_version: 4
64
67
  summary: A configurable ruby interpreter for the command line
65
68
  test_files: []
66
- has_rdoc: