Class: ContextFilters::GlobalContext

Inherits:
Object
  • Object
show all
Defined in:
lib/context-filters/global_context.rb

Overview

builds list of filters and provides dsl for building nested context and allows evaluating filters on methods in the current context

Direct Known Subclasses

Context

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(priority_filters = nil, context = [], options = nil) ⇒ GlobalContext

initialize new GlobalContext, works in two modes:

  1. start totally new context, takes one param - array of priorities, nil to use one anonymous priority

  2. build sub context, params: global_filters_list, parents_context, value to add to context

Parameters:

  • priority_filters (Array, PriorityFilters) (defaults to: nil)

    when PriorityFilters - uses it for priority_filters otherwise - initializes new priority_filters with it

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

    parents context, duplicates to initialize own context

  • options (Object) (defaults to: nil)

    new context, ads it to current context



32
33
34
35
36
37
38
# File 'lib/context-filters/global_context.rb', line 32

def initialize(priority_filters = nil, context = [], options = nil)
  if ContextFilters::PriorityFilters === priority_filters
  then @priority_filters = priority_filters
  else @priority_filters = ContextFilters::PriorityFilters.new(priority_filters)
  end
  @context = context.dup + [options]
end

Instance Attribute Details

#contextArray (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the context stack.

Returns:

  • (Array)

    the context stack



15
16
17
# File 'lib/context-filters/global_context.rb', line 15

def context
  @context
end

#priority_filtersPriorityFilters (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns shared list of filters.

Returns:



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

def priority_filters
  @priority_filters
end

Instance Method Details

#evaluate_filters(target, method) { ... } ⇒ Object

evaluates all matching filters for given context, allows to do extra work for priority.nil? or on the end of the priorities,

Parameters:

  • method (Proc)

    the method to evaluate with filters matching current context

Yields:

  • on first priority.nil? or on the end when none



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/context-filters/global_context.rb', line 62

def evaluate_filters(target, method)
  local_called = false

  @priority_filters.each do |priority, filters|

    @context.each { |options| filters.apply(target, method, options) }

    if priority.nil? && block_given? && !local_called
      yield
      local_called = true
    end

  end

  yield if block_given? && !local_called
end

#filter(priority, options = nil, &block) ⇒ Object

defines new filter for given priority and options

Parameters:

  • priority (nil, Object)

    has to correspond to one of the initialized priorities

  • options (Object) (defaults to: nil)

    the options to use for new filter

  • block (Proc)

    the transformation to use when the options match



46
47
48
# File 'lib/context-filters/global_context.rb', line 46

def filter(priority, options = nil, &block)
  @priority_filters.store(priority, options, &block)
end

#in_context(options, &block) {|GlobalContext| ... } ⇒ Object

starts new context

Parameters:

  • options (Object)

    options to start new context

  • block (Proc)

    code block that will enable filtering for the given options

Yields:



54
55
56
# File 'lib/context-filters/global_context.rb', line 54

def in_context(options, &block)
  self.class.new(@priority_filters, @context, options).tap(&block)
end