Class: Stencil::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/stencil/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Data

Returns a new instance of Data.



18
19
20
21
# File 'lib/stencil/template.rb', line 18

def initialize(data)
  @data = data
  @path = []
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



71
72
73
# File 'lib/stencil/template.rb', line 71

def data
  @data
end

#pathObject

Returns the value of attribute path.



71
72
73
# File 'lib/stencil/template.rb', line 71

def path
  @path
end

#pointObject

Returns the value of attribute point.



71
72
73
# File 'lib/stencil/template.rb', line 71

def point
  @point
end

Instance Method Details

#access(string) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/stencil/template.rb', line 73

def access(string)
  data = @data
  path = relative(string)
  path.each do |segment|
    data = data[segment]
  end
  return data
end

#recontext(string) ⇒ Object



65
66
67
68
69
# File 'lib/stencil/template.rb', line 65

def recontext(string)
  result = Data.new(@data)
  result.path = relative(string)
  return result
end

#relative(string) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/stencil/template.rb', line 23

def relative(string)
  path = @path.dup
  s = StringScanner.new(string||"")

  goto = s.scan(/[^:#]*/)
  g = StringScanner.new(goto)

  while g.rest?
    go = g.getch

    case go
    when "^"
      path.pop
    when "/"
      path.clear
    when "+"
      number = g.scan(/\d+/).to_i
      here = path.pop
      path.push(here + number)
    when "-"
      number = g.scan(/\d+/).to_i
      here = path.pop
      path.push(here - number)
    else 
      raise "Unrecognized path offset: #{go}"
    end
  end

  while s.rest?
    delim = s.getch
    segment = s.scan(/[^:#]*/)
      case delim
      when ":"
        path << segment
      when "#"
        path << segment.to_i
      end
  end

  path
end