Class: Chicago::StarSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/chicago/star_schema.rb

Overview

A collection of facts & dimensions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStarSchema

Creates a new star schema.



27
28
29
30
# File 'lib/chicago/star_schema.rb', line 27

def initialize
  @dimensions = Schema::NamedElementCollection.new
  @facts = Schema::NamedElementCollection.new
end

Instance Attribute Details

#dimensionsChicago::Schema::Dimension (readonly)

a collection of all the dimensions defined in this schema.



24
25
26
# File 'lib/chicago/star_schema.rb', line 24

def dimensions
  @dimensions
end

#factsChicago::Schema::Fact (readonly)

A collection of all the facts defined in this schema.



20
21
22
# File 'lib/chicago/star_schema.rb', line 20

def facts
  @facts
end

Instance Method Details

#add(schema_table) ⇒ Object

Adds a prebuilt schema table to the schema

Schema tables may not be dupliates of already present tables in the schema.

TODO: figure out how to deal with linked dimensions when adding facts.



62
63
64
65
66
67
68
69
70
# File 'lib/chicago/star_schema.rb', line 62

def add(schema_table)
  if schema_table.kind_of? Schema::Fact
    collection = @facts
  elsif schema_table.kind_of? Schema::Dimension
    collection = @dimensions
  end
  
  add_to_collection collection, schema_table
end

#define_dimension(name, &block) ⇒ Chicago::Schema::Dimension

Defines a dimension table named name in this schema.

For example:

@schema.define_dimension(:date) do
  columns do
    date   :date
    year   :year
    string :month
    ...
  end

  natural_key :date
  null_record :id => 1, :month => "Unknown Month"
end

Returns:

See Also:



99
100
101
# File 'lib/chicago/star_schema.rb', line 99

def define_dimension(name, &block)
  add Schema::Builders::DimensionBuilder.new(self).build(name, &block)
end

#define_fact(name, &block) ⇒ Chicago::Schema::Fact

Defines a fact table named name in this schema.

Returns:

Raises:

  • Chicago::MissingDefinitionError

See Also:



77
78
79
# File 'lib/chicago/star_schema.rb', line 77

def define_fact(name, &block)
  add Schema::Builders::FactBuilder.new(self).build(name, &block)
end

#define_shrunken_dimension(name, base_name, &block) ⇒ Chicago::Schema::Dimension

Defines a shrunken dimension table named name in this schema.

base_name is the name of the base dimension that the shrunken dimension is derived from; this base dimention must already be defined.

Returns:

Raises:

See Also:



112
113
114
115
# File 'lib/chicago/star_schema.rb', line 112

def define_shrunken_dimension(name, base_name, &block)
  add Schema::Builders::ShrunkenDimensionBuilder.new(self, base_name).
    build(name, &block)
end

#dimension(name) ⇒ Chicago::Schema::Dimension

Returns a dimension, named name

Parameters:

  • name (Symbol)

    the name of the dimension

Returns:



44
45
46
# File 'lib/chicago/star_schema.rb', line 44

def dimension(name)
  @dimensions[name]
end

#fact(name) ⇒ Chicago::Schema::Fact

Returns a fact, named name

Parameters:

  • name (Symbol)

    the name of the fact

Returns:



36
37
38
# File 'lib/chicago/star_schema.rb', line 36

def fact(name)
  @facts[name]
end

#tablesArray

Returns all dimensions and facts in this schema.

Returns:

  • (Array)


51
52
53
# File 'lib/chicago/star_schema.rb', line 51

def tables
  @dimensions.to_a + @facts.to_a
end