Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added each, each_with_index and find to perform actions with segments.
- Loading branch information
Doug Youch
committed
May 20, 2010
1 parent
5de548e
commit 97dfafb
Showing
3 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters