lib/legato/query.rb in legato-0.0.10 vs lib/legato/query.rb in legato-0.1.0
- old
+ new
@@ -9,10 +9,16 @@
(class << self; self; end).instance_eval do
define_method(name) {|*args| apply_filter(*args, &block)}
end
end
+ def define_segment_filter(name, &block)
+ (class << self; self; end).instance_eval do
+ define_method(name) {|*args| apply_segment_filter(*args, &block)}
+ end
+ end
+
def self.define_filter_operators(*methods)
methods.each do |method|
class_eval <<-CODE
def #{method}(field, value, join_character=nil)
Filter.new(field, :#{method}, value, join_character)
@@ -22,42 +28,50 @@
end
attr_reader :parent_klass
attr_accessor :profile, :start_date, :end_date
attr_accessor :sort, :limit, :offset, :quota_user #, :segment # individual, overwritten
- attr_accessor :filters # appended to, may add :segments later for dynamic segments
+ attr_accessor :filters, :segment_filters # combined, can be appended to
def initialize(klass)
@loaded = false
@parent_klass = klass
self.filters = FilterSet.new
+ self.segment_filters = FilterSet.new
self.start_date = Time.now - MONTH
self.end_date = Time.now
klass.filters.each do |name, block|
define_filter(name, &block)
end
- # may add later for dynamic segments
- # klass.segment_definitions.each do |name, segment|
- # self.class.define_segment(name, segment)
- # end
+ klass.segments.each do |name, block|
+ define_segment_filter(name, &block)
+ end
end
def instance_klass
@parent_klass.instance_klass
end
def apply_filter(*args, &block)
+ apply_filter_expression(self.filters, *args, &block)
+ end
+
+ def apply_segment_filter(*args, &block)
+ apply_filter_expression(self.segment_filters, *args, &block)
+ end
+
+ def apply_filter_expression(filter_set, *args, &block)
@profile = extract_profile(args)
join_character = Legato.and_join_character # filters are joined by AND
# # block returns one filter or an array of filters
Array.wrap(instance_exec(*args, &block)).each do |filter|
filter.join_character ||= join_character # only set when not set explicitly
- self.filters << filter
+ filter_set << filter
join_character = Legato.or_join_character # arrays are joined by OR
end
self
end
@@ -167,10 +181,14 @@
def sort=(arr)
@sort = Legato::ListParameter.new(:sort, arr)
end
+ def segment
+ "dynamic::#{segment_filters.to_params}" if segment_filters.any?
+ end
+
# def segment_id
# segment.nil? ? nil : "gaid::#{segment}"
# end
def profile_id
@@ -182,10 +200,10 @@
'ids' => profile_id,
'start-date' => Legato.format_time(start_date),
'end-date' => Legato.format_time(end_date),
'max-results' => limit,
'start-index' => offset,
- # 'segment' => segment_id,
+ 'segment' => segment,
'filters' => filters.to_params, # defaults to AND filtering
'fields' => REQUEST_FIELDS,
'quotaUser' => quota_user
}