Method: Path.configure

Defined in:
lib/path/implementation.rb

.configure(config) ⇒ Object

Configures the behavior of #+. The default is :warning.

Path.configure(:+ => :defined) # aliased to Path#/
Path.configure(:+ => :warning) # calls Path#/ but warns
Path.configure(:+ => :error)   # not defined
Path.configure(:+ => :string)  # like String#+. Warns if $VERBOSE (-w)

Parameters:

  • config (Hash)

    a customizable set of options

Options Hash (config):

  • :+ (:defined, :warning, :error, :string)

    the configuration value



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/path/implementation.rb', line 48

def Path.configure(config)
  config = config[:+]
  unless [:defined, :warning, :error, :string].include? config
    raise ArgumentError, "Invalid configuration: #{config.inspect}"
  end
  if @plus_configured
    raise "Path.configure(:+ => ...) has already been called: #{@plus_configured}"
  end
  remove_method :+ if method_defined? :+
  case config
  when :defined
    alias :+ :/
  when :warning
    def +(other)
      warn 'Warning: use of deprecated Path#+ as Path#/: ' <<
           "#{inspect} + #{other.inspect}\n#{caller.first}"
      self / other
    end
  when :error
    # nothing to do, the method has been removed
  when :string
    def +(other)
      warn 'Warning: use of deprecated Path#+ as String#+: ' <<
           "#{inspect} + #{other.inspect}\n#{caller.first}" if $VERBOSE
      Path.new(to_s + other.to_s)
    end
  end
  @plus_configured = caller.first
end