Class: Babik::QuerySet::ProjectedField
- Inherits:
-
Object
- Object
- Babik::QuerySet::ProjectedField
- 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
-
#alias ⇒ Object
readonly
Returns the value of attribute alias.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#selection ⇒ Object
readonly
Returns the value of attribute selection.
-
#transform ⇒ Object
readonly
Returns the value of attribute transform.
Instance Method Summary collapse
-
#initialize(model, field) ⇒ ProjectedField
constructor
Construct a projected field from a model and its field.
-
#initialize_from_array(field) ⇒ Object
Initialize from Array.
-
#initialize_from_string(field) ⇒ Object
Initialize from String.
-
#initialize_from_symbol(field) ⇒ Object
Initialize from Symbol.
-
#sql ⇒ SQL
Return sql of the field to project.
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.
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
#alias ⇒ Object (readonly)
Returns the value of attribute alias.
51 52 53 |
# File 'lib/babik/queryset/components/projection.rb', line 51 def alias @alias end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
51 52 53 |
# File 'lib/babik/queryset/components/projection.rb', line 51 def model @model end |
#selection ⇒ Object (readonly)
Returns the value of attribute selection.
51 52 53 |
# File 'lib/babik/queryset/components/projection.rb', line 51 def selection @selection end |
#transform ⇒ Object (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 |
#sql ⇒ SQL
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
113 114 115 |
# File 'lib/babik/queryset/components/projection.rb', line 113 def sql "#{@selection.target_alias}.#{@selection.selected_field} AS #{@alias}" end |