Sha256: f16d2ce0e055e38a8edbfd36498c881063a91553ae7a7b4a2a30a5371e05a519

Contents?: true

Size: 1.13 KB

Versions: 31

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

class ReeMapper::ErrorWithLocation < ReeMapper::Error
  attr_accessor :location

  contract(String, String, ArrayOf[String]  => Any)
  def initialize(message, location = nil, field_name_parts = [])
    @message = message
    @location = location
    @field_name_parts = field_name_parts
  end

  contract(String => nil)
  def prepend_field_name(part)
    @field_name_parts.unshift part
    nil
  end

  contract(None => Nilor[String])
  def field_name
    @field_name_parts.reduce { "#{_1}[#{_2}]" }
  end

  if ENV["RUBY_ENV"] == "test"

    contract(None => String)
    def message
      msg = @message

      if location
        msg = "#{msg}, located at #{location}"
      end

      return msg if @field_name_parts.empty?

      "`#{field_name}` #{msg}"
    end

  else

    def message
      return @message if @field_name_parts.empty?

      "`#{field_name}` #{@message}"
    end

    def full_message(...)
      msg = super
      return msg if location.nil?

      last_sym_idx = msg.index(/\).*\n/)
      return msg if last_sym_idx.nil?

      msg.insert(last_sym_idx + 1, ", located at #{location}")
    end

  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
ree_lib-1.0.124 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb