Class: Wonkavision::Plugins::Aggregation::Dimension

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ Dimension

Returns a new instance of Dimension.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/wonkavision/plugins/analytics/aggregation/dimension.rb', line 10

def initialize(name,options={},&block)
  @name = name
  @options = options
  @attributes = HashWithIndifferentAccess.new
  @from = options[:from]
  key options[:key] if options[:key]
  sort options[:sort] if options[:sort]
  caption options[:caption] if options[:caption]
  self.instance_eval(&block) if block
  key name unless key
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/wonkavision/plugins/analytics/aggregation/dimension.rb', line 7

def attributes
  @attributes
end

#caption(caption_key = nil, options = {}) ⇒ Object



39
40
41
42
43
# File 'lib/wonkavision/plugins/analytics/aggregation/dimension.rb', line 39

def caption(caption_key=nil, options={})
  return @caption || @key unless caption_key
  attribute(caption_key, options) unless attributes[caption_key]
  @caption = caption_key
end

#fromObject (readonly)

Returns the value of attribute from.



7
8
9
# File 'lib/wonkavision/plugins/analytics/aggregation/dimension.rb', line 7

def from
  @from
end

#key(key = nil, options = {}) ⇒ Object



45
46
47
48
49
# File 'lib/wonkavision/plugins/analytics/aggregation/dimension.rb', line 45

def key(key=nil, options={})
  return @key unless key
  attribute(key, options) unless attributes[key]
  @key = key
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/wonkavision/plugins/analytics/aggregation/dimension.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/wonkavision/plugins/analytics/aggregation/dimension.rb', line 7

def options
  @options
end

#sort(sort_key = nil, options = {}) ⇒ Object Also known as: sort_by



32
33
34
35
36
# File 'lib/wonkavision/plugins/analytics/aggregation/dimension.rb', line 32

def sort(sort_key = nil, options={})
  return @sort || @key unless sort_key
  attribute(sort_key, options) unless attributes[sort_key]
  @sort = sort_key
end

Instance Method Details

#attribute(*attribute_list) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/wonkavision/plugins/analytics/aggregation/dimension.rb', line 22

def attribute(*attribute_list)
  raise "No attribute names were specified when calling '#attribute'" if
    attribute_list.blank?

  options = attribute_list.extract_options! || {}
  attribute_list.flatten.each do |attribute|
    @attributes[attribute] = Attribute.new(attribute,options)
  end
end

#extract(data) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/wonkavision/plugins/analytics/aggregation/dimension.rb', line 51

def extract(data)
  dimension_data = data[from.to_s] if from
  dimension_data ||= data[name.to_s] if data[name.to_s].kind_of?(Hash)
  dimension_data ||= data
  attributes.values.inject({}) do |message,attribute|
    message.tap { |m| m[attribute.name.to_s] = attribute.extract(dimension_data)};
  end
end