Module: JSONSelect::PositionHelpers

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

Instance Method Summary collapse

Instance Method Details

#format_nth_child(test) ⇒ Object



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

def format_nth_child(test)
  ":nth-child(#{test[:a]}n#{test[:b]})"
end

#format_nth_last_child(test) ⇒ Object



28
29
30
# File 'lib/json_select/helpers/position.rb', line 28

def format_nth_last_child(test)
  ":nth-last-child(#{test[:a]}n#{test[:b]})"
end

#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



22
23
24
25
26
# File 'lib/json_select/helpers/position.rb', line 22

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