Sha256: e6be089422de740b414028f6d49da7deb59012e51f4561966ae157d2db82ec91

Contents?: true

Size: 1.43 KB

Versions: 6

Compression:

Stored size: 1.43 KB

Contents

module Fierce
  class MasterOfCeremonies
    attr_reader    :template, :path, :locals, :controller, :context

    def initialize(template, path, locals, controller, context)
      @template =   template
      @path =       path
      @locals =     locals
      @controller = controller
      @context =    context
    end

    def view_model
      @view_model ||= ViewModel.new(*presenters)
    end

    def custom_presenter
      return @custom_presenter if @custom_presenter
      return unless presenter_class = PresenterFinder.new(path).perform

      @custom_presenter = if presenter_class.instance_method(:initialize).arity == 1
        presenter_class.new(ViewModel.new(*base_presenters))
      else
        presenter_class.new
      end
    end

    def base_presenters
      collection = [
        DelegateGenerator::Controller.new(controller).generate,
        context
      ]
      collection.unshift(locals_presenter) if locals_presenter
      collection
    end

    def presenters
      collection = base_presenters
      collection.unshift(custom_presenter) if custom_presenter
      collection
    end

    def locals_presenter
      return unless locals.size > 0
      Struct.new(*locals.keys.map(&:to_sym)).new(*locals.values)
    end

    def render
      Renderer.new(self).render(template, view_model).html_safe
    end

    def partial(name)
      partial = PartialFinder.new(name, path, custom_presenter, context).perform
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
fierce-0.2.0 lib/fierce/master_of_ceremonies.rb
fierce-0.1.4 lib/fierce/master_of_ceremonies.rb
fierce-0.1.3 lib/fierce/master_of_ceremonies.rb
fierce-0.1.2 lib/fierce/master_of_ceremonies.rb
fierce-0.1.1 lib/fierce/master_of_ceremonies.rb
fierce-0.1.0 lib/fierce/master_of_ceremonies.rb