Module: Cubicle::Query::Dsl

Includes:
TimeIntelligence
Included in:
Cubicle::Query
Defined in:
lib/cubicle/query/dsl.rb,
lib/cubicle/query/dsl/time_intelligence.rb

Defined Under Namespace

Modules: TimeIntelligence

Instance Method Summary collapse

Methods included from TimeIntelligence

#from, #last, #last_complete, #mtd, #next, #this, #time_dimension, #time_range, #until, #ytd

Instance Method Details

#alias_member(alias_hash) ⇒ Object Also known as: alias_members



110
111
112
113
# File 'lib/cubicle/query/dsl.rb', line 110

def alias_member(alias_hash)
  alias_hash.each {|key,value|@query_aliases[value] = key}
  self
end

#by(*args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cubicle/query/dsl.rb', line 70

def by(*args)
  return @by unless args.length > 0
  #Resolve any query level aliases
  args = unalias(args)
  #We'll need these in the result set
  select *args
  #replace any alias names with actual member names
  @by = args.map{|member_name|@aggregation.find_member(member_name).name}
  return if @time_dimension #If a time dimension has been explicitly specified, the following isn't helpful.

  #Now let's see if we can find ourselves a time dimension
  if (@aggregation.time_dimension && time_dimension.included_in?(args))
    time_dimension(@aggregation.time_dimension)
  else
    args.each do |by_member|
      if (detected = detect_time_period by_member)
        time_dimension by_member
        @time_period = detected
        break
      end
    end
  end
end

#define(expression_name, value) ⇒ Object



116
117
118
# File 'lib/cubicle/query/dsl.rb', line 116

def define(expression_name,value)
  @named_expressions[expression_name] = value
end

#limit(in_limit = nil) ⇒ Object



57
58
59
60
61
# File 'lib/cubicle/query/dsl.rb', line 57

def limit(in_limit = nil)
  return @limit unless in_limit
  @limit = in_limit
  return self
end

#offset(in_offset = nil) ⇒ Object Also known as: skip



63
64
65
66
67
# File 'lib/cubicle/query/dsl.rb', line 63

def offset(in_offset = nil)
  return @offset unless in_offset
  @offset = in_offset
  return self
end

#order_by(*args) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/cubicle/query/dsl.rb', line 94

def order_by(*args)
  return @order_by unless args.length > 0
  args.each do |order|
    @order_by << (order.is_a?(Array) ? [unalias(order[0]),order[1]] : [unalias(order),:asc])
  end
  self
end

#select(*args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cubicle/query/dsl.rb', line 14

def select(*args)
  args = unalias(args[0].is_a?(Array) ? args[0] : args)
  if (args.include?(:all))
    select_all
    return
  end

  if (args.include?(:all_measures))
    @all_measures = true
    @measures = Cubicle::MemberList.new
  end
  if (args.include?(:all_dimensions))
    @all_dimensions = true
    @dimensions = Cubicle::MemberList.new
  end

  return if args.length == 1 && selected?(args[0])

  found=[:all_measures,:all_dimensions]

  if args.length == 1 && !all_dimensions? && args[0].is_a?(Cubicle::Dimension)
    @dimensions << convert_dimension(args.pop)
  elsif args.length == 1 && !all_measures? && args[0].is_a?(Cubicle::Measure)
    @measures << convert_measure(args.pop)
  else
    #remove from the list any dimensions or measures that are already
    #selected. This allows select to be idempotent,
    #which is useful for ensuring certain members are selected
    #even though the user may already have selected them previously
    args.each do |member_name|               
      if (member = @aggregation.dimensions[member_name])
        @dimensions << convert_dimension(member)
      elsif (member = @aggregation.measures[member_name])
        @measures << convert_measure(member)
      end
      found << member_name if member || selected?(member_name)
    end
  end
  args = args - found
  raise "You selected one or more members that do not exist in the underlying data source:#{args.inspect}" unless args.blank?
  self
end

#select_allObject



5
6
7
# File 'lib/cubicle/query/dsl.rb', line 5

def select_all
  select :all_dimensions, :all_measures
end

#transient!Object



9
10
11
12
# File 'lib/cubicle/query/dsl.rb', line 9

def transient!
  @transient = true
  @source_collection_name = nil
end

#where(filter = nil) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/cubicle/query/dsl.rb', line 102

def where(filter = nil)
  return prepare_filter unless filter
  filter.each do |key,value|
    (@where ||= {})[unalias(key)] = value
  end
  self
end