Module: Jekyll::Drill

Defined in:
lib/jekyll-drill/filter.rb

Instance Method Summary collapse

Instance Method Details

#drill(str, obj) ⇒ Object

Takes a dot-notated string and uses it to drill down into a hash. Returns the drilled value, or the original string if there are any errors.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jekyll-drill/filter.rb', line 8

def drill(str, obj)
  # Keep track of the last thing we drilled to.
  drilled = obj

  # Keep track of how many levels we have drilled.
  levels_drilled = 0
  levels = str.split('.')

  # Loop through each level.
  levels.each do |level|

    # If we have drilled down to a scalar value too soon, abort.
    break if drilled.class != Hash

    if drilled.has_key? level
      # If we find something, continue drilling.
      drilled = drilled[level]
      levels_drilled += 1
    end

  end

  # If we didn't drill the right number of levels, return the
  # original string.
  if levels.length != levels_drilled
    return str
  end

  # Otherwise we must have drilled all they way.
  return drilled
end