Class: Filterameter::FilterRegistry

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

Overview

Filters

Class FilterRegistry is a collection of the filters. It captures the filter declarations when classes are loaded, then uses the injected FilterFactory to build the filters on demand as they are needed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter_factory) ⇒ FilterRegistry

Returns a new instance of FilterRegistry.



11
12
13
14
15
16
# File 'lib/filterameter/filter_registry.rb', line 11

def initialize(filter_factory)
  @filter_factory = filter_factory
  @declarations = {}
  @ranges = {}
  @filters = {}
end

Instance Attribute Details

#rangesObject (readonly)

Returns the value of attribute ranges.



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

def ranges
  @ranges
end

Instance Method Details

#add_filter(parameter_name, options) ⇒ Object



18
19
20
21
22
23
# File 'lib/filterameter/filter_registry.rb', line 18

def add_filter(parameter_name, options)
  name = parameter_name.to_s
  @declarations[name] = Filterameter::FilterDeclaration.new(name, options).tap do |fd|
    add_declarations_for_range(fd, options, name) if fd.range_enabled?
  end
end

#fetch(parameter_name) ⇒ Object



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

def fetch(parameter_name)
  name = parameter_name.to_s
  @filters.fetch(name) do
    raise Filterameter::Exceptions::UndeclaredParameterError, name unless @declarations.keys.include?(name)

    @filters[name] = @filter_factory.build(@declarations[name])
  end
end

#filter_declarationsObject



34
35
36
# File 'lib/filterameter/filter_registry.rb', line 34

def filter_declarations
  @declarations.values
end