Sha256: 5687210c8562b15dc62445e852123e95adc2cbf1f33c318b7c13213fbb43154a

Contents?: true

Size: 1.47 KB

Versions: 10

Compression:

Stored size: 1.47 KB

Contents

require 'alephant/logger'

module Alephant
  module Publisher
    class RenderMapper
      include ::Alephant::Logger
      DEFAULT_LOCATION = 'components'

      def initialize(component_id, view_base_path=nil)
        self.base_path = "#{view_base_path}/#{component_id}" unless view_base_path.nil?
        @component_id = component_id
      end

      def base_path
        @base_path || DEFAULT_LOCATION
      end

      def base_path=(path)
        @base_path = File.directory?(path) ? path : (raise "Invalid path: '#{path}'")
      end

      def generate(data)
        template_locations.reduce({}) do |obj, file|
          template_id = template_id_for file
          obj.tap { |o| o[template_id] = create_renderer(template_id, data) }
        end
      end

      def create_renderer(template_file, data)
        model = instantiate_model(template_file, data)
        Renderer.create(template_file, base_path, model)
      end

      private

      def instantiate_model(view_id, data)
        require model_location_for view_id
        Views.get_registered_class(view_id).new(data)
      end

      def model_location_for(template_file)
        File.join(base_path, 'models', "#{template_file}.rb")
      end

     def template_locations
        Dir[template_base_path]
      end

      def template_base_path
        "#{base_path}/templates/*"
      end

      def template_id_for(template_location)
        template_location.split('/').last.sub(/\.mustache/, '')
      end

    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
alephant-publisher-0.5.1 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.5.0 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.4.0 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.3.1 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.3.0 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.2.9 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.2.8 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.2.7 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.2.6 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.2.5 lib/alephant/publisher/render_mapper.rb