Module: Handlebars::Context

Included in:
Handlebars
Defined in:
lib/ruby-handlebars/context.rb

Instance Method Summary collapse

Instance Method Details

#add_item(key, value) ⇒ Object



18
19
20
# File 'lib/ruby-handlebars/context.rb', line 18

def add_item(key, value)
  locals[key.to_sym] = value
end

#add_items(hash) ⇒ Object



22
23
24
# File 'lib/ruby-handlebars/context.rb', line 22

def add_items(hash)
  hash.map { |k, v| add_item(k, v) }
end

#get(path) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ruby-handlebars/context.rb', line 3

def get(path)
  items = path.split('.'.freeze)
  if locals.key? items.first.to_sym
    current = locals
  else
    current = @data
  end

  until items.empty?
    current = get_attribute(current, items.shift)
  end

  current
end

#with_temporary_context(args = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/ruby-handlebars/context.rb', line 26

def with_temporary_context(args = {})
  saved = args.keys.collect { |key| [key, get(key.to_s)] }.to_h

  add_items(args)
  block_result = yield
  locals.merge!(saved)

  block_result
end