Class: ActiveWarehouse::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/active_warehouse/field.rb

Overview

Encapsulates a field.

Direct Known Subclasses

AggregateField, CalculatedField

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owning_class, name, type, field_options = {}) ⇒ Field

owning_class is the class of the table, either Fact or Dimension, that this field is found in. Must somehow subclass ActiveRecord::Base name is the name of this field. field_options is a hash of raw options from the original definition. Options can include :label => a column alias or label for this field, :table_alias for a table alias (useful for building queries)



26
27
28
29
30
31
32
33
# File 'lib/active_warehouse/field.rb', line 26

def initialize(owning_class, name, type, field_options = {})
  @owning_class = owning_class
  @name = name
  @type = type
  @field_options = field_options
  @label = field_options[:label]
  @table_alias = field_options[:table_alias]
end

Instance Attribute Details

#field_optionsObject (readonly)

A Hash of options for the field



18
19
20
# File 'lib/active_warehouse/field.rb', line 18

def field_options
  @field_options
end

#limitObject

Returns the value of attribute limit.



13
14
15
# File 'lib/active_warehouse/field.rb', line 13

def limit
  @limit
end

#nameObject (readonly)

returns name of this field, matches name of the column



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

def name
  @name
end

#owning_classObject (readonly)

The owning class which is either a Fact or Dimension



5
6
7
# File 'lib/active_warehouse/field.rb', line 5

def owning_class
  @owning_class
end

#precisionObject

Returns the value of attribute precision.



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

def precision
  @precision
end

#scaleObject

Returns the value of attribute scale.



14
15
16
# File 'lib/active_warehouse/field.rb', line 14

def scale
  @scale
end

#typeObject (readonly)

The field type



11
12
13
# File 'lib/active_warehouse/field.rb', line 11

def type
  @type
end

Instance Method Details

#column_typeObject

returns rails specific column type, e.g. :float or :string



48
49
50
# File 'lib/active_warehouse/field.rb', line 48

def column_type
  @type
end

#from_table_nameObject

returns the table name that has this fact column



59
60
61
# File 'lib/active_warehouse/field.rb', line 59

def from_table_name
  owning_class.table_name
end

#labelObject

returns the :label set in field_options, or from_table_name+‘_’+name. Unless you have table_alias specified, then label will return table_alias+‘_’+name. The default label can exceed database limits, so use :label to override.



38
39
40
# File 'lib/active_warehouse/field.rb', line 38

def label
  @label ? @label : "#{table_alias || from_table_name}_#{name}"
end

#label_for_tableObject

convert the label into something we can use in a table. i.e., ‘Sum of Transactions’ becomes ‘sum_of_transactions’



54
55
56
# File 'lib/active_warehouse/field.rb', line 54

def label_for_table
  label.gsub(/ /, '_').downcase
end

#table_aliasObject

returns a table alias or if none set just the table name



64
65
66
# File 'lib/active_warehouse/field.rb', line 64

def table_alias
  @table_alias || from_table_name
end

#to_sObject

Get a display string for the field. Delegates to label.



69
70
71
# File 'lib/active_warehouse/field.rb', line 69

def to_s
  label
end