module Safrano::EntityBase

this will be mixed in the Model classes (subclasses of Sequel Model)

Constants

D
DELETE_REL_AND_ENTY
DJ_CLOSE
DJ_OPEN

Attributes

params[R]

Public Instance Methods

==(other) click to toggle source

for testing only?

# File lib/odata/entity.rb, line 63
def ==(other)
  ((self.class.type_name == other.class.type_name) and (@values == other.values))
end
copy_request_infos(req) click to toggle source
# File lib/odata/entity.rb, line 127
def copy_request_infos(req)
  @params = @inactive_query_params ? EMPTY_HASH : req.params
  @do_links = req.walker.do_links
  @uparms = UrlParameters4Single.new(self, @params)
end
inactive_query_params() click to toggle source
# File lib/odata/entity.rb, line 155
def inactive_query_params
  @inactive_query_params = true
  self # chaining
end
nav_coll() click to toggle source
nav_values() click to toggle source
odata_delete_relation_and_entity(req, assoc, parent) click to toggle source
# File lib/odata/entity.rb, line 165
def odata_delete_relation_and_entity(req, assoc, parent)
  if parent
    if req.in_changeset
      # in-changeset requests get their own transaction
      DELETE_REL_AND_ENTY.call(self, assoc, parent)
    else
      db.transaction do
        DELETE_REL_AND_ENTY.call(self, assoc, parent)
      end
    end
  else
    destroy(transaction: false)
  end
rescue StandardError => e
  raise SequelAdapterError.new(e)
end
odata_get(req) click to toggle source

Finally Process REST verbs…

# File lib/odata/entity.rb, line 149
def odata_get(req)
  copy_request_infos(req)
  @uparms.check_all.tap_valid { return odata_get_output(req) }
         .tap_error { |e| return e.odata_get(req) }
end
odata_get_output(req) click to toggle source
# File lib/odata/entity.rb, line 133
def odata_get_output(req)
  if req.walker.media_value
    odata_media_value_get(req)
  elsif req.accept?(APPJSON)
    # json is default content type so we dont need to specify it here again
    if req.walker.do_links
      [200, EMPTY_HASH, [to_odata_onelink_json(service: req.service)]]
    else
      [200, EMPTY_HASH, [to_odata_json(request: req)]]
    end
  else # TODO: other formats
    415
  end
end
odata_patch(req) click to toggle source
# File lib/odata/entity.rb, line 222
def odata_patch(req)
  req.with_parsed_data do |data|
    data.delete('__metadata')

    # validate payload column names
    if (invalid = self.class.invalid_hash_data?(data))
      ::Safrano::Request::ON_CGST_ERROR.call(req)
      return [422, EMPTY_HASH, ['Invalid attribute name: ', invalid.to_s]]
    end
    # TODO: check values/types

    my_data_fields = self.class.data_fields

    if req.in_changeset
      set_fields(data, my_data_fields, missing: :skip)
      save(transaction: false)
    else
      update_fields(data, my_data_fields, missing: :skip)
    end
    # patch should return 204 + no content
    ARY_204_EMPTY_HASH_ARY
  end
end
odata_post(req) click to toggle source

TODO: differentiate between POST/PUT/PATCH/MERGE

# File lib/odata/entity.rb, line 183
def odata_post(req)
  if req.walker.media_value
    odata_media_value_put(req)
  elsif req.accept?(APPJSON)
    data.delete('__metadata')

    if req.in_changeset
      set_fields(data, self.class.data_fields, missing: :skip)
      save(transaction: false)
    else
      update_fields(data, self.class.data_fields, missing: :skip)
    end

    [202, EMPTY_HASH, to_odata_post_json(service: req.service)]
  else # TODO: other formats
    415
  end
end
odata_put(req) click to toggle source
# File lib/odata/entity.rb, line 202
def odata_put(req)
  if req.walker.media_value
    odata_media_value_put(req)
  elsif req.accept?(APPJSON)
    data = JSON.parse(req.body.read)
    data.delete('__metadata')

    if req.in_changeset
      set_fields(data, self.class.data_fields, missing: :skip)
      save(transaction: false)
    else
      update_fields(data, self.class.data_fields, missing: :skip)
    end

    ARY_204_EMPTY_HASH_ARY
  else # TODO: other formats
    415
  end
end
selected_values_for_odata(cols) click to toggle source
# File lib/odata/entity.rb, line 108
def selected_values_for_odata(cols)
  allvals = values_for_odata
  selvals = {}
  cols.map(&:to_sym).each { |k| selvals[k] = allvals[k] if allvals.key?(k) }
  selvals
end
to_odata_array_json(request:) click to toggle source

some clients wrongly expect post payload with the new entity in an array TODO quirks array mode !

# File lib/odata/entity.rb, line 117
def to_odata_array_json(request:)
  innerj = request.service.get_coll_odata_h(array: [self],
                                            template: self.class.default_template).to_json
  "#{DJ_OPEN}#{innerj}#{DJ_CLOSE}"
end
to_odata_json(request:) click to toggle source

Json formatter for a single entity (probably OData V1/V2 like)

# File lib/odata/entity.rb, line 94
def to_odata_json(request:)
  template = self.class.output_template(expand_list: @uparms.expand.template,
                                        select: @uparms.select)
  innerj = request.service.get_entity_odata_h(entity: self,
                                              template: template).to_json
  "#{DJ_OPEN}#{innerj}#{DJ_CLOSE}"
end
type_name() click to toggle source
# File lib/odata/entity.rb, line 123
def type_name
  self.class.type_name
end
uri() click to toggle source
# File lib/odata/entity.rb, line 84
def uri
  @odata_pk ||= "(#{pk_uri})"
  "#{self.class.uri}#{@odata_pk}"
end