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)



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

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

Yields:

  • (String, *args)

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



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.

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

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