class Torque::FieldFilter

Parses and stores a field filter for stories

Attributes

contents[R]

The contents of the filter

field[R]

The field that the FieldFilter filters over

Public Class Methods

new(field, contents="") click to toggle source

@param field A symbol representing the field to filter @option field [Symbol] :label @option field [Symbol] :owner @option field [Symbol] :type

@param contents The contents of the field filter

The contents should be a list of values speparated by “,” or “+”, where AND is signified by “+” and OR is signified by “,”, and “+” has a higher precedence than “,”. For example,

“ios+android,ios+web”

translates to

(ios AND android) OR (ios AND web)

An :owner filter with no spaces in it will filter separately by first/middle/last name. For instance, “Adam” would match “Adam Barnes” or “John Adam Smith” or “Joe Quincy Adam”.

# File lib/torque/field_filter.rb, line 35
def initialize(field, contents="")
  @field = field
  @contents = contents

  # Represents evaluator as a 2-level array, @evaluator
  #
  # (ios AND android) OR (ios AND web) <=>
  # [["ios", "android"], ["ios", "web"]]
  parse_contents
end

Public Instance Methods

eval(story) click to toggle source

Returns true if the filter includes the story, false if it excludes it

# File lib/torque/field_filter.rb, line 48
def eval(story)

  # Determines the field to evaluate

  story_field = nil
  if @field == :label
    story_field = story.labels
  elsif @field == :owner
    story_field = story.owner
  elsif @field == :type
    story_field = story.type
  else
    # Invalid filter
    return true
  end

  # Evaluates the story's field

  or_eval = false
  @evaluator.each do |or_element|
    
    and_eval = true
    or_element.each do |and_element|

      if @field == :label
        and_eval &&= (story_field.member? and_element) 

      elsif @field == :owner
        # Special case: "owner" filter
        # If the filter element has no spaces in it...
        # Then it is assumed to be a first/middle/last name, not a full name
        if !and_element.match(" ")
          and_eval &&= (story_field.split(" ").member? and_element)
        else
          and_eval &&= (story_field == and_element)
        end

      else # @field == :type
        and_eval &&= (story_field == and_element)
      end

    end

    or_eval ||= and_eval
  end

  or_eval

end
to_s() click to toggle source

Returns a string representation of the FieldFilter

# File lib/torque/field_filter.rb, line 100
def to_s
  "#{@field} = #{@contents}"
end

Private Instance Methods

parse_contents() click to toggle source

Parses @contents and stores the result in @evaluator, a 2-level array Returns @evaluator

# File lib/torque/field_filter.rb, line 109
def parse_contents

  # Handles special case: empty contents
  if @contents == ""
    @evaluator = [[]]
    return @evaluator
  end
  
  # Splits all strings into arrays
  @evaluator = contents
  @evaluator = @evaluator.split ","
  @evaluator.length.times do |i|
    @evaluator[i] = @evaluator[i].split "+"
  end

  # Strips all array elements
  @evaluator.each do |element1|
    element1.each do |element2|
      element2.strip!
    end
  end

  @evaluator
end