Sha256: 6a514247622266d911089e2f032312d872f9377a3c4304b8be2606d2f97e7ade

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

module Alephant
  module Publisher
    module Request
      class DataMapperFactory
        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
          raise InvalidComponentName, "Invalid component name: #{component_id}"
        rescue NameError
          raise InvalidComponentClassName, "Invalid class name #{klass}"
        rescue
          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

2 entries across 2 versions & 1 rubygems

Version Path
alephant-publisher-request-0.0.3 lib/alephant/publisher/request/data_mapper_factory.rb
alephant-publisher-request-0.0.2 lib/alephant/publisher/request/data_mapper_factory.rb