Class: Handlebars::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-handlebars/context.rb

Instance Method Summary collapse

Constructor Details

#initialize(hbs, data) ⇒ Context

Returns a new instance of Context.



3
4
5
6
# File 'lib/ruby-handlebars/context.rb', line 3

def initialize(hbs, data)
  @hbs = hbs
  @data = data
end

Instance Method Details

#add_item(key, value) ⇒ Object



39
40
41
# File 'lib/ruby-handlebars/context.rb', line 39

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

#add_items(hash) ⇒ Object



43
44
45
# File 'lib/ruby-handlebars/context.rb', line 43

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

#escaperObject



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

def escaper
  @hbs.escaper
end

#get(path) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ruby-handlebars/context.rb', line 8

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

#get_as_helper(name) ⇒ Object



31
32
33
# File 'lib/ruby-handlebars/context.rb', line 31

def get_as_helper(name)
  @hbs.get_as_helper(name)
end

#get_helper(name) ⇒ Object



27
28
29
# File 'lib/ruby-handlebars/context.rb', line 27

def get_helper(name)
  @hbs.get_helper(name)
end

#get_partial(name) ⇒ Object



35
36
37
# File 'lib/ruby-handlebars/context.rb', line 35

def get_partial(name)
  @hbs.get_partial(name)
end

#with_temporary_context(args = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/ruby-handlebars/context.rb', line 47

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