Class: Wonkavision::Analytics::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/wonkavision/plugins/analytics/query.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQuery

Returns a new instance of Query.



6
7
8
9
10
# File 'lib/wonkavision/plugins/analytics/query.rb', line 6

def initialize()
  @axes = []
  @slicer = Set.new
  @filters = []
end

Instance Attribute Details

#axesObject (readonly)

Returns the value of attribute axes.



4
5
6
# File 'lib/wonkavision/plugins/analytics/query.rb', line 4

def axes
  @axes
end

#filtersObject (readonly)

Returns the value of attribute filters.



4
5
6
# File 'lib/wonkavision/plugins/analytics/query.rb', line 4

def filters
  @filters
end

Class Method Details

.axis_ordinal(axis_def) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/wonkavision/plugins/analytics/query.rb', line 63

def self.axis_ordinal(axis_def)
  case axis_def.to_s.strip.downcase.to_s
  when "columns" then 0
  when "rows" then 1
  when "pages" then 2
  when "chapters" then 3
  when "sections" then 4
  else axis_def.to_i
  end
end

Instance Method Details

#all_dimensions?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/wonkavision/plugins/analytics/query.rb', line 46

def all_dimensions?
  axes.empty?
end

#all_filters_applied?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/wonkavision/plugins/analytics/query.rb', line 55

def all_filters_applied?
  @all_filters_applied ||= !(filters.detect{ |filter| !filter.applied? })
end

#matches_filter?(aggregation, tuple) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/wonkavision/plugins/analytics/query.rb', line 50

def matches_filter?(aggregation, tuple)
  return true if all_filters_applied?
  !( filters.detect{ |filter| !filter.matches(aggregation, tuple) } )
end

#referenced_dimensionsObject



36
37
38
# File 'lib/wonkavision/plugins/analytics/query.rb', line 36

def referenced_dimensions
  ( [] + selected_dimensions + slicer ).compact
end

#select(*dimensions) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/wonkavision/plugins/analytics/query.rb', line 12

def select(*dimensions)
  options = dimensions.extract_options!
  axis = options[:axis] || options[:on]
  axis_ordinal = self.class.axis_ordinal(axis)
  @axes[axis_ordinal] = dimensions
  self
end

#selected_dimensionsObject



40
41
42
43
44
# File 'lib/wonkavision/plugins/analytics/query.rb', line 40

def selected_dimensions
  dimensions = []
  axes.each { |dims|dimensions.concat(dims) unless dims.blank? }
  dimensions.uniq.compact
end

#slicerObject



31
32
33
34
# File 'lib/wonkavision/plugins/analytics/query.rb', line 31

def slicer
  dims = selected_dimensions
  @slicer.reject{|m|dims.include?(m)}
end

#validate!Object



59
60
61
# File 'lib/wonkavision/plugins/analytics/query.rb', line 59

def validate!
  axes.each_with_index{|axis,index|raise "Axes must be selected from in consecutive order and contain at least one dimension. Axis #{index} is blank." if axis.blank?}
end

#where(criteria_hash = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/wonkavision/plugins/analytics/query.rb', line 20

def where(criteria_hash = {})
  criteria_hash.each_pair do |filter,value|
    member_filter = filter.kind_of?(MemberFilter) ? filter :
      MemberFilter.new(filter)
    member_filter.value = value
    @filters << member_filter
    @slicer << member_filter.name if member_filter.dimension?
  end
  self
end