Sha256: fb5620080ea150aabcc8edabb6e58f313e9c96b53a82b82753c31967af6f29da

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module Hanami
  module Model
    module Plugins
      # Transform output into model domain types (entities).
      #
      # @since 0.7.0
      # @api private
      module Mapping
        # Takes the output and applies the transformations
        #
        # @since 0.7.0
        # @api private
        class InputWithMapping < WrappingInput
          # @since 0.7.0
          # @api private
          def initialize(relation, input)
            super
            @mapping = Hanami::Model.configuration.mappings[relation.name.to_sym]
          end

          # Processes the output
          #
          # @since 0.7.0
          # @api private
          def [](value)
            @input[@mapping.process(value)]
          end
        end

        # Class interface
        #
        # @since 0.7.0
        # @api private
        module ClassMethods
          # Builds the output processor
          #
          # @since 0.7.0
          # @api private
          def build(relation, options = {})
            wrapped_input = InputWithMapping.new(relation, options.fetch(:input) { input })
            super(relation, options.merge(input: wrapped_input))
          end
        end

        # @since 0.7.0
        # @api private
        def self.included(klass)
          super

          klass.extend ClassMethods
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hanami-model-1.3.3 lib/hanami/model/plugins/mapping.rb