Class: EventFilter

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ EventFilter

Returns a new instance of EventFilter


26
27
28
29
30
31
32
# File 'lib/event_filter.rb', line 26

def initialize(params)
  @params = if params
              params.dup
            else
              []#EventFilter.default_filter
            end
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params


2
3
4
# File 'lib/event_filter.rb', line 2

def params
  @params
end

Class Method Details

.commentsObject


17
18
19
# File 'lib/event_filter.rb', line 17

def comments
  'comments'
end

.default_filterObject


5
6
7
# File 'lib/event_filter.rb', line 5

def default_filter
  %w{ push issues merge_requests team}
end

.mergedObject


13
14
15
# File 'lib/event_filter.rb', line 13

def merged
  'merged'
end

.pushObject


9
10
11
# File 'lib/event_filter.rb', line 9

def push
  'push'
end

.teamObject


21
22
23
# File 'lib/event_filter.rb', line 21

def team
  'team'
end

Instance Method Details

#active?(key) ⇒ Boolean

Returns:

  • (Boolean)

65
66
67
# File 'lib/event_filter.rb', line 65

def active?(key)
  params.include? key
end

#apply_filter(events) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/event_filter.rb', line 34

def apply_filter(events)
  return events unless params.present?

  filter = params.dup

  actions = []
  actions << Event::PUSHED if filter.include? 'push'
  actions << Event::MERGED if filter.include? 'merged'

  if filter.include? 'team'
    actions << Event::JOINED
    actions << Event::LEFT
  end

  actions << Event::COMMENTED if filter.include? 'comments'

  events.where(action: actions)
end

#options(key) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/event_filter.rb', line 53

def options(key)
  filter = params.dup

  if filter.include? key
    filter.delete key
  else
    filter << key
  end

  filter
end