Class: ActiveWarehouse::Fact

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/active_warehouse/fact.rb

Overview

Facts represent business measures. A row in a fact table corresponds to set of measurements in a particular granularity along with the foreign keys connecting the fact to various dimensions. All measurements in a fact table must be at the same grain.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.aggregate_field_optionsObject

Get a hash of all aggregate field options



12
13
14
# File 'lib/active_warehouse/fact.rb', line 12

def aggregate_field_options
  @aggregate_field_options
end

.aggregate_fieldsObject

Get a list of all aggregate fields



8
9
10
# File 'lib/active_warehouse/fact.rb', line 8

def aggregate_fields
  @aggregate_fields
end

.calculated_field_optionsObject

Get a hash of all calculated field options



19
20
21
# File 'lib/active_warehouse/fact.rb', line 19

def calculated_field_options
  @calculated_field_options
end

.calculated_fieldsObject

Get a list of all calculated fields



15
16
17
# File 'lib/active_warehouse/fact.rb', line 15

def calculated_fields
  @calculated_fields
end

Class Method Details

.calculated_field(field, options = {}, &block) ⇒ Object

Define a calculated field

  • field: The field name

  • options: An options hash

This method takes a block which will be passed the current aggregate record.

Example: calculated_field (:gross_margin) { |r| r.gross_profit_dollar_amount / r.sales_dollar_amount}



104
105
106
107
108
# File 'lib/active_warehouse/fact.rb', line 104

def calculated_field(field, options={}, &block)
  calculated_fields << field
  options[:block] = block
  calculated_field_options[field] = options
end

.class_for_name(name) ⇒ Object

Get the class for the specified fact name



76
77
78
# File 'lib/active_warehouse/fact.rb', line 76

def class_for_name(name)
  class_name(name).constantize
end

.class_name(name) ⇒ Object

Get the class name for the specified fact name



69
70
71
72
73
# File 'lib/active_warehouse/fact.rb', line 69

def class_name(name)
  fact_name = name.to_s
  fact_name = "#{fact_name}_facts" unless fact_name =~ /_fact[s?]$/
  fact_name.classify
end

.define_aggregate(field, options = {}) ⇒ Object Also known as: aggregate

Define an aggregate. Also aliased from aggregate()

  • field: The field name

  • options: A hash of options for the aggregate



90
91
92
93
94
# File 'lib/active_warehouse/fact.rb', line 90

def define_aggregate(field, options={})
  aggregate_fields << field
  options[:type] ||= :sum
  aggregate_field_options[field] = options
end

.dimensionsObject

Return a list of dimensions for this fact.

Example:

sales_fact

date_id
region_id
sales_amount
number_items_sold

Calling SalesFact.dimensions would return the list: [:date, :region]



32
33
34
# File 'lib/active_warehouse/fact.rb', line 32

def dimensions
  foreign_key_columns.collect { |c| c.name.gsub(/_id/, '').to_sym }
end

.foreign_key_columnsObject

Get all of the Column objects representing foreign key columns

Example:

sales_fact

date_id
region_id
sales_amount
number_items_sold

Calling SalesFact.foreign_key_columns would return a list of column objects containing the date column and the region column.



48
49
50
51
52
53
54
# File 'lib/active_warehouse/fact.rb', line 48

def foreign_key_columns
  fk_columns = []
  columns.each do |column|
    fk_columns << column if column.name =~ /(.*)_id/
  end
  fk_columns
end

.last_modifiedObject

Get the time when the fact source file was last modified



57
58
59
# File 'lib/active_warehouse/fact.rb', line 57

def last_modified
  File.new(__FILE__).mtime
end

.table_nameObject

Get the table name. The fact table name is pluralized



62
63
64
65
66
# File 'lib/active_warehouse/fact.rb', line 62

def table_name
  name = self.name.demodulize.underscore.pluralize
  set_table_name(name)
  name
end

.to_fact(dimension) ⇒ Object

Get the fact class for the specified value. The fact parameter may be a class, String or Symbol.



82
83
84
85
# File 'lib/active_warehouse/fact.rb', line 82

def to_fact(dimension)
  return dimension if dimension.is_a?(Class) and dimension.superclass == Fact
  return class_for_name(dimension)
end