\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /proc/self/root/proc/self/root/opt/chef/embedded/lib/ruby/gems/2.7.0/gems/hana-1.3.6/lib/

Viewing File: hana.rb

# frozen_string_literal: true

module Hana
  VERSION = '1.3.6'

  class Pointer
    include Enumerable

    class Exception < StandardError
    end

    class FormatError < Exception
    end

    def initialize path
      @path = Pointer.parse path
    end

    def each(&block); @path.each(&block); end

    def eval object
      Pointer.eval @path, object
    end

    ESC = {'^/' => '/', '^^' => '^', '~0' => '~', '~1' => '/'} # :nodoc:

    def self.eval list, object
      list.inject(object) { |o, part|
        return nil unless o

        if Array === o
          raise Patch::IndexError unless part =~ /\A(?:\d|[1-9]\d+)\Z/
          part = part.to_i
        end
        o[part]
      }
    end

    def self.parse path
      return [''] if path == '/'
      return []   if path == ''

      unless path.start_with? '/'
        raise FormatError, "JSON Pointer should start with a slash"
      end

      parts = path.sub(/^\//, '').split(/(?<!\^)\//).each { |part|
        part.gsub!(/\^[\/^]|~[01]/) { |m| ESC[m] }
      }

      parts.push("") if path[-1] == '/'
      parts
    end
  end

  class Patch
    class Exception < StandardError
    end

    class FailedTestException < Exception
      attr_accessor :path, :value

      def initialize path, value
        super "expected #{value} at #{path}"
        @path  = path
        @value = value
      end
    end

    class OutOfBoundsException < Exception
    end

    class ObjectOperationOnArrayException < Exception
    end

    class IndexError < Exception
    end

    class MissingTargetException < Exception
    end

    class InvalidPath < Exception
    end

    def initialize is
      @is = is
    end

    VALID = Hash[%w{ add move test replace remove copy }.map { |x| [x,x]}] # :nodoc:

    def apply doc
      @is.inject(doc) { |d, ins|
        send VALID.fetch(ins[OP].strip) { |k|
          raise Exception, "bad method `#{k}`"
        }, ins, d
      }
    end

    private

    PATH  = 'path' # :nodoc:
    FROM  = 'from' # :nodoc:
    VALUE = 'value' # :nodoc:
    OP    = 'op' # :nodoc:

    def add ins, doc
      unless ins.key?('path')
        raise Hana::Patch::InvalidPath, "missing 'path' parameter"
      end

      path = ins['path']

      unless path
        raise Hana::Patch::InvalidPath, "null is not valid value for 'path'"
      end

      list = Pointer.parse path
      key  = list.pop
      dest = Pointer.eval list, doc
      obj  = ins.fetch VALUE

      raise(MissingTargetException, ins['path']) unless dest

      if key
        add_op dest, key, obj
      else
        if doc.equal? dest
          doc = obj
        else
          dest.replace obj
        end
      end
      doc
    end

    def move ins, doc
      from     = Pointer.parse ins.fetch FROM
      to       = Pointer.parse ins[PATH]
      from_key = from.pop
      key      = to.pop
      src      = Pointer.eval from, doc
      dest     = Pointer.eval to, doc

      unless Array === src
        unless src.key? from_key
          raise Hana::Patch::MissingTargetException
        end
      end

      obj = rm_op src, from_key
      add_op dest, key, obj
      doc
    end

    def copy ins, doc
      from     = Pointer.parse ins.fetch FROM
      to       = Pointer.parse ins[PATH]
      from_key = from.pop
      key      = to.pop
      src      = Pointer.eval from, doc
      dest     = Pointer.eval to, doc

      if Array === src
        raise Patch::IndexError unless from_key =~ /\A\d+\Z/
        obj = src.fetch from_key.to_i
      else
        begin
          obj = src.fetch from_key
        rescue KeyError => e
          raise Hana::Patch::MissingTargetException, e.message
        end
      end

      add_op dest, key, obj
      doc
    end

    def test ins, doc
      expected = Pointer.new(ins[PATH]).eval doc

      unless expected == ins.fetch(VALUE)
        raise FailedTestException.new(ins[VALUE], ins[PATH])
      end
      doc
    end

    def replace ins, doc
      list = Pointer.parse ins[PATH]
      key  = list.pop
      obj  = Pointer.eval list, doc

      return ins.fetch VALUE unless key

      if Array === obj
        raise Patch::IndexError unless key =~ /\A\d+\Z/
        obj[key.to_i] = ins.fetch VALUE
      else
        raise Patch::MissingTargetException unless obj
        obj[key] = ins.fetch VALUE
      end
      doc
    end

    def remove ins, doc
      list = Pointer.parse ins[PATH]
      key  = list.pop
      obj  = Pointer.eval list, doc
      rm_op obj, key
      doc
    end

    def check_index obj, key
      return -1 if key == '-'

      raise ObjectOperationOnArrayException unless key =~ /\A-?\d+\Z/
      idx = key.to_i
      raise OutOfBoundsException if idx > obj.length || idx < 0
      idx
    end

    def add_op dest, key, obj
      if Array === dest
        dest.insert check_index(dest, key), obj
      else
        dest[key] = obj
      end
    end

    def rm_op obj, key
      if Array === obj
        raise Patch::IndexError unless key =~ /\A\d+\Z/
        key = key.to_i
        raise Patch::OutOfBoundsException if key >= obj.length
        obj.delete_at key
      else
        raise Patch::IndexError unless obj&.key? key
        obj.delete key
      end
    end
  end
end