Sha256: 2b9b637815ca158dff59e68fa2865d1d5517d2c807c2cd7169614a2115c35be4

Contents?: true

Size: 1.28 KB

Versions: 19

Compression:

Stored size: 1.28 KB

Contents

module Finitio
  class DressHelper

    def initialize
      @stack = []
    end

    def iterate(value)
      value.each.each_with_index do |elm, index|
        deeper(index) do
          yield(elm, index)
        end
      end
    end

    def deeper(location)
      @stack.push(location.to_s)
      res = yield
    ensure
      @stack.pop
      res
    end

    def just_try(rescue_on = TypeError)
      [ true, yield ]
    rescue rescue_on => cause
      #STDERR.puts cause.inspect
      [ false, cause ]
    end

    def try(type, value)
      yield
    rescue TypeError => cause
      failed!(type, value, cause)
    end

    def failed!(type, value, cause = nil)
      msg = default_error_message(type, value)
      raise TypeError.new(msg, cause, location)
    end

    def fail!(msg, cause = nil)
      raise TypeError.new(msg, cause, location)
    end

    def default_error_message(type, value)
      value_s, type_s = value_to_s(value), type_to_s(type)
      "Invalid #{type_s} `#{value_s}`"
    end

    def location
      @stack.join('/')
    end

  private

    def value_to_s(value)
      return 'null' if value.nil?
      s = value.to_s
      s = "#{s[0..25]}..." if s.size>25
      s
    end

    def type_to_s(type)
      type.name.to_s
    end

  end # class DressHelper
end # module Finitio

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
finitio-0.12.0 lib/finitio/support/dress_helper.rb
finitio-0.11.4 lib/finitio/support/dress_helper.rb
finitio-0.11.3 lib/finitio/support/dress_helper.rb
finitio-0.11.2 lib/finitio/support/dress_helper.rb
finitio-0.11.1 lib/finitio/support/dress_helper.rb
finitio-0.10.0 lib/finitio/support/dress_helper.rb
finitio-0.9.1 lib/finitio/support/dress_helper.rb
finitio-0.9.0 lib/finitio/support/dress_helper.rb
finitio-0.8.0 lib/finitio/support/dress_helper.rb
finitio-0.7.0 lib/finitio/support/dress_helper.rb
finitio-0.7.0.pre.rc4 lib/finitio/support/dress_helper.rb
finitio-0.7.0.pre.rc3 lib/finitio/support/dress_helper.rb
finitio-0.7.0.pre.rc2 lib/finitio/support/dress_helper.rb
finitio-0.7.0.pre.rc1 lib/finitio/support/dress_helper.rb
finitio-0.6.1 lib/finitio/support/dress_helper.rb
finitio-0.6.0 lib/finitio/support/dress_helper.rb
finitio-0.5.2 lib/finitio/support/dress_helper.rb
finitio-0.5.1 lib/finitio/support/dress_helper.rb
finitio-0.5.0 lib/finitio/support/dress_helper.rb