Class: Melitta::FilterDsl

Inherits:
Object
  • Object
show all
Defined in:
lib/melitta/filter_dsl.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver) ⇒ FilterDsl

Returns a new instance of FilterDsl.



11
12
13
# File 'lib/melitta/filter_dsl.rb', line 11

def initialize(receiver)
  @receiver = receiver.new
end

Instance Attribute Details

#receiverObject (readonly)

Returns the value of attribute receiver.



3
4
5
# File 'lib/melitta/filter_dsl.rb', line 3

def receiver
  @receiver
end

Class Method Details

.evaluate(type, &block) ⇒ Object



5
6
7
8
9
# File 'lib/melitta/filter_dsl.rb', line 5

def self.evaluate type, &block
  filter = new(type)
  filter.instance_eval &block
  filter.receiver
end

Instance Method Details

#allow(field, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/melitta/filter_dsl.rb', line 39

def allow field, &block
  if block_given?
    filter = Melitta::Filters::PassThrough.new
    second_level_filter = evaluate(Melitta::Filters::Tre, block)
    filter.filter(field, second_level_filter)
  else
    filter = Melitta::Filters::PassThrough.new
  end

  receiver.filter(field, filter)
end

#demand(field, coercer = String, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/melitta/filter_dsl.rb', line 26

def demand field, coercer = String, &block
  if block_given?
    filter = evaluate(Melitta::Filters::Tree, block)
    filter.required = true
  else
    filter = Melitta::Filters::Base.new
    filter.required = true
    filter.coercer = evaluate_coercer(coercer)
  end

  receiver.filter(field, filter)
end

#keep(field, coercer = String, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/melitta/filter_dsl.rb', line 15

def keep field, coercer = String, &block
  if block_given?
    filter = evaluate(Melitta::Filters::Tree, block)
  else
    filter = Melitta::Filters::Base.new()
    filter.coercer = evaluate_coercer(coercer)
  end

  receiver.filter(field, filter)
end