Sha256: 8e67e99d96f20aa225550eb79c964874131e8d98e96d28957bb6d9789fb7e7a1

Contents?: true

Size: 1.93 KB

Versions: 6

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

require 'forwardable'

require_relative 'configuration'
require_relative 'conversions'
require_relative 'conversion_target'

module Necromancer
  # A class used by Necromancer to provide user interace
  #
  # @api public
  class Context
    extend Forwardable

    def_delegators :"@conversions", :register

    # Create a context.
    #
    # @api private
    def initialize(&block)
      block.call(configuration) if block_given?
      @conversions = Conversions.new(configuration)
      @conversions.load
    end

    # The configuration object.
    #
    # @example
    #   converter = Necromancer.new
    #   converter.configuration.strict = true
    #
    # @return [Necromancer::Configuration]
    #
    # @api public
    def configuration
      @configuration ||= Configuration.new
    end

    # Yields global configuration to a block.
    #
    # @yield [Necromancer::Configuration]
    #
    # @example
    #   converter = Necromancer.new
    #   converter.configure do |config|
    #     config.strict true
    #   end
    #
    # @api public
    def configure
      yield configuration if block_given?
    end

    # Converts the object
    # @param [Object] object
    #   any object to be converted
    #
    # @api public
    def convert(object = ConversionTarget::UndefinedValue, &block)
      ConversionTarget.for(conversions, object, block)
    end

    # Check if this converter can convert source to target
    #
    # @param [Object] source
    #   the source class
    # @param [Object] target
    #   the target class
    #
    # @return [Boolean]
    #
    # @api public
    def can?(source, target)
      !conversions[source, target].nil?
    rescue NoTypeConversionAvailableError
      false
    end

    # Inspect this context
    #
    # @api public
    def inspect
      %(#<#{self.class}@#{object_id} @config=#{configuration}>)
    end

    protected

    attr_reader :conversions
  end # Context
end # Necromancer

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
pokedex-terminal-0.2.8 vendor/bundle/ruby/2.7.0/gems/necromancer-0.5.1/lib/necromancer/context.rb
pokedex-terminal-0.2.7 vendor/bundle/ruby/2.7.0/gems/necromancer-0.5.1/lib/necromancer/context.rb
pokedex-terminal-0.2.6 vendor/bundle/ruby/2.7.0/gems/necromancer-0.5.1/lib/necromancer/context.rb
pokedex-terminal-0.2.5 vendor/bundle/ruby/2.7.0/gems/necromancer-0.5.1/lib/necromancer/context.rb
pokedex-terminal-0.2.4 vendor/bundle/ruby/2.7.0/gems/necromancer-0.5.1/lib/necromancer/context.rb
necromancer-0.5.1 lib/necromancer/context.rb