Module: Cadenza::Context::Filters

Included in:
Cadenza::Context
Defined in:
lib/cadenza/context/filters.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filtersHash (readonly)

Returns the filter names mapped to their implementing procs.

Returns:

  • (Hash)

    the filter names mapped to their implementing procs



10
11
12
# File 'lib/cadenza/context/filters.rb', line 10

def filters
   @filters ||= {}
end

Instance Method Details

#alias_filter(original_name, alias_name) ⇒ Object

creates an alias of the given filter name under a different name

Parameters:

  • original_name (Symbol)

    the original name of the filter

  • alias_name (Symbol)

    the new name of the filter

Returns:

  • nil

Raises:



40
41
42
# File 'lib/cadenza/context/filters.rb', line 40

def alias_filter(original_name, alias_name)
   define_filter alias_name, &lookup_filter(original_name)
end

#define_filter(name) {|String, *args| ... } ⇒ Object

defines a filter proc with the given name

Parameters:

  • name (Symbol)

    the name for the template to use for this filter

Yields:

  • (String, *args)

    the block will receive the input string and a variable number of arguments passed to the filter.

Returns:

  • nil



29
30
31
32
# File 'lib/cadenza/context/filters.rb', line 29

def define_filter(name, &block)
   filters[name.to_sym] = block
   nil
end

#evaluate_filter(name, input, params = []) ⇒ String

calls the defined filter proc with the given parameters and returns the result.

Parameters:

  • name (Symbol)

    the name of the filter to evaluate

  • input (Object)

    the input value which will be filtered

  • params (Array) (defaults to: [])

    a list of parameters to pass to the filter block when calling it.

Returns:

  • (String)

    the result of evaluating the filter

Raises:



53
54
55
# File 'lib/cadenza/context/filters.rb', line 53

def evaluate_filter(name, input, params=[])
   lookup_filter(name).call(input, params)
end

#lookup_filter(name) ⇒ Proc

looks up the filter by name

Parameters:

  • name (Symbol)

    the name of the filter to look up

Returns:

  • (Proc)

    the filter implementation

Raises:



19
20
21
# File 'lib/cadenza/context/filters.rb', line 19

def lookup_filter(name)
   filters.fetch(name.to_sym) { raise FilterNotDefinedError.new("undefined filter '#{name}'") }
end