Class: Necromancer::Context

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/necromancer/context.rb

Overview

A class used by Necromancer to provide user interace

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Context

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a context.



21
22
23
24
25
# File 'lib/necromancer/context.rb', line 21

def initialize(&block)
  block.(configuration) if block_given?
  @conversions = Conversions.new(configuration)
  @conversions.load
end

Instance Method Details

#can?(source, target) ⇒ Boolean

Check if this converter can convert source to target

Parameters:

  • source (Object)

    the source class

  • target (Object)

    the target class

Returns:

  • (Boolean)


74
75
76
77
78
# File 'lib/necromancer/context.rb', line 74

def can?(source, target)
  !conversions[source, target].nil?
rescue NoTypeConversionAvailableError
  false
end

#configurationNecromancer::Configuration

The configuration object.

Examples:

converter = Necromancer.new
converter.configuration.strict = true

Returns:



36
37
38
# File 'lib/necromancer/context.rb', line 36

def configuration
  @configuration ||= Configuration.new
end

#configure {|Necromancer::Configuration| ... } ⇒ Object

Yields global configuration to a block.

Examples:

converter = Necromancer.new
converter.configure do |config|
  config.strict true
end

Yields:



51
52
53
# File 'lib/necromancer/context.rb', line 51

def configure
  yield configuration if block_given?
end

#convert(object = ConversionTarget::UndefinedValue, &block) ⇒ Object

Converts the object

Parameters:

  • object (Object) (defaults to: ConversionTarget::UndefinedValue)

    any object to be converted



60
61
62
# File 'lib/necromancer/context.rb', line 60

def convert(object = ConversionTarget::UndefinedValue, &block)
  ConversionTarget.for(conversions, object, block)
end

#inspectObject

Inspect this context



83
84
85
# File 'lib/necromancer/context.rb', line 83

def inspect
  %(#<#{self.class}@#{object_id} @config=#{configuration}>)
end