Class: Stencil::Template::DataContext

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

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ DataContext

Returns a new instance of DataContext.



86
87
88
89
# File 'lib/stencil/template.rb', line 86

def initialize(data)
  @datasets = Hash.new{|h,k| h[k]=[]}
  @datasets[nil] << Data.new(data)
end

Instance Method Details

#access(path) ⇒ Object



119
120
121
122
# File 'lib/stencil/template.rb', line 119

def access(path)
  which, path = context_name(path)
  @datasets[which].last.access(path)
end

#context_name(path) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/stencil/template.rb', line 91

def context_name(path)
  m = /^@?([a-z]*)/.match(path)
  which = m[1]
  which = nil if which.empty?
  path = m.post_match
  return which,path
end

#inject_dataset(name, dataset) ⇒ Object



115
116
117
# File 'lib/stencil/template.rb', line 115

def inject_dataset(name, dataset)
  @datasets[name] << Data.new(dataset)
end

#pop_context(from = nil) ⇒ Object



104
105
106
# File 'lib/stencil/template.rb', line 104

def pop_context(from=nil)
  @datasets[from].pop
end

#push_context(path, onto = nil) ⇒ Object



99
100
101
102
# File 'lib/stencil/template.rb', line 99

def push_context(path, onto=nil)
  which, path = context_name(path)
  @datasets[onto] << @datasets[which].last.recontext(path)
end

#with_context(path, onto = nil) ⇒ Object



108
109
110
111
112
113
# File 'lib/stencil/template.rb', line 108

def with_context(path, onto = nil)
  push_context(path, onto)
  result = yield(self)
  pop_context(onto)
  return result
end