Sha256: 3f371dc8cc3476e4c1d80cfb67521c01887cb5f8a3443bb18bcd6d259161bfdf

Contents?: true

Size: 1.91 KB

Versions: 5

Compression:

Stored size: 1.91 KB

Contents

class Whoops::Event
  include Mongoid::Document
  include FieldNames
  
  belongs_to :event_group, :class_name => "Whoops::EventGroup", :index=>true
  
  field :details
  field :keywords, :type => String
  field :message, :type => String
  field :event_time, :type => DateTime

  index([[:event_group_id,Mongo::ASCENDING],[:event_time, Mongo::DESCENDING]])

  validates_presence_of :message  
  
  before_save :set_keywords, :sanitize_details, :set_event_time
  
  def self.search(query)
    conditions = Whoops::MongoidSearchParser.new(query).conditions
    where(conditions)
  end
  
  def set_keywords
    keywords_array = []
    keywords_array << self.message
    add_details_to_keywords(keywords_array)
    self.keywords = keywords_array.join(" ")
  end
  
  def sanitize_details
    if details.is_a? Hash
      sanitized_details = {}
      details.each do |key, value|
        if key =~ /\./
          key = key.gsub(/\./, "_")
        end
        
        if value.is_a? Hash
          child_keys = all_keys([value])
          if child_keys.any?{ |child_key| child_key =~ /\./ } 
            value = value.to_s
          end
        end
        
        sanitized_details[key] = value
      end
      
      self.details = sanitized_details
    end
  end
  
  def all_keys(values)
    keys = []
    values.each do |value|
      if value.is_a? Hash
        keys |= value.keys
        keys |= all_keys(value.values)
      end
    end
    keys
  end
    
  private

  
  def add_details_to_keywords(keywords_array)
    flattened = details.to_a.flatten
    flattened -= details.keys if details.respond_to?(:keys)
    
    until flattened.empty?
      non_hash = flattened.select{ |i| !i.is_a?(Hash) }
      keywords_array.replace(keywords_array | non_hash)
      flattened -= non_hash
      
      flattened.collect! do |i|
        i.to_a.flatten - i.keys
      end.flatten!
    end
  end

  def set_event_time
    self.event_time = Time.now
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
whoops-0.5.5 app/models/whoops/event.rb
whoops-0.5.4 app/models/whoops/event.rb
whoops-0.5.3 app/models/whoops/event.rb
whoops-0.5.2 app/models/whoops/event.rb
whoops-0.5.1 app/models/whoops/event.rb