Skip to content

Commit

Permalink
Added each, each_with_index and find to perform actions with segments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Youch committed May 20, 2010
1 parent 5de548e commit 97dfafb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
54 changes: 50 additions & 4 deletions app/models/user_segment.rb
@@ -1,27 +1,73 @@

class UserSegment < DomainModel

has_many :user_segment_caches, :order => 'created_at, id', :dependent => :destroy, :class_name => 'UserSegmentCache'
serialize :segment_options
serialize :fields

validates_presence_of :name

def operations
return @operations if @operations
@operations = UserSegment::Operations.new
@operations.operations = self.segment_options if self.segment_options
@operations
end

def operations=(text)
def segment_options_text=(text)
self.write_attribute :segment_options_text, text
@operations = UserSegment::Operations.new
@operations.parse text
self.segment_options = @operations.valid? ? self.operations.to_a : nil
text
end

def before_create
def cache_ids
self.user_segment_caches.delete_all

self.order_by = 'created_at DESC' unless self.order_by
ids = EndUser.find(:all, :select => 'id', :conditions => {:id => self.operations.end_user_ids}, :order => self.order_by)

num_segements = (self.operations.end_user_ids.length / 1000)
num_segements = num_segements + 1 if (self.operations.end_user_ids.length % 1000) > 0

(0..num_segements-1).each do |idx|
start = idx * 1000
self.user_segment_caches.create :id_list => ids[start..start+999]
end

self.last_ran_at = Time.now
self.last_count = ids.length
self.save
end

def end_user_ids
return @end_user_ids if @end_user_ids
@end_user_ids = []
self.user_segment_caches.each do |segement|
@end_user_ids = @end_user_ids + segement.id_list
end
@end_user_ids
end

def each(&block)
self.user_segment_caches.each do |segement|
segement.each &block
end
end

def each_with_index(&block)
idx = 0
self.user_segment_caches.each do |segement|
idx = segement.each_with_index idx, &block
end
end

def before_save
self.segment_options = self.operations.to_a if self.operations && self.operations.valid?
def find(&block)
self.user_segment_caches.each do |segement|
user = segement.find &block
return user if user
end
end
end

25 changes: 25 additions & 0 deletions app/models/user_segment_cache.rb
@@ -1,4 +1,29 @@

class UserSegmentCache < DomainModel
belongs_to :user_segment

serialize :id_list

validates_presence_of :user_segment_id

def before_create
self.created_at = Time.now unless self.created_at
end

def end_users
@end_users ||= EndUser.find(:all, :conditions => {:id => self.id_list})
end

def each
self.end_users.each { |user| yield user }
end

def each_with_index(idx=0)
self.end_users.each { |user| yield user, idx; idx = idx.succ }
idx
end

def find
self.end_users.find { |user| yield user }
end
end
4 changes: 4 additions & 0 deletions db/migrate/20100511165552_user_segments.rb
Expand Up @@ -20,6 +20,8 @@ def self.up
t.datetime :created_at
end

add_index :user_segment_caches, [:user_segment_id], :name => 'user_segment_cache_segment_idx'

create_table :user_segment_analytics, :force => true do |t|
t.integer :user_segment_id
t.text :fields
Expand All @@ -29,6 +31,8 @@ def self.up
t.string :step
t.timestamps
end

add_index :user_segment_analytics, [:user_segment_id], :name => 'user_segment_analytics_segment_idx'
end

def self.down
Expand Down

0 comments on commit 97dfafb

Please sign in to comment.