Class: AllSeeingEye::Model
- Inherits:
-
Object
- Object
- AllSeeingEye::Model
- Defined in:
- lib/all_seeing_eye/model.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#score ⇒ Object
Returns the value of attribute score.
Class Method Summary collapse
- .all(options = {}) ⇒ Object
- .count ⇒ Object
- .count_by_field(field, options = {}) ⇒ Object
- .create(fields) ⇒ Object
- .field_keys ⇒ Object
- .field_names ⇒ Object
- .fields ⇒ Object
- .find(id) ⇒ Object
- .find_by_field(field, options = {}) ⇒ Object
- .first ⇒ Object
- .last ⇒ Object
- .list_by_field(field, options = {}) ⇒ Object
- .load(object) ⇒ Object
- .model_name ⇒ Object
- .new(fields = {}) ⇒ Object
- .next_id ⇒ Object
- .search(query, options = {}) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Model
constructor
A new instance of Model.
- #save ⇒ Object
Constructor Details
#initialize ⇒ Model
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
#id ⇒ Object
Returns the value of attribute id.
2 3 4 |
# File 'lib/all_seeing_eye/model.rb', line 2 def id @id end |
#score ⇒ Object
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( = {}) 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 > [:start]} if [:start] all = all.find_all{|o| o.created_at < [:stop]} if [:stop] all end |
.count ⇒ Object
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, = {}) AllSeeingEye.redis.llen("allseeingeye:#{self.model_name}:fields:#{field}:#{[: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_keys ⇒ Object
16 17 18 |
# File 'lib/all_seeing_eye/model.rb', line 16 def self.field_keys self.fields.keys + ['time_spent'] end |
.field_names ⇒ Object
20 21 22 |
# File 'lib/all_seeing_eye/model.rb', line 20 def self.field_names (self.field_keys + ['created_at']).flatten end |
.fields ⇒ Object
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, = {}) if [:value] all = AllSeeingEye.redis.sort("allseeingeye:#{self.model_name}:fields:#{field}:#{[:value]}", :by => 'nosort', :get => "allseeingeye:#{self.model_name}:*", :limit => [:offset] && [:limit] ? [[:offset], [:limit]] : nil).collect{|o| self.load(o)} all = all.find_all{|o| o.created_at > [:start]} if [:start] all = all.find_all{|o| o.created_at < [:stop]} if [:stop] all else [:start] = [:start].to_i [:stop] = [:stop].to_i = {:start => '-inf', :stop => '+inf'}.merge() AllSeeingEye.redis.zrangebyscore("allseeingeye:#{self.model_name}:fields:#{field}", [:start], [:stop], :with_scores => false).collect{|id| self.find(id)} end end |
.first ⇒ Object
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 |
.last ⇒ Object
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, = {}) = {:start => '-inf', :stop => '+inf', :page => nil}.merge() limit = if [:page] [([:page] - 1) * 10, 10] else nil end raw_list, needs_pruning = if [:value] [AllSeeingEye.redis.zrangebyscore("allseeingeye:#{self.model_name}:fields:#{field}:#{[: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}", [:stop], [: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 > [:start]} if [:start] && [:start] != '-inf' list = list.find_all{|i| i.first.to_i < [:stop]} if [:stop] && [: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_name ⇒ Object
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_id ⇒ Object
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, = {}) 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 [: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 [:count] self.list_by_field($1, :value => $2) else self.find_by_field($1, :value => $2) end end end |
Instance Method Details
#save ⇒ Object
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 = (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, ) 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, ) 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 |