Class: ActiveWarehouse::CalculatedField

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

Overview

A field that uses a Proc to calculate the value

Instance Attribute Summary collapse

Attributes inherited from Field

#field_options, #limit, #name, #owning_class, #precision, #scale, #type

Instance Method Summary collapse

Methods inherited from Field

#column_type, #from_table_name, #label, #label_for_table, #table_alias, #to_s

Constructor Details

#initialize(fact_class, name, type = :integer, field_options = {}, &block) ⇒ CalculatedField

Initialize the calculated field

fact_class is the fact class that the field is calculated in name is the name of the calculated field type is the type of the calculated field (defaults to :integer) field_options is a Hash of options for the field

This method accepts a block which should take a single argument that is the record itself.



14
15
16
17
18
19
20
# File 'lib/active_warehouse/calculated_field.rb', line 14

def initialize(fact_class, name, type = :integer, field_options = {}, &block)
  unless block_given?
    raise ArgumentError, "A block is required for the calculated field #{name} in #{fact_class}"
  end
  super(fact_class, name.to_s, type, field_options)
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



4
5
6
# File 'lib/active_warehouse/calculated_field.rb', line 4

def block
  @block
end

Instance Method Details

#calculate(values) ⇒ Object

Calculate the field value using the Hash of type-casted values



23
24
25
# File 'lib/active_warehouse/calculated_field.rb', line 23

def calculate(values)
  @block.call(values)
end