Sha256: 99bed22ab52e54665f9f53da9da444bae0f387a91d189339e97cb9b16271ce21

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

require 'alephant/publisher/request/log_helper'

module Alephant
  module Publisher
    module Request
      class DataMapperFactory
        include Alephant::Publisher::Request::LogHelper

        attr_reader :connection, :base_path

        def initialize(connection, base_path)
          @connection = connection
          @base_path  = base_path
          raise InvalidComponentBasePath, base_path unless File.directory? base_path
        end

        def create(component_id, context = {})
          require base_path_for component_id

          klass = mapper_class_for(component_id)
          klass.new(connection, context)
        rescue LoadError => e
          log_error_with_metric(e, component_id, "PublisherRequestDataMapperFactoryInvalidComponentName")
          raise InvalidComponentName, "Invalid component name: #{component_id}"
        rescue NameError => e
          log_error_with_metric(e, component_id, "PublisherRequestDataMapperFactoryInvalidComponentClassName")
          raise InvalidComponentClassName, "Invalid class name #{klass}"
        rescue => e
          log_error_with_metric(e, component_id, "PublisherRequestDataMapperFactoryInvalidComponent")
          raise InvalidComponent, "Name: #{component_id}, Class: #{klass}"
        end

        protected

        def base_path_for(component_id)
          "#{base_path}/#{component_id}/mapper"
        end

        def camalize(snake_case)
          "#{snake_case.split('_').collect(&:capitalize).join}"
        end

        def mapper_class_for(component_id)
          Object.const_get("#{camalize component_id}Mapper")
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
alephant-publisher-request-0.3.0 lib/alephant/publisher/request/data_mapper_factory.rb
alephant-publisher-request-0.2.6 lib/alephant/publisher/request/data_mapper_factory.rb
alephant-publisher-request-0.2.5 lib/alephant/publisher/request/data_mapper_factory.rb