Sha256: 39cf98f1bd02ddffa56dc25ad3869f7e5ff55e1a39fa371215daa7e2c41c23be

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 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

        def log_error(e, component_id, metric)
          log_error_with_metric(e, 'DataMapperFactory#create', component_id, metric)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alephant-publisher-request-0.2.4 lib/alephant/publisher/request/data_mapper_factory.rb
alephant-publisher-request-0.2.3 lib/alephant/publisher/request/data_mapper_factory.rb