class AzaharaSchema::DerivedAttribute

The class is attribute for derivateve attribute, it is used for working with combination of attributes as one attribute.

Attributes

attribute_names[RW]
derivation_method[RW]

Public Class Methods

new(model, name, derivation_method, *attribute_names, **options) click to toggle source
Calls superclass method AzaharaSchema::Attribute::new
# File lib/azahara_schema/derived_attribute.rb, line 13
def initialize(model, name, derivation_method, *attribute_names, **options)
  self.attribute_names = attribute_names
  self.derivation_method = derivation_method.to_sym
  @options = options
  super(model, name, type)
end

Public Instance Methods

add_join(scope) click to toggle source
# File lib/azahara_schema/derived_attribute.rb, line 89
def add_join(scope)
  attributes.each{|a| scope = a.add_join(scope) }
  scope
end
add_preload(scope) click to toggle source
# File lib/azahara_schema/derived_attribute.rb, line 94
def add_preload(scope)
  attributes.each{|a| scope = a.add_preload(scope) }
  scope
end
add_statement(scope, operator, values) click to toggle source
Calls superclass method AzaharaSchema::Attribute#add_statement
# File lib/azahara_schema/derived_attribute.rb, line 78
def add_statement(scope, operator, values)
  super(add_join(scope), operator, values)
end
arel_field(t_alias=nil) click to toggle source

do not alias tables - let attributes derived from handle that

# File lib/azahara_schema/derived_attribute.rb, line 52
def arel_field(t_alias=nil)
  case derivation_method
  when :concat
    arel_fields = attributes.collect{|a| a.arel_field}
    (1..arel_fields.length-1).to_a.reverse.each{|i| arel_fields.insert(i, Arel::Nodes::SqlLiteral.new("'#{concat_divider}'")) }
    Arel::Nodes::NamedFunction.new 'CONCAT', arel_fields
  else
    raise "DerivedAttribute(#{name}) - derivation_method '#{derivation_method}' is not supported"
  end
end
arel_join(parent=nil, join_type=::Arel::Nodes::OuterJoin, a_tbl=self.arel_table(self.table_alias)) click to toggle source
# File lib/azahara_schema/derived_attribute.rb, line 82
def arel_join(parent=nil, join_type=::Arel::Nodes::OuterJoin, a_tbl=self.arel_table(self.table_alias))
  parent ||= self.arel_table(nil)
  joined = parent
  attributes.each{|a| joined = a.arel_join(joined, join_type) }
  joined
end
arel_statement(operator, values) click to toggle source
Calls superclass method AzaharaSchema::Attribute#arel_statement
# File lib/azahara_schema/derived_attribute.rb, line 67
def arel_statement(operator, values)
  case operator
  when '~'
    arl = attributes[0].arel_statement(operator, values)
    attributes[1..-1].each{|att| arl = arl.or( att.arel_statement(operator, values) ) }
    arl
  else
    super
  end
end
attribute_names=(names) click to toggle source

TODO: check if attributes support derivation method

# File lib/azahara_schema/derived_attribute.rb, line 23
def attribute_names=(names)
  @attributes = nil
  @attribute_names = names
end
attributes() click to toggle source
# File lib/azahara_schema/derived_attribute.rb, line 28
def attributes
  @attributes ||= options[:attributes] || options[:schema].available_attributes_hash.slice(*attribute_names).values
end
concat_divider() click to toggle source
# File lib/azahara_schema/derived_attribute.rb, line 32
def concat_divider
  options[:divider] || ' '
end
options() click to toggle source
# File lib/azahara_schema/derived_attribute.rb, line 36
def options
  @options || {}
end
type() click to toggle source

——————-| OVERRIDES |———————

# File lib/azahara_schema/derived_attribute.rb, line 42
def type
  case derivation_method
  when :concat
    'string'
  else
    raise "DerivedAttribute(#{name}) - derivation_method '#{derivation_method}' is not supported"
  end
end
value(record) click to toggle source
# File lib/azahara_schema/derived_attribute.rb, line 63
def value(record)
  record.try(name) || attributes.collect{|a| a.value(record) }.join(concat_divider)
end