Class: Alephant::MultiRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/alephant/models/multi_renderer.rb

Constant Summary collapse

DEFAULT_LOCATION =
'components'

Instance Method Summary collapse

Constructor Details

#initialize(model_file, view_base_path = nil) ⇒ MultiRenderer

Returns a new instance of MultiRenderer.



5
6
7
8
9
# File 'lib/alephant/models/multi_renderer.rb', line 5

def initialize(model_file, view_base_path=nil)
  self.base_path = view_base_path unless view_base_path.nil?
  @model_file = model_file
  @logger = ::Alephant.logger
end

Instance Method Details

#base_pathObject



11
12
13
# File 'lib/alephant/models/multi_renderer.rb', line 11

def base_path
  @base_path || DEFAULT_LOCATION
end

#base_path=(path) ⇒ Object



15
16
17
# File 'lib/alephant/models/multi_renderer.rb', line 15

def base_path=(path)
  @base_path = File.directory?(path) ? path : (raise Errors::InvalidViewPath)
end

#create_instance(data) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/alephant/models/multi_renderer.rb', line 42

def create_instance(data)
  begin
    create_model(klass, data)
  rescue Exception => e
    @logger.error("Renderer.model: exeception #{e.message}")
    raise Errors::ViewModelNotFound
  end
end

#render(data) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/alephant/models/multi_renderer.rb', line 19

def render(data)
  instance = create_instance(data)

  template_locations.reduce({}) do |obj, file|
    template_id = template_id_for file
    obj.tap do |o|
      o[template_id.to_sym] = render_template(
        template_id,
        data,
        instance
      )
    end
  end
end

#render_template(template_file, data, instance = nil) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/alephant/models/multi_renderer.rb', line 34

def render_template(template_file, data, instance = nil)
  renderer(
    template_file,
    base_path,
    instance.nil? ? create_instance(data) : instance
  ).render.chomp!
end

#renderer(template_file, base_path, model_object) ⇒ Object



51
52
53
# File 'lib/alephant/models/multi_renderer.rb', line 51

def renderer(template_file, base_path, model_object)
  Renderer.new(template_file, base_path, model_object)
end