Class: RShade::Filter::FilterComposition

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rshade/filter/filter_composition.rb

Constant Summary collapse

AND_OP =
:and
OR_OP =
:or
UNARY_OP =
:unary

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op = UNARY_OP, left = nil, right = nil) ⇒ FilterComposition

Returns a new instance of FilterComposition.

Parameters:

  • left (#call, Enumerable) (defaults to: nil)
  • right (#call, Enumerable) (defaults to: nil)


13
14
15
16
17
# File 'lib/rshade/filter/filter_composition.rb', line 13

def initialize(op=UNARY_OP, left=nil, right=nil)
  @op = op
  @left = left
  @right = right
end

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.



8
9
10
# File 'lib/rshade/filter/filter_composition.rb', line 8

def left
  @left
end

#opObject (readonly)

Returns the value of attribute op.



8
9
10
# File 'lib/rshade/filter/filter_composition.rb', line 8

def op
  @op
end

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/rshade/filter/filter_composition.rb', line 8

def parent
  @parent
end

#rightObject (readonly)

Returns the value of attribute right.



8
9
10
# File 'lib/rshade/filter/filter_composition.rb', line 8

def right
  @right
end

Instance Method Details

#call(event) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rshade/filter/filter_composition.rb', line 19

def call(event)
  case op
  when UNARY_OP
    return left&.call(event)
  when AND_OP
    return left&.call(event) && right&.call(event)
  when OR_OP
    l = left&.call(event)
    r = right&.call(event)
    # puts "#{left} => #{l} OR #{right} => #{r}"
    return l || r
  else
    raise 'undefined op'
  end
end

#config_filter(type, &block) ⇒ Object



49
50
51
52
53
54
# File 'lib/rshade/filter/filter_composition.rb', line 49

def config_filter(type, &block)
  filter = find do |filter|
    filter.is_a? type
  end
  filter.config(&block) if filter
end

#each(&block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rshade/filter/filter_composition.rb', line 35

def each(&block)
  if left&.respond_to?(:each)
    left&.each(&block)
  else
    yield left
  end

  if right&.respond_to?(:each)
    right&.each(&block)
  else
    yield right
  end
end