Class: Chicago::Schema::DimensionReference

Inherits:
Column
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/chicago/schema/dimension_reference.rb

Overview

A reference to a dimension - supports the API of Column and of Dimension, so you can treat it as either.

Instance Attribute Summary

Attributes inherited from Column

#calculation, #column_type, #default, #elements, #max, #min

Attributes included from NamedElement

#label, #name

Instance Method Summary collapse

Methods inherited from Column

#==, #binary?, #calculated?, #countable?, #descriptive?, #hash, #indexed?, #internal?, #null?, #numeric?, #optional?, #textual?, #unique?

Constructor Details

#initialize(name, dimension, opts = {}) ⇒ DimensionReference

Returns a new instance of DimensionReference.



13
14
15
16
17
18
# File 'lib/chicago/schema/dimension_reference.rb', line 13

def initialize(name, dimension, opts={})
  super name, :integer, opts.merge(:min => 0)
  @dimension = dimension
  @table_name = "dimension_#{@name}".to_sym
  @key_name   = opts[:key_name] || "#{@name}_dimension_id".to_sym
end

Instance Method Details

#countable_labelObject



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

def countable_label
  "No. of #{label.pluralize}"
end

#database_nameObject

Returns the key name of this dimension.



25
26
27
# File 'lib/chicago/schema/dimension_reference.rb', line 25

def database_name
  @key_name
end

#default_valueObject

Returns the first null record id for this dimension, or 0 if the dimension has no null records defined.



53
54
55
56
57
58
59
60
# File 'lib/chicago/schema/dimension_reference.rb', line 53

def default_value
  record = @dimension.null_records.first
  if record && record[:id]
    record[:id]
  else
    super
  end
end

#kind_of?(klass) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/chicago/schema/dimension_reference.rb', line 63

def kind_of?(klass)
  klass == Chicago::Schema::Dimension || super
end

#qualify(col) ⇒ Object



35
36
37
# File 'lib/chicago/schema/dimension_reference.rb', line 35

def qualify(col)
  col.qualify_by(@table_name)
end

#qualify_by(table) ⇒ Object



39
40
41
# File 'lib/chicago/schema/dimension_reference.rb', line 39

def qualify_by(table)
  database_name.qualify(table)
end

#roleplayed?Boolean

Returns true if this dimension reference is roleplayed - i.e. it has a different name from the underlying dimension so that, for example, multiple date dimensions can be assigned to the same fact table.

Returns:

  • (Boolean)


47
48
49
# File 'lib/chicago/schema/dimension_reference.rb', line 47

def roleplayed?
  name != @dimension.name
end

#to_hashObject



29
30
31
32
33
# File 'lib/chicago/schema/dimension_reference.rb', line 29

def to_hash
  hsh = super
  hsh[:name] = database_name
  hsh
end

#visit(visitor) ⇒ Object

Dimension references are visitable



68
69
70
# File 'lib/chicago/schema/dimension_reference.rb', line 68

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