class Indy::Search

Attributes

end_time[RW]
inclusive[RW]
source[RW]
start_time[RW]

Public Class Methods

new(params_hash={}) click to toggle source
# File lib/indy/search.rb, line 9
def initialize(params_hash={})
  while (param = params_hash.shift) do
    send("#{param.first}=",param.last)
  end
end

Public Instance Methods

inside_time_window?(time_string,start_time,end_time,inclusive) click to toggle source

Evaluate if a log entry satisfies the configured time conditions

# File lib/indy/search.rb, line 147
def inside_time_window?(time_string,start_time,end_time,inclusive)
  time = Indy::Time.parse_date(time_string, @source.log_definition.time_format)
  return false unless time
  if inclusive
    return true unless (time > end_time || time < start_time)
  else
    return true unless (time >= end_time || time <= start_time)
  end
  return false
end
is_match?(type, result, search_criteria) click to toggle source

Evaluates if field => value criteria is an exact match on entry

@param [Hash] result The entry_hash @param [Hash] search_criteria The field => value criteria to match

# File lib/indy/search.rb, line 101
def is_match?(type, result, search_criteria)
  if type == :for
    search_criteria.reject {|criteria,value| result[criteria] == value }.empty?
  elsif type == :like
    search_criteria.reject {|criteria,value| result[criteria] =~ /#{value}/i }.empty?
  end
end
iterate_and_compare(type,search_criteria,&block) click to toggle source

Helper function called by Indy#for, Indy#like and Indy#all

@param [Symbol] type The symbol :for, :like or :all

@param [Hash] search_criteria the field to search for as the key and the

value to compare against the log entries.
# File lib/indy/search.rb, line 23
def iterate_and_compare(type,search_criteria,&block)
  results = []
  results += search do |entry|
    if type == :all || is_match?(type,entry,search_criteria)
      result_struct = @source.log_definition.create_struct(entry)
      if block_given?
        block.call(result_struct)
      else
        result_struct
      end
    end
  end
  results.compact
end
reset_scope() click to toggle source

Clear time scope settings

# File lib/indy/search.rb, line 140
def reset_scope
  @inclusive = @start_time = @end_time = nil
end
time_scope(params_hash) click to toggle source

Parse hash to set @start_time, @end_time and @inclusive

# File lib/indy/search.rb, line 112
def time_scope(params_hash)
  if params_hash[:time]
    time_scope_from_direction(params_hash[:direction], params_hash[:span], params_hash[:time])
  else
    @start_time = Indy::Time.parse_date(params_hash[:start_time], @source.log_definition.time_format) if params_hash[:start_time]
    @end_time = Indy::Time.parse_date(params_hash[:end_time], @source.log_definition.time_format) if params_hash[:end_time]
  end
  @inclusive = params_hash[:inclusive]
end
time_scope_from_direction(direction, span, time) click to toggle source

Parse direction, span, and time to set @start_time and @end_time

# File lib/indy/search.rb, line 125
def time_scope_from_direction(direction, span, time)
  time = Indy::Time.parse_date(time, @source.log_definition.time_format)
  span = (span.to_i * 60).seconds if span
  if direction == :before
    @end_time = time
    @start_time = time - span if span
  elsif direction == :after
    @start_time = time
    @end_time = time + span if span
  end
end
use_time_criteria?() click to toggle source

Return true if start or end time has been set, and a :time field exists

# File lib/indy/search.rb, line 86
def use_time_criteria?
  if @start_time || @end_time
    # ensure both boundaries are set
    @start_time ||= Indy::Time.forever_ago(@source.log_definition.time_format)
    @end_time ||= Indy::Time.forever(@source.log_definition.time_format)
  end
  @start_time && @end_time
end