Class: Enumerable::Pipe

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/agents/sets/enum/pipe.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#each_cluster, #each_with_neighbors, #group, nest, #nest, #pipe

Constructor Details

#initialize(enum, filter_spec = nil, *args, &filter_proc) ⇒ Pipe

Returns a new instance of Pipe.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/agents/sets/enum/pipe.rb', line 10

def initialize enum, filter_spec = nil, *args, &filter_proc
  @enum = enum
  @args = args
  
  case filter_spec
  when Symbol
    @filter_name = filter_spec
  when String
    @filter_name = filter_spec.intern
  when nil
    @filter_map = filter_proc
  else
    unless filter_spec.respond_to? :[]
      raise ArgumentError,
        "filter_spec must be a method name or respond to []."
    end
    @filter_map = filter_spec
  end
  
  unless @filter_name or @filter_map
    raise ArgumentError,
      "no filter specified."
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



8
9
10
# File 'lib/agents/sets/enum/pipe.rb', line 8

def args
  @args
end

#enumObject (readonly)

Returns the value of attribute enum.



8
9
10
# File 'lib/agents/sets/enum/pipe.rb', line 8

def enum
  @enum
end

#filter_mapObject (readonly)

Returns the value of attribute filter_map.



8
9
10
# File 'lib/agents/sets/enum/pipe.rb', line 8

def filter_map
  @filter_map
end

#filter_nameObject (readonly)

Returns the value of attribute filter_name.



8
9
10
# File 'lib/agents/sets/enum/pipe.rb', line 8

def filter_name
  @filter_name
end

Instance Method Details

#eachObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/agents/sets/enum/pipe.rb', line 35

def each
  if @filter_name
    filter_name = @filter_name
    message = filter_name, *@args
    @enum.each { |entry|
      yield entry.send *message
    }
  elsif @filter_map
    filter_map = @filter_map
    args = *@args
    @enum.each { |entry|
      yield filter_map[entry, *args]
    }
  end
end