Sha256: 11dd0342aa864a13355b010ae0583be30fe29b2b3c8984afecc44930d1c3bb32

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

module BloodContracts::Core
  # Mapper in form of refinement type, transforms the value using mapper_klass
  class MapValue < Refined
    class << self
      # Any callable object which you prefer to turn value into other form
      #
      # @param [Class, #call]
      # @return [Class]
      #
      attr_accessor :mapper_klass

      # Generates meta-class with predefined mapper_klass
      #
      # @param mapper_klass [Class, callable] callable object that will
      #   transform the value
      # @return [MapValue]
      #
      def with(mapper_klass)
        type = Class.new(self)
        type.mapper_klass = mapper_klass
        type
      end
    end

    # Always successful matching process which transforms the value and
    # store it in the context
    #
    # @return [Refined]
    #
    def match
      context[:mapper_input] = value
      context[:mapped_value] =
        self.class.mapper_klass.call(**context[:mapper_input])
      self
    end

    # Mapped representation of the value
    #
    # @return [Object]
    #
    def mapped
      match.context[:mapped_value]
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
blood_contracts-ext-0.1.4 lib/blood_contracts/core/map_value.rb
blood_contracts-ext-0.1.3 lib/blood_contracts/core/map_value.rb
blood_contracts-ext-0.1.2 lib/blood_contracts/core/map_value.rb
blood_contracts-ext-0.1.1 lib/blood_contracts/core/map_value.rb
blood_contracts-ext-0.1.0 lib/blood_contracts/core/map_value.rb