Class: Fierce::PartialFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/fierce/partial_finder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path, custom_presenter, context) ⇒ PartialFinder

Returns a new instance of PartialFinder.



5
6
7
8
9
10
# File 'lib/fierce/partial_finder.rb', line 5

def initialize(name, path, custom_presenter, context)
  @name = name
  @path = path
  @custom_presenter = custom_presenter
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



3
4
5
# File 'lib/fierce/partial_finder.rb', line 3

def context
  @context
end

#custom_presenterObject (readonly)

Returns the value of attribute custom_presenter.



3
4
5
# File 'lib/fierce/partial_finder.rb', line 3

def custom_presenter
  @custom_presenter
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/fierce/partial_finder.rb', line 3

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/fierce/partial_finder.rb', line 3

def path
  @path
end

Instance Method Details

#dir(p) ⇒ Object



30
31
32
33
34
# File 'lib/fierce/partial_finder.rb', line 30

def dir(p)
  parts = p.split('/')
  dir = parts[0..parts.length-2]
  dir.join('/')
end

#local_pathObject



26
27
28
# File 'lib/fierce/partial_finder.rb', line 26

def local_path
  "#{dir(path)}/#{name}"
end

#mustache_partialObject



36
37
38
39
40
# File 'lib/fierce/partial_finder.rb', line 36

def mustache_partial
  context.lookup_context.find_template(partial_path, [], true, [], { handlers: [:mustache] })
rescue
  false
end

#partial_pathObject



22
23
24
# File 'lib/fierce/partial_finder.rb', line 22

def partial_path
  @partial_path ||= presenter_path || local_path
end

#performObject



42
43
44
45
46
47
48
# File 'lib/fierce/partial_finder.rb', line 42

def perform
  if template = mustache_partial
    template.source
  else
    context.render partial: partial_path
  end
end

#presenter_partialsObject



12
13
14
15
# File 'lib/fierce/partial_finder.rb', line 12

def presenter_partials
  return unless custom_presenter && custom_presenter.respond_to?(:partials)
  custom_presenter.partials.symbolize_keys
end

#presenter_pathObject



17
18
19
20
# File 'lib/fierce/partial_finder.rb', line 17

def presenter_path
  return unless presenter_partials
  presenter_partials[name]
end