Class: Cubicle::Duration

Inherits:
Measure show all
Defined in:
lib/cubicle/duration.rb

Instance Attribute Summary collapse

Attributes inherited from Measure

#aggregation_method

Attributes inherited from Member

#alias_list, #expression_type, #field_name, #name, #options

Instance Method Summary collapse

Methods inherited from Measure

#aggregate, #distinct_count?, #finalize_aggregation

Methods inherited from Member

#included_in?, #matches, #to_js_keys, #to_js_value

Constructor Details

#initialize(*args) ⇒ Duration

Returns a new instance of Duration.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/cubicle/duration.rb', line 5

def initialize(*args)
  super
  self.duration_unit = options(:in) || :seconds
  self.timestamp_prefix = options :timestamp_prefix, :prefix
  self.expression_type = :javascript
  #only one item should be left in the hash, the duration map
  raise "duration must be provided with a hash with a single entry, where the key represents the starting milestone of a duration and the value represents the ending milestone." if options.length != 1

  self.begin_milestone, self.end_milestone = options.to_a[0]
  self.name ||= "#{begin_milestone}_to_#{end_milestone}_#{aggregation_method}".to_sym
end

Instance Attribute Details

#begin_milestoneObject

Returns the value of attribute begin_milestone.



3
4
5
# File 'lib/cubicle/duration.rb', line 3

def begin_milestone
  @begin_milestone
end

#duration_unitObject

Returns the value of attribute duration_unit.



3
4
5
# File 'lib/cubicle/duration.rb', line 3

def duration_unit
  @duration_unit
end

#end_milestoneObject

Returns the value of attribute end_milestone.



3
4
5
# File 'lib/cubicle/duration.rb', line 3

def end_milestone
  @end_milestone
end

#timestamp_prefixObject

Returns the value of attribute timestamp_prefix.



3
4
5
# File 'lib/cubicle/duration.rb', line 3

def timestamp_prefix
  @timestamp_prefix
end

Instance Method Details

#conditionObject



21
22
23
24
# File 'lib/cubicle/duration.rb', line 21

def condition
  cond = " && (#{super})" unless super.blank?
  "#{milestone_js(:begin)} && #{milestone_js(:end)}#{cond}"
end

#default_aggregation_methodObject



17
18
19
# File 'lib/cubicle/duration.rb', line 17

def default_aggregation_method
  :average
end

#expressionObject



26
27
28
29
30
31
# File 'lib/cubicle/duration.rb', line 26

def expression
  #prefix these names for the expression
#      prefix = "#{self.timestamp_prefix}#{self.timestamp_prefix.blank? ? '' : '.'}"
#      ms1,ms2 = [self.begin_milestone,self.end_milestone].map{|ms|ms.to_s=='now' ? "new Date(#{Time.now.to_i*1000})" : "this.#{prefix}#{ms}"}
  @expression = "(#{milestone_js(:end)}-#{milestone_js(:begin)})/#{denominator}" 
end