lib/dense/methods.rb in dense-1.1.4 vs lib/dense/methods.rb in dense-1.1.5

- old
+ new

@@ -10,34 +10,34 @@ end def fetch(o, path, default=::KeyError, &block) pa = Dense::Path.make(path) - r = pa.gather(o).partition(&:first) + hits, misses = pa.gather(o).partition(&:first) - if r[0].empty? + if hits.empty? return pa.narrow( - r[1].collect { |m| call_default_block(o, path, block, m) } + misses.collect { |m| call_default_block(o, path, block, m) } ) if block return pa.narrow( - r[1].collect { |m| default } + misses.collect { |m| default } ) if default != KeyError - fail miss_error(path, r[1].first) + fail miss_error(path, misses.first) end - pa.narrow(r[0].collect { |e| e[2][e[3]] }) + pa.narrow(hits.collect { |e| e[2][e[3]] }) end def set(o, path, value) Dense::Path.make(path) .gather(o) .each { |hit| - fail_miss_error(path, hit) if hit[0] == false + validate(path, hit) if hit[0] == false hit[2][hit[3]] = value } value end @@ -65,11 +65,11 @@ Dense::Path.make(path) .gather(o) .each { |hit| if hit[0] == false n = hit[4].first - fail_miss_error(path, hit) \ + validate(path, hit) \ if n.nil? && ! key_matches_collection?(hit[3], hit[2]) hit[2][hit[3]] = if n.is_a?(String) {} else @@ -85,11 +85,11 @@ def insert(o, path, value) Dense::Path.make(path) .gather(o) .each { |hit| - fail_miss_error(path, hit) if hit[0] == false + validate(path, hit) if hit[0] == false if hit[2].is_a?(Array) hit[2].insert(hit[3], value) else hit[2][hit[3]] = value end } @@ -147,19 +147,37 @@ err.miss = miss err end - def key_error(path, miss) + # IndexError: + # Raised when the given index is invalid. + # KeyError: + # Raised when the specified key is not found. It is a subclass of IndexError. - path1 = Dense::Path.make(miss[1] + [ miss[3] ]).to_s.inspect - path2 = Dense::Path.make(miss[4]).to_s.inspect + def index_error(path, miss) - msg = "found nothing at #{path1}" - msg = "#{msg} (#{path2} remains)" if path2 != '""' + if miss[2].is_a?(Array) || miss[2].is_a?(Hash) - make_error(KeyError, msg, path, miss) + path1 = Dense::Path.make(miss[1] + [ miss[3] ]).to_s.inspect + path2 = Dense::Path.make(miss[4]).to_s.inspect + + msg = "found nothing at #{path1}" + msg = "#{msg} (#{path2} remains)" if path2 != '""' + + make_error(KeyError, msg, path, miss) + + else + + path1 = Dense::Path.make(miss[1]).to_s.inspect + path2 = Dense::Path.make(miss[4]).to_s.inspect + + msg = "found no collection at #{path1} for key #{miss[3].inspect}" + msg = "#{msg} (#{path2} remains)" if path2 != '""' + + make_error(IndexError, msg, path, miss) + end end def type_error(path, miss) key = miss[3].inspect @@ -172,19 +190,21 @@ def miss_error(path, miss) if miss[2].is_a?(Array) && ! miss[3].is_a?(Integer) type_error(path, miss) else - key_error(path, miss) + index_error(path, miss) end end - def fail_miss_error(path, miss) + def validate(path, miss) fail miss_error(path, miss) \ if miss[4].any? fail type_error(path, miss) \ if miss[2].is_a?(Array) && ! miss[2].is_a?(Integer) + + # else, no failure, carry on! end def call_default_block(o, path, block, miss) # [ collection, path,