module Safrano::EntityClassMultiPK

special handling for composite key

Public Instance Methods

parse_odata_key(mid) click to toggle source

input fx='aas',fy_w='0001' output true, ['aas', '0001'] … or false when typ-error

# File lib/odata/model_ext.rb, line 598
def parse_odata_key(mid)
  # @iuk_rgx is (needs to be) built on start with
  # collklass.prepare_pk

  # first try to match single regex assuming orderd key fields
  if (md = @iuk_rgx.match(mid))
    md = md.captures
    mdc = []
    primary_key.each_with_index do |pk, i|
      mdc << if (pk_cast = @pk_cast_from_string[pk])
               pk_cast.call(md[i])
             else
               md[i] # no cast needed, eg for string
             end
    end

  else

    # order key fields didnt match--> try and collect/check each parts unordered
    scan_rgx_parts = @iuk_rgx_parts.dup
    mdch = {}

    mid.split(/\s*,\s*/).each { |midpart|
      mval = nil
      mpk, mrgx = scan_rgx_parts.find { |pk, rgx|
        if (md = rgx.match(midpart))
          mval = md[1]
        end
      }
      if mpk and mval
        mdch[mpk] = if (pk_cast = @pk_cast_from_string[mpk])
                      pk_cast.call(mval)
                    else
                      mval # no cast needed, eg for string
                    end
        scan_rgx_parts.delete(mpk)
      else
        return Contract::NOK
      end
    }
    # normally arriving here we have mdch filled with key values pairs,
    # but not in the model key ordering. lets just re-order the values
    mdc = @iuk_rgx_parts.keys.map { |pk| mdch[pk] }

  end
  Contract.valid(mdc)
  # catch remaining convertion errors that we failed to prevent
rescue StandardError => e
  RubyStandardErrorException.new(e)
end
pk_lookup_expr(ids) click to toggle source
# File lib/odata/model_ext.rb, line 592
def pk_lookup_expr(ids)
  primary_key.zip(ids)
end