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
# File 'lib/filterameter/filter_registry.rb', line 18

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

#fetch(name) ⇒ Object



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

def fetch(name)
  name = 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



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

def filter_declarations
  @declarations.values
end