Sha256: c0ad4a8eab64d8d6cd3b851f638506aa022c2d778aa2ae885dd451a9c887ba1f

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

require "alephant/renderer/error/invalid_path"
require "alephant/logger"

module Alephant
  module Renderer
    class ViewMapper
      include Logger

      DEFAULT_LOCATION = "components"

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

      def base_path
        @base_path || DEFAULT_LOCATION
      end

      def base_path=(path)
        @base_path = File.directory?(path) ? path : raise_error(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 raise_error(path)
        fail(Error::InvalidBasePath.new(path)).tap do
          logger.metric("ViewMapperInvalidPath")
          logger.error(
            "event"  => "ViewMapperBasePathInvalidFound",
            "path"   => path,
            "method" => "#{self.class}#raise_error"
          )
        end
      end

      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

3 entries across 3 versions & 1 rubygems

Version Path
alephant-renderer-3.0.0 lib/alephant/renderer/view_mapper.rb
alephant-renderer-2.0.3 lib/alephant/renderer/view_mapper.rb
alephant-renderer-2.0.2 lib/alephant/renderer/view_mapper.rb