Class: Nexter::Derange

Inherits:
Object
  • Object
show all
Defined in:
lib/nexter/derange.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, goto) ⇒ Derange

Returns a new instance of Derange.



11
12
13
14
15
16
17
18
# File 'lib/nexter/derange.rb', line 11

def initialize(model, goto)
  @model = model
  @table_name = model.class.table_name
  @trunks = []
  @reorder = false
  @or_null = false
  @compass = Nexter::Compass.new(goto)
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



6
7
8
# File 'lib/nexter/derange.rb', line 6

def columns
  @columns
end

#compassObject (readonly)

Returns the value of attribute compass.



4
5
6
# File 'lib/nexter/derange.rb', line 4

def compass
  @compass
end

#delimiterObject

Returns the value of attribute delimiter.



6
7
8
# File 'lib/nexter/derange.rb', line 6

def delimiter
  @delimiter
end

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/nexter/derange.rb', line 4

def model
  @model
end

#reorderObject (readonly)

Returns the value of attribute reorder.



4
5
6
# File 'lib/nexter/derange.rb', line 4

def reorder
  @reorder
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



4
5
6
# File 'lib/nexter/derange.rb', line 4

def table_name
  @table_name
end

#trunksObject

Returns the value of attribute trunks.



6
7
8
# File 'lib/nexter/derange.rb', line 6

def trunks
  @trunks
end

Instance Method Details

#rangeObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/nexter/derange.rb', line 34

def range
  trunk = columns.map do |col|
    if range_value = value_of(col[0])
      # binding.pry
      "#{col[0]} = #{model.class.sanitize range_value}"
    else
      "#{col[0]} IS NULL"
    end
  end.join(' AND ')
end

#set_vals(order_vals, order_col) ⇒ Object



20
21
22
23
24
# File 'lib/nexter/derange.rb', line 20

def set_vals(order_vals, order_col)
  @columns = order_vals
  @delimiter = order_col[0]
  compass.direction = order_col[1]
end

#sliceObject



45
46
47
48
49
50
51
52
53
# File 'lib/nexter/derange.rb', line 45

def slice
  if val = value_of(delimiter)
    d = model.class.sanitize val
    delimited = "#{delimiter} #{bracket} #{d}"
  else
    # @reorder = true
    "#{delimiter} IS NULL AND #{table_name}.id > #{model.id}"
  end
end

#value_of(cursor) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/nexter/derange.rb', line 55

def value_of(cursor)
  splits = cursor.split(".")
  result = if splits.first == table_name || splits.size == 1
    model.send(splits.last)
  else
    # binding.pry
    asso = model.class.reflections.keys.grep(/#{splits.first.singularize}/).first
    asso = model.send(asso) and asso.send(splits.last)
  end
end

#whereObject



26
27
28
# File 'lib/nexter/derange.rb', line 26

def where
  "(#{range} #{range.blank? ? '' : 'AND'} #{slice})"
end