class Terrazine::Constructor

Attributes

params[R]
structure[R]

Public Class Methods

new(structure = {}) click to toggle source
# File lib/terrazine/constructor.rb, line 4
def initialize(structure = {})
  @structure = structure
end

Public Instance Methods

build_sql(options = {}) click to toggle source

constructor.build_sql

> 'SELECT .… FROM …'

> ['SELECT .… FROM .… WHERE id = $1', [22]]

# File lib/terrazine/constructor.rb, line 113
def build_sql(options = {})
  Builder.new.get_sql @structure, options
end
distinct(fields = true) click to toggle source
# File lib/terrazine/constructor.rb, line 34
def distinct(fields = true)
  @structure[:distinct] = fields
  self
end
distinct_select(structure, fields = true) click to toggle source
# File lib/terrazine/constructor.rb, line 39
def distinct_select(structure, fields = true)
  @structure[:distinct] = fields
  select structure
  self
end
from(structure) click to toggle source

TODO: from construction from [:mrgl, :m] from [:_values, [1, 2], :rgl, [:zgl, :gl]]

> [[:mrgl, :m], [:_values, [1, 2], :rgl, [:zgl, :gl]]]

# File lib/terrazine/constructor.rb, line 49
def from(structure)
  @structure[:from] = structure
  self
end
join(structure) click to toggle source

TODO: join constructor AND better syntax

# File lib/terrazine/constructor.rb, line 55
def join(structure)
  @structure[:join] = structure
  self
end
limit(per) click to toggle source

TODO: default per used here and in builder…-_-

# File lib/terrazine/constructor.rb, line 85
def limit(per)
  @structure[:limit] = (per || 8).to_i
  self
end
merge(params) click to toggle source

just rewrite data. TODO: merge with merge without loss of data? constructor.merge(select: :content, order_by: 'f.id DESC', limit: 1)

# File lib/terrazine/constructor.rb, line 105
def merge(params)
  @structure.merge! params
  self
end
offset(offset) click to toggle source

same as limit =(

# File lib/terrazine/constructor.rb, line 91
def offset(offset)
  @structure[:offset] = offset || 0
end
order(structure) click to toggle source

TODO: order constructor -_-

# File lib/terrazine/constructor.rb, line 79
def order(structure)
  @structure[:order] = structure
  self
end
paginate(params) click to toggle source

TODO: serve - return count of all rows params - hash with keys :per, :page

# File lib/terrazine/constructor.rb, line 97
def paginate(params)
  limit params[:per]
  offset((params.fetch(:page, 1).to_i - 1) * @structure[:limit])
  self
end
select(structure) click to toggle source
# File lib/terrazine/constructor.rb, line 29
def select(structure)
  @structure[:select] = structure_constructor(@structure[:select], structure)
  self
end
structure_constructor(structure, modifier) click to toggle source

TODO? join hash inside array? TODO!! join values of existing keys on hashes merge

# File lib/terrazine/constructor.rb, line 10
def structure_constructor(structure, modifier)
  return modifier unless structure

  if structure.is_a?(Hash) && modifier.is_a?(Hash)
    modifier.each do |k, v|
      structure[k] = structure_constructor(structure[k], v)
    end
    structure
  else
    structure = structure.is_a?(Array) ? structure : [structure]
    if modifier.is_a?(Array)
      modifier.each { |i| structure_constructor structure, i }
    else
      structure << modifier
    end
    structure.uniq
  end
end
where(structure) click to toggle source
# File lib/terrazine/constructor.rb, line 60
def where(structure)
  w = @structure[:where]
  if w.is_a?(Array) && w.first.is_a?(Array)
    @structure[:where].push structure
  elsif w
    @structure[:where] = [w, structure]
  else
    @structure[:where] = structure
  end
  self
end
with(structure) click to toggle source

TODO: with constructor -_-

# File lib/terrazine/constructor.rb, line 73
def with(structure)
  @structure[:with] = structure
  self
end