Class: PgExecArrayParams::SqlRefIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_exec_array_params/sql_ref_index.rb

Overview

Calculates inclusive bounds of each element in a flattened list Bounds are one-based (as sql ref indexes), single value for non-arrays

1, [2, 3], 4, [5, 6, 7]

> [1, [2, 3], 4, [5, 7]]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ SqlRefIndex

Returns a new instance of SqlRefIndex.



10
11
12
13
# File 'lib/pg_exec_array_params/sql_ref_index.rb', line 10

def initialize(array)
  @array = array
  @extra_items = 0
end

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



8
9
10
# File 'lib/pg_exec_array_params/sql_ref_index.rb', line 8

def array
  @array
end

Instance Method Details

#[](key) ⇒ Object



15
16
17
# File 'lib/pg_exec_array_params/sql_ref_index.rb', line 15

def [](key)
  sql_ref_index[key]
end

#sql_ref_indexObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pg_exec_array_params/sql_ref_index.rb', line 19

def sql_ref_index
  @sql_ref_index ||= array.map.with_index(1) do |item, idx|
    if item.is_a?(Array)
      add_extra_items = item.size
      add_extra_items -= 1 if add_extra_items.positive?
      [idx + @extra_items, idx + (@extra_items += add_extra_items)]
    else
      idx + @extra_items
    end
  end
end