Class: Chicago::Schema::Column

Inherits:
Object
  • Object
show all
Includes:
NamedElement
Defined in:
lib/chicago/schema/column.rb

Overview

A column in a dimension or fact record.

The column definition is used to generate the options to create the column in the database schema, but also to provide an abstract definition of the column for views and other Data Warehouse code.

You shouldn’t need to create a Column manually - they are generally defined using the schema definition DSL.

Direct Known Subclasses

DimensionReference, Measure

Instance Attribute Summary collapse

Attributes included from NamedElement

#label, #name

Instance Method Summary collapse

Constructor Details

#initialize(name, column_type, opts = {}) ⇒ Column

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new column definition.

name

the name of the column.

column_type

the abstract type of the column. For example, :string.

Options:

min

the minimum length/number of this column.

max

the maximum length/number of this column.

range

any object with a min & max method - overrides min/max (above).

null

whether this column can be null. False by default.

elements

the allowed values this column can take.

default

the default value for this column.

descriptive

whether this column is purely descriptive and

won’t be used for grouping/filtering.

internal

the column is for internal use only, and shouldn’t

be displayed/used directly in a user context
optional

the column isn’t expected to be populated.

calculation

the column is not stored in the database, but

calculated on demand


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chicago/schema/column.rb', line 41

def initialize(name, column_type, opts={})
  @opts = normalize_opts(column_type, opts)
  
  super name, opts
  
  @column_type = column_type
  if @opts[:countable].kind_of?(String)
    @countable_label =  @opts[:countable]
  end
  @countable   = !! @opts[:countable]
  @min         = @opts[:min]
  @max         = @opts[:max]
  @null        = @opts[:null]
  @elements    = @opts[:elements]
  @default     = @opts[:default]
  @descriptive = !! @opts[:descriptive]
  @internal    = @opts.has_key?(:internal) ? !! @opts[:internal] : 
    column_type == :binary
  @unique      = @opts.has_key?(:unique) ? !! @opts[:unique] :
    column_type == :binary
  @optional    = !! (@opts.has_key?(:optional) ? @opts[:optional] : 
                     @opts[:null])
  @calculation = @opts[:calculation]
end

Instance Attribute Details

#calculationObject (readonly)

Returns the Sequel calculation for this column.



83
84
85
# File 'lib/chicago/schema/column.rb', line 83

def calculation
  @calculation
end

#column_typeObject (readonly)

Returns the type of this column. This is an abstract type, not a database type (for example :string, not :varchar).



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

def column_type
  @column_type
end

#countable_labelObject (readonly)

The human-friendly label for counting items in this column.



109
110
111
# File 'lib/chicago/schema/column.rb', line 109

def countable_label
  @countable_label
end

#defaultObject (readonly)

Returns the explicit default as set in the database, or nil.



80
81
82
# File 'lib/chicago/schema/column.rb', line 80

def default
  @default
end

#elementsObject (readonly)

Returns an Array of allowed elements, or nil.



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

def elements
  @elements
end

#maxObject (readonly)

Returns the minimum value of this column, or nil.



74
75
76
# File 'lib/chicago/schema/column.rb', line 74

def max
  @max
end

#minObject (readonly)

Returns the minimum value of this column, or nil.



71
72
73
# File 'lib/chicago/schema/column.rb', line 71

def min
  @min
end

Instance Method Details

#==(other) ⇒ Object

Returns true if both definition’s attributes are equal.



159
160
161
162
163
164
# File 'lib/chicago/schema/column.rb', line 159

def ==(other)
  other.kind_of?(self.class) && 
    name == other.name && 
    column_type == other.column_type && 
    @opts == other.instance_variable_get(:@opts)
end

#binary?Boolean

Returns true if the column stores a binary value.

Returns:

  • (Boolean)


177
178
179
# File 'lib/chicago/schema/column.rb', line 177

def binary?
  @binary ||= :binary == column_type
end

#calculated?Boolean

Returns true if this column is calculated.

Returns:

  • (Boolean)


154
155
156
# File 'lib/chicago/schema/column.rb', line 154

def calculated?
  !! @calculation
end

#countable?Boolean

Returns true if this column can be counted.

Returns:

  • (Boolean)


114
115
116
# File 'lib/chicago/schema/column.rb', line 114

def countable?
  @countable
end

#default_valueObject

Returns the calculated default value.

This may be different from the explicit default - for example a boolean not-null column that has no explicit default will have a default of nil, and a default value of false.

The distinction is important, otherwise values can end up missing in junk dimensions due to differences between what ruby and the database considers unique.



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chicago/schema/column.rb', line 94

def default_value
  if @default || null?
    @default
  elsif @column_type == :boolean
    false
  elsif numeric?
    0
  elsif textual?
    ''
  else
    nil
  end
end

#descriptive?Boolean

Returns true if this column is just informational, and is not intended to be used as a filter.

Returns:

  • (Boolean)


144
145
146
# File 'lib/chicago/schema/column.rb', line 144

def descriptive?
  @descriptive
end

#hashObject

:nodoc:



181
182
183
# File 'lib/chicago/schema/column.rb', line 181

def hash #:nodoc:
  name.hash
end

#indexed?Boolean

Returns true if this column should be indexed

Returns:

  • (Boolean)


119
120
121
# File 'lib/chicago/schema/column.rb', line 119

def indexed?
  ! (descriptive? || calculated?)
end

#internal?Boolean

Returns true if this column should be ignored in user-facing parts of an application

Returns:

  • (Boolean)


133
134
135
# File 'lib/chicago/schema/column.rb', line 133

def internal?
  @internal
end

#null?Boolean

Returns true if null values are allowed.

Returns:

  • (Boolean)


138
139
140
# File 'lib/chicago/schema/column.rb', line 138

def null?
  @null
end

#numeric?Boolean

Returns true if this column stores a numeric value.

Returns:

  • (Boolean)


167
168
169
# File 'lib/chicago/schema/column.rb', line 167

def numeric?
  @numeric ||= [:integer, :money, :percent, :decimal, :float].include?(column_type)
end

#optional?Boolean

Returns true if this column is optional.

Will be defaulted from whether the column allows null values, can be overridden (for dates etc).

Returns:

  • (Boolean)


127
128
129
# File 'lib/chicago/schema/column.rb', line 127

def optional?
  @optional
end

#qualify_by(table) ⇒ Object



199
200
201
# File 'lib/chicago/schema/column.rb', line 199

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

#textual?Boolean

Returns true if the column stores a textual value.

Returns:

  • (Boolean)


172
173
174
# File 'lib/chicago/schema/column.rb', line 172

def textual?
  @textual ||= [:string, :text].include?(column_type)
end

#to_hashObject

Returns a hash of column options.



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/chicago/schema/column.rb', line 186

def to_hash
  db_schema = {
    :name => name,
    :column_type => column_type,
    :null => null?
  }
  db_schema[:default]  = default   if default || column_type == :timestamp
  db_schema[:elements] = elements  if elements
  db_schema[:size]     = size      if size
  db_schema[:unsigned] = !! unsigned? if numeric?
  db_schema
end

#unique?Boolean

Returns true if this column is unique.

Returns:

  • (Boolean)


149
150
151
# File 'lib/chicago/schema/column.rb', line 149

def unique?
  @unique
end

#visit(visitor) ⇒ Object

Columns accept Visitors



204
205
206
# File 'lib/chicago/schema/column.rb', line 204

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