Class: Terrazzo::Filter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resources, dashboard, filter_name, filter_value = nil) ⇒ Filter

Returns a new instance of Filter.



5
6
7
8
9
10
# File 'lib/terrazzo/filter.rb', line 5

def initialize(resources, dashboard, filter_name, filter_value = nil)
  @resources = resources
  @dashboard = dashboard
  @filter_name = filter_name&.to_sym
  @filter_value = filter_value
end

Instance Attribute Details

#dashboardObject (readonly)

Returns the value of attribute dashboard.



3
4
5
# File 'lib/terrazzo/filter.rb', line 3

def dashboard
  @dashboard
end

#filter_nameObject (readonly)

Returns the value of attribute filter_name.



3
4
5
# File 'lib/terrazzo/filter.rb', line 3

def filter_name
  @filter_name
end

#filter_valueObject (readonly)

Returns the value of attribute filter_value.



3
4
5
# File 'lib/terrazzo/filter.rb', line 3

def filter_value
  @filter_value
end

#resourcesObject (readonly)

Returns the value of attribute resources.



3
4
5
# File 'lib/terrazzo/filter.rb', line 3

def resources
  @resources
end

Instance Method Details

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/terrazzo/filter.rb', line 12

def run
  return resources unless filter_name

  filters = dashboard.collection_filters
  filter_lambda = filters[filter_name]
  return resources unless filter_lambda

  if filter_lambda.arity == 1 || (filter_lambda.arity < 0 && filter_value.nil?)
    filter_lambda.call(resources)
  else
    filter_lambda.call(resources, filter_value)
  end
end