class Safrano::OData::Collection

Constants

D
DJ_CLOSE
DJ_OPEN
EMPTYH

Attributes

cx[RW]
inlinecount[R]
modelk[R]
params[R]

url params

uparms[R]

url parameters processing object (mostly convert to sequel exprs). exposed for testing only

Public Class Methods

new(modelk) click to toggle source
# File lib/odata/collection.rb, line 21
def initialize(modelk)
  @modelk = modelk
end

Public Instance Methods

allowed_transitions() click to toggle source
# File lib/odata/collection.rb, line 25
def allowed_transitions
  @modelk.allowed_transitions
end
dataset() click to toggle source

this is redefined in NavigatedCollection

# File lib/odata/collection.rb, line 39
def dataset
  @modelk.dataset
end
find_by_odata_key(pkid) click to toggle source

pkid can be a single value for single-pk models, or an array. type checking/convertion is done in check_odata_key_type

# File lib/odata/collection.rb, line 57
def find_by_odata_key(pkid)
  lkup = @modelk.pk_lookup_expr(pkid)
  dataset[lkup]
end
initialize_dataset(dtset = nil) click to toggle source
# File lib/odata/collection.rb, line 62
def initialize_dataset(dtset = nil)
  @cx = @cx || dtset || @modelk
end
initialize_uparms() click to toggle source
# File lib/odata/collection.rb, line 66
def initialize_uparms
  @uparms = UrlParameters4Coll.new(@cx, @params)
end
odata_get(req) click to toggle source

on model class level we return the collection

# File lib/odata/collection.rb, line 136
def odata_get(req)
  @params = @params || req.params
  initialize_dataset
  initialize_uparms
  @uparms.check_all.if_valid { |_ret|
    odata_get_apply_params.if_valid { |_ret|
      odata_get_output(req)
    }
  }.tap_error { |e| return e.odata_get(req) }.result
end
odata_get_apply_params() click to toggle source
# File lib/odata/collection.rb, line 70
def odata_get_apply_params
  @uparms.apply_to_dataset(@cx).map_result! do |dataset|
    @cx = dataset
    odata_get_inlinecount_w_sequel
    if (skipp = @params['$skip'])
      @cx = @cx.offset(skipp) if skipp != '0'
    end
    @cx = @cx.limit(@params['$top']) if @params['$top']

    @cx
  end
end
odata_get_inlinecount_w_sequel() click to toggle source
# File lib/odata/collection.rb, line 106
def odata_get_inlinecount_w_sequel
  return unless (icp = @params['$inlinecount'])

  @inlinecount = if icp == 'allpages'
                   if @modelk.is_a? Sequel::Model::ClassMethods
                     @cx.count
                   else
                     @cx.dataset.count
                   end
                 end
end
odata_get_output(req) click to toggle source

finally return the requested output according to format, options etc

# File lib/odata/collection.rb, line 119
def odata_get_output(req)
  output = if req.walker.do_count
             [200, CT_TEXT, @cx.count.to_s]
           elsif req.accept?(APPJSON)
             # json is default content type so we dont need to specify it here again
             if req.walker.do_links
               [200, EMPTYH, [to_odata_links_json(service: req.service)]]
             else
               [200, EMPTYH, [to_odata_json(request: req)]]
             end
           else # TODO: other formats
             406
           end
  Contract.valid(output)
end
odata_post(req) click to toggle source
# File lib/odata/collection.rb, line 147
def odata_post(req)
  @modelk.odata_create_entity_and_relation(req)
end
to_odata_json(request:) click to toggle source
# File lib/odata/collection.rb, line 88
def to_odata_json(request:)
  template = @modelk.output_template(expand_list: @uparms.expand.template,
                                     select: @uparms.select)
  # TODO: Error handling if database contains binary BLOB data that cant be
  # interpreted as UTF-8 then JSON will fail here
  innerj = request.service.get_coll_odata_h(array: @cx.all,
                                            template: template,
                                            icount: @inlinecount).to_json

  "#{DJ_OPEN}#{innerj}#{DJ_CLOSE}"
end
transition_count(_match_result) click to toggle source
# File lib/odata/collection.rb, line 33
def transition_count(_match_result)
  [self, :end_with_count]
end
transition_end(_match_result) click to toggle source
# File lib/odata/collection.rb, line 29
def transition_end(_match_result)
  Safrano::Transition::RESULT_END
end
transition_id(match_result) click to toggle source
# File lib/odata/collection.rb, line 43
def transition_id(match_result)
  if (rawid = match_result[1])
    @modelk.parse_odata_key(rawid).tap_error do
      return Safrano::Transition::RESULT_BAD_REQ_ERR
    end.if_valid do |casted_id|
      (y = find_by_odata_key(casted_id)) ? [y, :run] : Safrano::Transition::RESULT_NOT_FOUND_ERR
    end
  else
    Safrano::Transition::RESULT_SERVER_TR_ERR
  end
end