Class: Chicago::Schema::Fact

Inherits:
Table
  • Object
show all
Defined in:
lib/chicago/schema/fact.rb

Overview

A fact table in the star schema.

Facts contain keys to dimension tables, ad-hoc fields used for filtering and grouping (termed degenerate dimensions), and measures, which are values to be summed, averaged etc.

You shouldn’t need to initialize a Fact yourself - they should be created via StarSchema#define_fact.

Instance Attribute Summary collapse

Attributes inherited from Table

#description, #natural_key, #table_name

Attributes included from NamedElement

#label, #name

Instance Method Summary collapse

Methods inherited from Table

#[], #qualify

Constructor Details

#initialize(name, opts = {}) ⇒ Fact

Creates a new fact.

Parameters:

  • name

    the name of this fact.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • dimensions (Object)

    an array of Dimensions this fact is linked with

  • degenerate_dimensions (Object)

    an array of Columns

  • measures (Object)

    an array of Measures

  • natual_key (Object)

    an array of symbols, representing a uniqueness constraint on the fact

  • description (Object)

    a long text description about the fact.



44
45
46
47
48
49
50
# File 'lib/chicago/schema/fact.rb', line 44

def initialize(name, opts={})
  super
  @dimensions = opts[:dimensions] || []
  @degenerate_dimensions = opts[:degenerate_dimensions] || []
  @measures = opts[:measures] || []
  @table_name = :"facts_#{@name}"
end

Instance Attribute Details

#degenerate_dimensionsObject (readonly)

The degenerate dimensions associated with this fact.

Degenerate dimensions are typically ids / numbers from a source system that have no associated information, for example: an order number. They are used for filtering and grouping facts.



27
28
29
# File 'lib/chicago/schema/fact.rb', line 27

def degenerate_dimensions
  @degenerate_dimensions
end

#dimensionsObject (readonly)

The Dimensions associated with this fact.

See Dimension.



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

def dimensions
  @dimensions
end

#measuresObject (readonly)

The measures associated with this fact.

Measures are usually numeric values that will be aggregated, for example the amount of a sale.



33
34
35
# File 'lib/chicago/schema/fact.rb', line 33

def measures
  @measures
end

Instance Method Details

#columnsObject

Returns an Array of all dimensions, degenerate_dimensions and measures for this fact table.



54
55
56
# File 'lib/chicago/schema/fact.rb', line 54

def columns
  @dimensions + @degenerate_dimensions + @measures
end

#factless?Boolean

A Factless Fact table has no measures - it used only to express a relationship between a set of dimensions.

Returns:

  • (Boolean)


60
61
62
# File 'lib/chicago/schema/fact.rb', line 60

def factless?
  @measures.empty?
end

#visit(visitor) ⇒ Object

Facts accept Visitors



65
66
67
# File 'lib/chicago/schema/fact.rb', line 65

def visit(visitor)
  visitor.visit_fact(self)
end