Class: Chicago::Schema::Dimension
- Defined in:
- lib/chicago/schema/dimension.rb
Overview
A dimension in the star schema.
Dimensions contain denormalized values from various source systems, and are used to group and filter the fact tables. They may also be queried themselves.
You shouldn’t need to initialize a Dimension yourself - they should be created via StarSchema#define_dimension.
Instance Attribute Summary collapse
-
#columns ⇒ Object
(also: #column_definitions)
readonly
Returns an array of Columns defined on this dimension.
-
#identifiers ⇒ Object
readonly
Returns all the human-friendly identifying columns for this dimension.
-
#key_table_name ⇒ Object
readonly
The table used to generate/store dimension keys.
-
#null_records ⇒ Object
readonly
Records representing missing or not applicable dimension values.
Attributes inherited from Table
#description, #natural_key, #table_name
Attributes included from NamedElement
Instance Method Summary collapse
-
#create_null_records(db, overridden_table_name = nil) ⇒ Object
Creates null records in a Database.
-
#has_predetermined_values? ⇒ Boolean
Returns true if the set of values for this dimension is pretermined.
-
#identifiable? ⇒ Boolean
Returns true if this dimension can be identified as a concrete entity, with an original_id from a source system.
-
#initialize(name, opts = {}) ⇒ Dimension
constructor
Creates a new Dimension, named
name. -
#main_identifier ⇒ Object
Returns the main identifier for this record.
-
#original_key ⇒ Object
Returns the column that represents the id in the original source for the dimension.
-
#visit(visitor) ⇒ Object
Dimensions accept Visitors.
Methods inherited from Table
Constructor Details
#initialize(name, opts = {}) ⇒ Dimension
Creates a new Dimension, named name.
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/chicago/schema/dimension.rb', line 56 def initialize(name, opts={}) super @columns = opts[:columns] || [] @identifiers = opts[:identifiers] || [] @null_records = opts[:null_records] || [] @table_name = sprintf(DIMENSION_TABLE_FORMAT, name).to_sym @key_table_name = sprintf(KEY_TABLE_FORMAT, @table_name).to_sym @predetermined_values = !! opts[:predetermined_values] check_null_records end |
Instance Attribute Details
#columns ⇒ Object (readonly) Also known as: column_definitions
Returns an array of Columns defined on this dimension.
25 26 27 |
# File 'lib/chicago/schema/dimension.rb', line 25 def columns @columns end |
#identifiers ⇒ Object (readonly)
Returns all the human-friendly identifying columns for this dimension.
There is no expectation that identifying values will be unique, but they are intended to identify a single record in a user friendly way.
36 37 38 |
# File 'lib/chicago/schema/dimension.rb', line 36 def identifiers @identifiers end |
#key_table_name ⇒ Object (readonly)
The table used to generate/store dimension keys.
39 40 41 |
# File 'lib/chicago/schema/dimension.rb', line 39 def key_table_name @key_table_name end |
#null_records ⇒ Object (readonly)
Records representing missing or not applicable dimension values.
42 43 44 |
# File 'lib/chicago/schema/dimension.rb', line 42 def null_records @null_records end |
Instance Method Details
#create_null_records(db, overridden_table_name = nil) ⇒ Object
Creates null records in a Database.
This will overwrite any records that share the id with the null record, so be careful.
Optionally provide an overridden table name, if you need to create null records for a temporary version of the table.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/chicago/schema/dimension.rb', line 74 def create_null_records(db, overridden_table_name=nil) table_to_populate = overridden_table_name || table_name unless @null_records.empty? db[table_to_populate].insert_replace. insert_multiple(@null_records) if db.table_exists?(key_table_name) ids = @null_records.map {|r| {:dimension_id => r[:id]} } db[key_table_name].insert_replace.insert_multiple(ids) end end end |
#has_predetermined_values? ⇒ Boolean
Returns true if the set of values for this dimension is pretermined.
Examples of this may be date dimensions, currency dimensions etc.
104 105 106 |
# File 'lib/chicago/schema/dimension.rb', line 104 def has_predetermined_values? @predetermined_values end |
#identifiable? ⇒ Boolean
change to be consistent with identifiers
Returns true if this dimension can be identified as a concrete entity, with an original_id from a source system.
95 96 97 |
# File 'lib/chicago/schema/dimension.rb', line 95 def identifiable? !! original_key end |
#main_identifier ⇒ Object
Returns the main identifier for this record.
87 88 89 |
# File 'lib/chicago/schema/dimension.rb', line 87 def main_identifier @identifiers.first end |
#original_key ⇒ Object
make configurable.
Returns the column that represents the id in the original source for the dimension.
Currently this column must be called original_id
114 115 116 |
# File 'lib/chicago/schema/dimension.rb', line 114 def original_key @original_key ||= @columns.detect {|c| c.name == :original_id } end |
#visit(visitor) ⇒ Object
Dimensions accept Visitors
119 120 121 |
# File 'lib/chicago/schema/dimension.rb', line 119 def visit(visitor) visitor.visit_dimension(self) end |