module Staccato::Measurable

Measurable adds field mapping and param collection

for Measurement classes to be add to Hit

Public Class Methods

included(model) click to toggle source
# File lib/staccato/measurable.rb, line 5
def self.included(model)
  model.extend Forwardable

  model.class_eval do
    attr_accessor :options

    def_delegators :@options, *model::FIELDS.keys
  end
end
new(options = {}) click to toggle source

@param options [Hash] options for the measurement fields

# File lib/staccato/measurable.rb, line 16
def initialize(options = {})
  self.options = OptionSet.new(options)
end

Public Instance Methods

add_custom_dimension(dimension_index, value) click to toggle source

Set a custom dimension value at an index @param dimension_index [Integer] @param value

# File lib/staccato/measurable.rb, line 49
def add_custom_dimension(dimension_index, value)
  return unless custom_fields_allowed?
  self.custom_dimensions["#{prefix}cd#{dimension_index}"] = value
end
add_custom_metric(metric_index, value) click to toggle source

Set a custom metric value at an index @param metric_index [Integer] @param value

# File lib/staccato/measurable.rb, line 63
def add_custom_metric(metric_index, value)
  return unless custom_fields_allowed?
  self.custom_metrics["#{prefix}cm#{metric_index}"] = value
end
custom_dimensions() click to toggle source

Custom dimensions for this measurable @return [Hash]

# File lib/staccato/measurable.rb, line 56
def custom_dimensions
  @custom_dimensions ||= {}
end
custom_fields_allowed?() click to toggle source

not all measurements allow custom dimensions or metrics

# File lib/staccato/measurable.rb, line 32
def custom_fields_allowed?
  false
end
custom_metrics() click to toggle source

Custom metrics for this measurable @return [Hash]

# File lib/staccato/measurable.rb, line 70
def custom_metrics
  @custom_metrics ||= {}
end
fields() click to toggle source

fields from options for this measurement

# File lib/staccato/measurable.rb, line 21
def fields
  self.class::FIELDS
end
params() click to toggle source

collects the parameters from options for this measurement @return [Hash]

# File lib/staccato/measurable.rb, line 38
def params
  {}.
  merge!(measurable_params).
  merge!(custom_dimensions).
  merge!(custom_metrics).
  reject {|_,v| v.nil?}
end
prefix() click to toggle source

measurement option prefix @return [String]

# File lib/staccato/measurable.rb, line 27
def prefix
  ''
end

Private Instance Methods

measurable_params() click to toggle source

@private

# File lib/staccato/measurable.rb, line 77
def measurable_params
  Hash[
    fields.map { |field,key|
      next if key.nil?
      key = (prefix+key.to_s)

      [key, options[field]] unless options[field].nil?
    }.compact
  ]
end