comprise 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -49,7 +49,7 @@ listcomp(x: 1..3, y: -> { 1..x }, z: -> { [x + y] }).to_a
49
49
  # => [[1, 1, 2], [2, 1, 3], [2, 2, 4], [3, 1, 4], [3, 2, 5], [3, 3, 6]]
50
50
  ```
51
51
 
52
- *Important note: In Ruby 2.0.x Comprise makes use of Lazy emuerators.*
52
+ *Important note: In Ruby 2.0.x Comprise makes use of Lazy enumerators.*
53
53
 
54
54
  ## Contributing
55
55
 
@@ -1,15 +1,14 @@
1
1
  module Comprise
2
2
  class ListComprehension
3
3
  def initialize(lists)
4
- vars = lists.values
5
- enum = init_enum(vars.shift)
6
-
7
- @context = Struct.new(*lists.keys)
8
- @enumerator = vars.inject(enum) do |enum, var|
9
- if var.respond_to? :call
10
- enum.flat_map { |other| @context.new(*other).instance_exec(&var).map { |x| other + [x] } }
4
+ generators = lists.values
5
+ @details_for_inspect = lists.keys.zip(generators.map(&:class))
6
+ @context_klass = Struct.new(*lists.keys)
7
+ @enumerator = generators.inject(init_enumerator(generators.shift)) do |enumerator, generator|
8
+ if generator.respond_to? :call
9
+ enumerator.flat_map { |other| new_context(other).instance_exec(&generator).map { |x| other + [x] } }
11
10
  else
12
- enum.flat_map { |other| var.map { |x| other + [x] } }
11
+ enumerator.flat_map { |other| generator.map { |x| other + [x] } }
13
12
  end
14
13
  end
15
14
  end
@@ -19,18 +18,22 @@ module Comprise
19
18
  end
20
19
 
21
20
  def map
22
- @enumerator.map { |values| @context.new(*values).instance_exec(&Proc.new) }
21
+ @enumerator.map { |values| new_context(values).instance_exec(&Proc.new) }
23
22
  end
24
23
 
25
24
  def inspect
26
- "#<#{self.class.name}:#{self.object_id}>"
25
+ "#<#{self.class.name}:#{self.object_id} generators:#{ @details_for_inspect }>"
27
26
  end
28
27
 
29
28
  private
30
- def init_enum(var)
31
- var = var.respond_to?(:call) ? var.call : var
32
- var = var.lazy if var.respond_to? :lazy
33
- var.map { |x| [x] }
29
+ def new_context(values)
30
+ @context_klass.new(*values)
31
+ end
32
+
33
+ def init_enumerator(generator)
34
+ enumerator = generator.respond_to?(:call) ? generator.call : generator
35
+ enumerator = enumerator.lazy if enumerator.respond_to? :lazy
36
+ enumerator.map { |x| [x] }
34
37
  end
35
38
  end
36
39
  end
@@ -1,3 +1,3 @@
1
1
  module Comprise
2
- VERSION = "1.1.0"
2
+ VERSION = "1.1.1"
3
3
  end
@@ -43,4 +43,9 @@ describe Comprise::ListComprehension do
43
43
  [3,1,4], [3,2,5], [3,3,6]
44
44
  ]
45
45
  end
46
+
47
+ it "exposes a little context via #inspect" do
48
+ comp = listcomp(x: [], y: 0..0, z: ->{ []})
49
+ comp.inspect.should == "#<Comprise::ListComprehension:#{comp.object_id} generators:[[:x, Array], [:y, Range], [:z, Proc]]>"
50
+ end
46
51
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: comprise
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: