Class: Babik::QuerySet::ProjectedField

Inherits:
Object
  • Object
show all
Defined in:
lib/babik/queryset/components/projection.rb

Overview

Each one of the fields that will be returned by SELECT clause

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, field) ⇒ ProjectedField

Construct a projected field from a model and its field. e.g.:

[:created_at, :birth_date]
[:stars, ->(stars) { [stars, 5].min } ]
Otherwise, a field of the local table or foreign tables.

Parameters:

  • model (ActiveRecord::Base)

    model whose field will be returned in the SELECT query.

  • field (Array, String)

    if Array, it must be [field_name, alias, transform] where

    • field_name is the name of the field (the column name). It is mandatory and must be the first

    item of the array.

    • alias if present, it will be used to name the field instead of its name.

    • transform, if present, a lambda function with the transformation each value of that column it must suffer.



66
67
68
69
70
71
72
73
74
# File 'lib/babik/queryset/components/projection.rb', line 66

def initialize(model, field)
  @model = model
  method_name = "initialize_from_#{field.class.to_s.downcase}"
  unless self.respond_to?(method_name)
    raise "No other parameter type is permitted in #{self.class}.new than Array, String and Symbol."
  end
  self.send(method_name, field)
  @selection = Babik::Selection::Path::Factory.build(model, @name)
end

Instance Attribute Details

#aliasObject (readonly)

Returns the value of attribute alias.



51
52
53
# File 'lib/babik/queryset/components/projection.rb', line 51

def alias
  @alias
end

#modelObject (readonly)

Returns the value of attribute model.



51
52
53
# File 'lib/babik/queryset/components/projection.rb', line 51

def model
  @model
end

#selectionObject (readonly)

Returns the value of attribute selection.



51
52
53
# File 'lib/babik/queryset/components/projection.rb', line 51

def selection
  @selection
end

#transformObject (readonly)

Returns the value of attribute transform.



51
52
53
# File 'lib/babik/queryset/components/projection.rb', line 51

def transform
  @transform
end

Instance Method Details

#initialize_from_array(field) ⇒ Object

Initialize from Array



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/babik/queryset/components/projection.rb', line 77

def initialize_from_array(field)
  @name = field[0]
  @alias = @name
  [1, 2].each do |field_index|
    next unless field[field_index]
    field_i = field[field_index]
    if [Symbol, String].include?(field_i.class)
      @alias = field_i
    elsif field_i.class == Proc
      @transform = field_i
    else
      raise "#{self.class}.new only accepts String/Symbol or Proc. Passed a #{field_i.class}."
    end
  end
end

#initialize_from_string(field) ⇒ Object

Initialize from String



94
95
96
97
98
# File 'lib/babik/queryset/components/projection.rb', line 94

def initialize_from_string(field)
  @name = field.to_sym
  @alias = field.to_sym
  @transform = nil
end

#initialize_from_symbol(field) ⇒ Object

Initialize from Symbol



101
102
103
# File 'lib/babik/queryset/components/projection.rb', line 101

def initialize_from_symbol(field)
  initialize_from_string(field)
end

#sqlSQL

Return sql of the field to project. i.e. something like this:

<table_alias>.<field>
<table_alias>.<field> AS <field_alias>

e.g.

users_0.first_name
posts_0.title AS post_title

Returns:

  • (SQL)

    SQL code for field to appear in SELECT.



113
114
115
# File 'lib/babik/queryset/components/projection.rb', line 113

def sql
  "#{@selection.target_alias}.#{@selection.selected_field} AS #{@alias}"
end