lib/dense/path.rb in dense-1.1.0 vs lib/dense/path.rb in dense-1.1.1

- old
+ new

@@ -6,11 +6,11 @@ def initialize(s) @original = s fail ArgumentError.new( - "Argument is a #{s.class}, not a String" + "argument is a #{s.class}, not a String" ) unless s.is_a?(String) s = ".#{s}" \ unless s[0, 1] == '[' || s[0, 2] == '.[' @@ -122,26 +122,37 @@ def _resolve_hash_key(o, k) return [ nil ] unless o.is_a?(Array) be = k[:start] || 0 - en = k[:end] || o.length - 1 - st = k[:step] || 1 + en, st = + if co = k[:count] + [ be + co - 1, 1 ] + else + [ k[:end] || o.length - 1, k[:step] || 1 ] + end + Range.new(be, en).step(st).to_a end def _resolve_key(o, k) return _resolve_hash_key(o, k) if k.is_a?(Hash) - return [ k.to_s ] if o.is_a?(Hash) + if o.is_a?(Hash) - case k - when /\Afirst\z/i then [ 0 ] - when /\Alast\z/i then [ -1 ] - else [ k ] + return o.keys.select { |ok| ok =~ k } if k.is_a?(Regexp) + [ k.to_s ] + + else # o is a Array + + case k + when /\Afirst\z/i then [ 0 ] + when /\Alast\z/i then [ -1 ] + else [ k ] + end end end def _resolve_keys(o, k) @@ -249,16 +260,23 @@ def _to_s(elt, in_array) case elt when Hash - s = [ "#{elt[:start]}:#{elt[:end]}", elt[:step] ].compact.join(':') + s = + if elt[:count] + [ elt[:start], elt[:count] ].collect(&:to_s).join(',') + else + [ "#{elt[:start]}:#{elt[:end]}", elt[:step] ].compact.join(':') + end in_array ? s : "[#{s}]" when Array - "[#{elt.map { |e| _to_s(e, true) }.join(',')}#{elt.size < 2 ? ',' : ''}]" + "[#{elt.map { |e| _to_s(e, true) }.join(';')}#{elt.size < 2 ? ';' : ''}]" when String _str_to_s(elt, in_array) + when Regexp + _rex_to_s(elt) when :star '*' when :dot '.' when :dotstar @@ -275,8 +293,15 @@ s = elt.to_s return "\\#{s}" if s == '.' || s == '*' return "[#{elt.inspect}]" if s =~ /["']/ s + end + + def _rex_to_s(r) + + r.fixed_encoding? ? + r.inspect + (Dense::Path::Parser::R_ENCODINGS[r.encoding.name] || '') : + r.inspect end end # Dense::Path