Sha256: 6c7b20894e70def9c98aad23763f2fbf3239f1a72071024d25931528a3b0b39c

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require 'alephant/renderer/error/invalid_base_path'

module Alephant
  module Renderer
    class ViewMapper

      DEFAULT_LOCATION = 'components'.freeze

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

      def base_path
        @base_path || DEFAULT_LOCATION
      end

      def base_path=(path)
        @base_path = File.directory?(path) ? path : raise(Error::InvalidBasePath.new(path))
      end

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

1 entries across 1 versions & 1 rubygems

Version Path
alephant-renderer-3.2.0 lib/alephant/renderer/view_mapper.rb