Sha256: 2de9430c032146a40f8c136edadb362ab3a5a3fd6f3765dfdc038c05048287ff

Contents?: true

Size: 915 Bytes

Versions: 9

Compression:

Stored size: 915 Bytes

Contents

# frozen_string_literal: true

module Humidifier
  class Config
    class Mapping
      attr_reader :clazz, :mapper

      def initialize(opts = {}, &block)
        @clazz = Humidifier[normalized(opts[:to])]
        raise Error, "Invalid resource: #{opts[:to].inspect}" if @clazz.nil?

        if opts[:using] && block_given?
          raise Error, 'Cannot specify :using and provide an anonymous mapper'
        end

        @mapper = mapper_from(opts, &block)
      end

      def resource_for(name, attributes)
        mapper.resource_for(clazz, name, attributes)
      end

      private

      def mapper_from(opts, &block)
        if opts[:using]
          opts[:using].new
        elsif block_given?
          Class.new(Mapper, &block).new
        else
          Mapper.new
        end
      end

      def normalized(name)
        name.start_with?('AWS') ? name : "AWS::#{name}"
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
humidifier-4.1.1 lib/humidifier/config/mapping.rb
humidifier-4.0.2 lib/humidifier/config/mapping.rb
humidifier-4.0.1 lib/humidifier/config/mapping.rb
humidifier-4.0.0 lib/humidifier/config/mapping.rb
humidifier-3.5.0 lib/humidifier/config/mapping.rb
humidifier-3.4.0 lib/humidifier/config/mapping.rb
humidifier-3.3.0 lib/humidifier/config/mapping.rb
humidifier-3.2.0 lib/humidifier/config/mapping.rb
humidifier-3.1.0 lib/humidifier/config/mapping.rb