Class: Terrazine::Constructor

Inherits:
Object
  • Object
show all
Defined in:
lib/terrazine/constructor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(structure = {}) ⇒ Constructor

Returns a new instance of Constructor.



4
5
6
# File 'lib/terrazine/constructor.rb', line 4

def initialize(structure = {})
  @structure = structure
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/terrazine/constructor.rb', line 3

def params
  @params
end

#structureObject (readonly)

Returns the value of attribute structure.



3
4
5
# File 'lib/terrazine/constructor.rb', line 3

def structure
  @structure
end

Instance Method Details

#build_sql(options = {}) ⇒ Object

constructor.build_sql

> ‘SELECT .… FROM …’

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



113
114
115
# File 'lib/terrazine/constructor.rb', line 113

def build_sql(options = {})
  Builder.new.get_sql @structure, options
end

#distinct(fields = true) ⇒ Object



34
35
36
37
# File 'lib/terrazine/constructor.rb', line 34

def distinct(fields = true)
  @structure[:distinct] = fields
  self
end

#distinct_select(structure, fields = true) ⇒ Object



39
40
41
42
43
# File 'lib/terrazine/constructor.rb', line 39

def distinct_select(structure, fields = true)
  @structure[:distinct] = fields
  select structure
  self
end

#from(structure) ⇒ Object

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

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



49
50
51
52
# File 'lib/terrazine/constructor.rb', line 49

def from(structure)
  @structure[:from] = structure
  self
end

#join(structure) ⇒ Object

TODO: join constructor AND better syntax



55
56
57
58
# File 'lib/terrazine/constructor.rb', line 55

def join(structure)
  @structure[:join] = structure
  self
end

#limit(per) ⇒ Object

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



85
86
87
88
# File 'lib/terrazine/constructor.rb', line 85

def limit(per)
  @structure[:limit] = (per || 8).to_i
  self
end

#merge(params) ⇒ Object

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



105
106
107
108
# File 'lib/terrazine/constructor.rb', line 105

def merge(params)
  @structure.merge! params
  self
end

#offset(offset) ⇒ Object

same as limit =(



91
92
93
# File 'lib/terrazine/constructor.rb', line 91

def offset(offset)
  @structure[:offset] = offset || 0
end

#order(structure) ⇒ Object

TODO: order constructor -_-



79
80
81
82
# File 'lib/terrazine/constructor.rb', line 79

def order(structure)
  @structure[:order] = structure
  self
end

#paginate(params) ⇒ Object

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



97
98
99
100
101
# 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) ⇒ Object



29
30
31
32
# File 'lib/terrazine/constructor.rb', line 29

def select(structure)
  @structure[:select] = structure_constructor(@structure[:select], structure)
  self
end

#structure_constructor(structure, modifier) ⇒ Object

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Object

TODO: with constructor -_-



73
74
75
76
# File 'lib/terrazine/constructor.rb', line 73

def with(structure)
  @structure[:with] = structure
  self
end