Sha256: 1f00b2992de0620b3aafc6f4ae532c9738315342ad7a5421e5023dac1af5a5c7

Contents?: true

Size: 1.46 KB

Versions: 16

Compression:

Stored size: 1.46 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")
      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

16 entries across 16 versions & 1 rubygems

Version Path
alephant-publisher-0.2.4 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.2.3 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.2.2 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.2.1 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.2.0 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.1.8 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.1.7 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.1.6 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.1.5 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.1.4 lib/alephant/publisher/render_mapper.rb
alephant-publisher-0.1.3 lib/alephant/publisher/models/render_mapper.rb
alephant-publisher-0.1.0 lib/alephant/publisher/models/render_mapper.rb
alephant-publisher-0.0.4 lib/alephant/publisher/models/render_mapper.rb
alephant-publisher-0.0.3 lib/alephant/publisher/models/render_mapper.rb
alephant-publisher-0.0.2 lib/alephant/publisher/models/render_mapper.rb
alephant-publisher-0.0.1 lib/alephant/publisher/models/render_mapper.rb