Sha256: 6a8d408c4f5852b192fd3fe31be44f2338829e94fa2361e56b9b7b1c98bff35f

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 KB

Contents

require 'alephant/logger'

module Alephant
  module Publisher
    class ViewMapper
      include Logger
      include Renderer

      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)
        model_locations.reduce({}) do |obj, file|
          obj.tap do |o|
            model_id = model_id_for file
            o[model_id] = model(model_id, data)
          end
        end
      end

      private

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

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

      def model_locations
        Dir[model_base_path]
      end

      def model_base_path
        "#{base_path}/models/*"
      end

      def model_id_for(location)
        location.split('/').last.sub(/\.rb/, '')
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
alephant-publisher-0.6.5 lib/alephant/publisher/view_mapper.rb
alephant-publisher-0.6.4 lib/alephant/publisher/view_mapper.rb
alephant-publisher-0.6.3 lib/alephant/publisher/view_mapper.rb
alephant-publisher-0.6.2 lib/alephant/publisher/view_mapper.rb
alephant-publisher-0.6.1 lib/alephant/publisher/view_mapper.rb