Class: AllSeeingEye::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/all_seeing_eye/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModel

Returns a new instance of Model.



132
133
134
135
136
137
# File 'lib/all_seeing_eye/model.rb', line 132

def initialize
  self.class.field_names.each do |key|
    self.class.send(:attr_accessor, key.to_sym)
  end
  self
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



2
3
4
# File 'lib/all_seeing_eye/model.rb', line 2

def id
  @id
end

#scoreObject

Returns the value of attribute score.



2
3
4
# File 'lib/all_seeing_eye/model.rb', line 2

def score
  @score
end

Class Method Details

.all(options = {}) ⇒ Object



43
44
45
46
47
48
# File 'lib/all_seeing_eye/model.rb', line 43

def self.all(options = {})
  all = AllSeeingEye.redis.sort("allseeingeye:#{self.model_name}:all", :by => 'nosort', :get => "allseeingeye:#{self.model_name}:*").collect{|o| self.load(o)}
  all = all.find_all{|o| o.created_at > options[:start]} if options[:start]
  all = all.find_all{|o| o.created_at < options[:stop]} if options[:stop]
  all
end

.countObject



50
51
52
# File 'lib/all_seeing_eye/model.rb', line 50

def self.count
  AllSeeingEye.redis.scard("allseeingeye:#{self.model_name}:all").to_i
end

.count_by_field(field, options = {}) ⇒ Object



54
55
56
# File 'lib/all_seeing_eye/model.rb', line 54

def self.count_by_field(field, options = {})
  AllSeeingEye.redis.llen("allseeingeye:#{self.model_name}:fields:#{field}:#{options[:value]}").to_i
end

.create(fields) ⇒ Object



30
31
32
# File 'lib/all_seeing_eye/model.rb', line 30

def self.create(fields)
  self.new(fields).save
end

.field_keysObject



16
17
18
# File 'lib/all_seeing_eye/model.rb', line 16

def self.field_keys
  self.fields.keys + ['time_spent']
end

.field_namesObject



20
21
22
# File 'lib/all_seeing_eye/model.rb', line 20

def self.field_names
  (self.field_keys + ['created_at']).flatten
end

.fieldsObject



12
13
14
# File 'lib/all_seeing_eye/model.rb', line 12

def self.fields
  AllSeeingEye.configuration[:all_seeing_eye][self.model_name]
end

.find(id) ⇒ Object



39
40
41
# File 'lib/all_seeing_eye/model.rb', line 39

def self.find(id)
  self.load(AllSeeingEye.redis["allseeingeye:#{self.model_name}:#{id}"])
end

.find_by_field(field, options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/all_seeing_eye/model.rb', line 58

def self.find_by_field(field, options = {})
  if options[:value]
    all = AllSeeingEye.redis.sort("allseeingeye:#{self.model_name}:fields:#{field}:#{options[:value]}", :by => 'nosort', :get => "allseeingeye:#{self.model_name}:*", :limit => options[:offset] && options[:limit] ? [options[:offset], options[:limit]] : nil).collect{|o| self.load(o)}
    all = all.find_all{|o| o.created_at > options[:start]} if options[:start]
    all = all.find_all{|o| o.created_at < options[:stop]} if options[:stop]
    all
  else
    options[:start] = options[:start].to_i
    options[:stop] = options[:stop].to_i
    options = {:start => '-inf', :stop => '+inf'}.merge(options)

    AllSeeingEye.redis.zrangebyscore("allseeingeye:#{self.model_name}:fields:#{field}", options[:start], options[:stop], :with_scores => false).collect{|id| self.find(id)}
  end
end

.firstObject



106
107
108
# File 'lib/all_seeing_eye/model.rb', line 106

def self.first
  self.find(AllSeeingEye.redis.zrange("allseeingeye:#{self.model_name}:fields:created_at", 0, 0))
end

.lastObject



110
111
112
# File 'lib/all_seeing_eye/model.rb', line 110

def self.last
  self.find(AllSeeingEye.redis.zrange("allseeingeye:#{self.model_name}:fields:created_at", -1, -1))
end

.list_by_field(field, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/all_seeing_eye/model.rb', line 73

def self.list_by_field(field, options = {})
  options = {:start => '-inf', :stop => '+inf', :page => nil}.merge(options)
  limit = if options[:page]
    [(options[:page] - 1) * 10, 10]
  else
    nil
  end

  raw_list, needs_pruning = if options[:value]
    [AllSeeingEye.redis.zrangebyscore("allseeingeye:#{self.model_name}:fields:#{field}:#{options[:value]}:count", '-inf', '+inf', :with_scores => true, :limit => limit), true]
  else
    if AllSeeingEye.redis.exists("allseeingeye:#{self.model_name}:fields:#{field}:count")
      [AllSeeingEye.redis.zrangebyscore("allseeingeye:#{self.model_name}:fields:#{field}:count", '-inf', '+inf', :with_scores => true, :limit => limit), true]
    else
      [AllSeeingEye.redis.zrevrangebyscore("allseeingeye:#{self.model_name}:fields:#{field}", options[:stop], options[:start], :with_scores => true, :limit => limit), false]
    end
  end
  
  list = []
  raw_list.each_with_index do |value, index|
    if index % 2 == 0
      list << [value]
    else
      list.last[1] = value.to_i
    end
  end
  if needs_pruning
    list = list.find_all{|i| i.first.to_i > options[:start]} if options[:start] && options[:start] != '-inf'
    list = list.find_all{|i| i.first.to_i < options[:stop]} if options[:stop] && options[:stop] != '+inf'
  end
  list.sort{|a, b| a.first.to_i == 0 ? b.last <=> a.last : a.first <=> b.first}
end

.load(object) ⇒ Object



34
35
36
37
# File 'lib/all_seeing_eye/model.rb', line 34

def self.load(object)
  object = Marshal.load(object)
  object.send(:initialize)
end

.model_nameObject



4
5
6
# File 'lib/all_seeing_eye/model.rb', line 4

def self.model_name
  self.to_s.split('::').last.downcase
end

.new(fields = {}) ⇒ Object



24
25
26
27
28
# File 'lib/all_seeing_eye/model.rb', line 24

def self.new(fields = {})
  object = super()
  fields.each {|key, value| object.send("#{key}=".to_sym, value)}
  object
end

.next_idObject



8
9
10
# File 'lib/all_seeing_eye/model.rb', line 8

def self.next_id
  AllSeeingEye.redis.incr("allseeingeye:#{self.model_name}:id")
end

.search(query, options = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/all_seeing_eye/model.rb', line 114

def self.search(query, options = {})
  if query =~ /^[\sa-zA-Z0-9,-]*? to [\sa-zA-Z0-9,-]*?$/
    start = Chronic.parse(query.split(' to ').first, :context => :past).to_i
    stop = Chronic.parse(query.split(' to ').last, :context => :past).to_i
    if options[:count]
      return self.list_by_field('created_at', :start => start, :stop => stop)
    else
      return AllSeeingEye.redis.zrangebyscore("allseeingeye:#{self.model_name}:fields:created_at", start, stop, :with_scores => false).collect{|id| self.find(id)}
    end
  elsif query =~ /^(#{self.field_names.join('|')}):(.*)$/
    if options[:count]
      self.list_by_field($1, :value => $2)
    else
      self.find_by_field($1, :value => $2)
    end
  end
end

Instance Method Details

#saveObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/all_seeing_eye/model.rb', line 143

def save
  timestamp = (self.created_at.to_i / AllSeeingEye.configuration[:all_seeing_eye]['round_to_seconds']) * AllSeeingEye.configuration[:all_seeing_eye]['round_to_seconds']
  
  AllSeeingEye.redis["allseeingeye:#{self.class.model_name}:#{id}"] = Marshal.dump(self)
  AllSeeingEye.redis.sadd("allseeingeye:#{self.class.model_name}:all", id)
  self.class.field_names.each do |field|
    value = self.send(field.to_sym)
    next if value.nil? || (value.respond_to?(:empty?) && value.empty?)
    if value.is_a?(Time) || value.is_a?(DateTime) || value.is_a?(Date)
      AllSeeingEye.redis.zincrby("allseeingeye:#{self.class.model_name}:fields:#{field}:count", 1, timestamp)
      AllSeeingEye.redis.zadd("allseeingeye:#{self.class.model_name}:fields:#{field}", value.to_i, id)
    else
      AllSeeingEye.redis.zincrby("allseeingeye:#{self.class.model_name}:fields:#{field}:#{value}:count", 1, timestamp)
      AllSeeingEye.redis.lpush("allseeingeye:#{self.class.model_name}:fields:#{field}:#{value}", id)
      AllSeeingEye.redis.zincrby("allseeingeye:#{self.class.model_name}:fields:#{field}", 1, value)
    end
  end
  self
end