Class: Appstats::Entry

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/appstats/entry.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_from_logger(action, contexts = {}) ⇒ Object



73
74
75
76
# File 'lib/appstats/entry.rb', line 73

def self.create_from_logger(action,contexts = {})
  return false if action.nil? || action.blank?
  create_from_logger_string(Logger.entry_to_s(action,contexts))
end

.create_from_logger_file(filename) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/appstats/entry.rb', line 43

def self.create_from_logger_file(filename)
  return false if filename.nil?
  return false unless File.exists?(filename)
  File.open(filename,"r").readlines.each do |line|
    create_from_logger_string(line.strip)
  end
  true
end

.create_from_logger_string(action_and_contexts) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/appstats/entry.rb', line 52

def self.create_from_logger_string(action_and_contexts)
  return false if action_and_contexts.nil? || action_and_contexts == ''
  hash = Logger.entry_to_hash(action_and_contexts)
  action_name = hash[:action].kind_of?(Array) ? hash[:action][0] : hash[:action]
  entry = Appstats::Entry.new(:action => action_name, :raw_entry => action_and_contexts)
  entry.occurred_at = Time.parse(hash[:timestamp]) unless hash[:timestamp].nil?
  hash.each do |key,value|
    next if key == :timestamp
    all_values = value.kind_of?(Array) ? value : [value]
    if key == :action
      all_values = all_values[1..-1]
    end
    all_values.each do |v|
      context = Appstats::Context.create(:context_key => key.to_s, :context_value => v)
      entry.contexts<< context
    end
  end
  entry.save
  entry
end

Instance Method Details

#occurred_at=(value) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/appstats/entry.rb', line 14

def occurred_at=(value)
  self[:occurred_at] = value
  if value.nil?
    self[:year] = nil
    self[:month] = nil
    self[:day] = nil
    self[:hour] = nil
    self[:min] = nil
    self[:sec] = nil
    self[:week] = nil
    self[:quarter] = nil
  else
    self[:year] = value.year
    self[:month] = value.month
    self[:day] = value.day
    self[:hour] = value.hour
    self[:min] = value.min
    self[:sec] = value.sec
    self[:week] = EntryDate.calculate_week_of(value)
    self[:quarter] = EntryDate.calculate_quarter_of(value)
  end
end

#to_sObject



37
38
39
40
41
# File 'lib/appstats/entry.rb', line 37

def to_s
  return "No Entry" if action.nil? || action == ''
  return action if occurred_at.nil?
  "#{action} at #{occurred_at.strftime('%Y-%m-%d %H:%M:%S')}"
end