Module: JSONSelect::PositionHelpers

Defined in:
lib/json_select/helpers/position.rb

Instance Method Summary collapse

Instance Method Details

#nth_child(object, test, key, idx, size, depth) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/json_select/helpers/position.rb', line 3

def nth_child(object, test, key, idx, size, depth)
  return false unless idx and size
  
  idx += 1
  
  a = test[:a]
  b = test[:b]
  
  if a == 0
    (b == idx)
  else
    (((idx - b) % a) == 0) and ((idx * a + b) >= 0)
  end
end

#nth_last_child(object, test, key, idx, size, depth) ⇒ Object



18
19
20
21
22
# File 'lib/json_select/helpers/position.rb', line 18

def nth_last_child(object, test, key, idx, size, depth)
  return false unless idx and size
  
  nth_child(object, test, key, (size - idx) - 1, size, depth)
end