Module: Safrano::EntityClassBase
- Includes:
- Transitions
- Included in:
- EntityClassMultiPK, EntityClassSinglePK
- Defined in:
- lib/odata/model_ext.rb
Overview
class methods. They Make heavy use of Sequel::Model functionality we will add this to our Model classes with “extend” –> self is the Class
Defined Under Namespace
Modules: Transitions
Constant Summary collapse
- ONLY_INTEGER_RGX =
/\A[+-]?\d+\z/.freeze
- MAX_DEPTH =
6
- MAX_TYPE_REPETITION =
2
- EMPTYH =
{}.freeze
- KEYPRED_URL_REGEXP =
/\A\(\s*((?:guid)?'?[\w=,\-'\s]+'?)\s*\)(.*)/.freeze
- GUIDRGX =
/guid'([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})'/i.freeze
- CREATE_AND_SAVE_ENTY_AND_REL =
lambda do |new_entity, assoc, parent| # in-changeset requests get their own transaction case assoc[:type] when :one_to_many, :one_to_one Safrano.create_nav_relation(new_entity, assoc, parent) new_entity.save(transaction: false) when :many_to_one new_entity.save(transaction: false) Safrano.create_nav_relation(new_entity, assoc, parent) parent.save(transaction: false) # else # not supported end end
Instance Attribute Summary collapse
-
#casted_cols ⇒ Object
readonly
Returns the value of attribute casted_cols.
-
#cols_metadata ⇒ Object
readonly
store cols metata here in the model (sub)-class.
-
#data_fields ⇒ Object
readonly
Returns the value of attribute data_fields.
-
#default_template ⇒ Object
readonly
Returns the value of attribute default_template.
-
#deferred_iblock ⇒ Object
initialising block of code to be executed at end of ServerApp.publish_service after all model classes have been registered (without the associations/relationships) typically the block should contain the publication of the associations.
-
#entity_id_url_regexp ⇒ Object
readonly
Returns the value of attribute entity_id_url_regexp.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#nav_collection_attribs ⇒ Object
readonly
Returns the value of attribute nav_collection_attribs.
-
#nav_collection_url_regexp ⇒ Object
readonly
Returns the value of attribute nav_collection_url_regexp.
-
#nav_entity_attribs ⇒ Object
readonly
Returns the value of attribute nav_entity_attribs.
-
#nav_entity_url_regexp ⇒ Object
readonly
Returns the value of attribute nav_entity_url_regexp.
-
#odata_upk_parts ⇒ Object
readonly
Returns the value of attribute odata_upk_parts.
-
#pk_castfunc ⇒ Object
Returns the value of attribute pk_castfunc.
-
#type_mappings ⇒ Object
allows to override standard types.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#add_metadata_navs_rexml(schema_enty, relman) ⇒ Object
and their Nav attributes == Sequel Model association.
-
#add_metadata_rexml(schema) ⇒ Object
add metadata xml to the passed REXML schema object.
-
#add_nav_prop_collection(assoc_symb, attr_name_str = nil) ⇒ Object
this functionally similar to the Sequel Rels (many_to_one etc) We need to base this on the Sequel rels, or extend them.
- #add_nav_prop_single(assoc_symb, attr_name_str = nil) ⇒ Object
- #attrib_path_valid?(path) ⇒ Boolean
- #attribute_path_list(depth_path = []) ⇒ Object
-
#build_all_props_list ⇒ Object
list of table columns + all nav attribs –> all props.
- #build_attribute_path_list ⇒ Object
- #build_casted_cols(service) ⇒ Object
- #build_default_template ⇒ Object
- #build_expand_path_list ⇒ Object
- #build_type_name ⇒ Object
- #build_uri(uribase) ⇒ Object
-
#cast_odata_val(val, pk_cast) ⇒ Object
super-minimal type check, but better as nothing.
- #default_entity_set_name ⇒ Object
-
#entity_set_name ⇒ Object
default for entity_set_name is @default_entity_set_name.
- #execute_deferred_iblock ⇒ Object
- #expand_path_list(depth_path = []) ⇒ Object
- #expand_path_valid?(path) ⇒ Boolean
-
#finalize_publishing(service) ⇒ Object
build_casted_cols(service).
- #find_invalid_props(propsset) ⇒ Object
- #invalid_hash_data?(data) ⇒ Boolean
-
#metadata_nav_rexml_attribs(assoc, to_klass, relman) ⇒ Object
metadata REXML data for a single Nav attribute.
-
#new_from_hson_h(hash) ⇒ Object
Factory json-> Model Object instance.
- #odata_create_save_entity_and_rel(req, new_entity, assoc, parent) ⇒ Object
-
#output_template(expand_list:, select: Safrano::SelectBase::ALL) ⇒ Object
Recursive this method is performance critical.
- #prepare_fields ⇒ Object
- #prepare_pk ⇒ Object
- #reset ⇒ Object
- #return_as_collection_descriptor ⇒ Object
- #return_as_instance_descriptor ⇒ Object
-
#transition_attribute_regexp ⇒ Object
A regexp matching all allowed attributes of the Entity (eg ID|name|size etc… ) at start position and returning the rest.
-
#type_name ⇒ Object
convention: entityType is the namepsaced Ruby Model class –> name is just to_s Warning: for handling Navigation relations, we use anonymous collection classes dynamically subtyped from a Model class, and in such an anonymous class the class-name is not the OData Type.
-
#with_attribute(asymb, &proc) ⇒ Object
allow to override default type settings on attribute level for example use Edm.Guid instead of Binary(Blob) or Edm.DateTimeOffset instead of Edm.DateTime.
Methods included from Transitions
#allowed_transitions, #build_allowed_transitions, #build_entity_allowed_transitions, #entity_allowed_transitions
Methods included from Transitions::GetNextTrans::BySimpleDetect
Instance Attribute Details
#casted_cols ⇒ Object (readonly)
Returns the value of attribute casted_cols.
36 37 38 |
# File 'lib/odata/model_ext.rb', line 36 def casted_cols @casted_cols end |
#cols_metadata ⇒ Object (readonly)
store cols metata here in the model (sub)-class. Initially we stored this infos (eg. edm_types etc) directly into sequels db_schema hash. But this hash is on the upper Sequel::Model(table) class and is shared by all subclasses. By storing it separately here we are less dependant from Sequel, and have less issues with testing with multiples models class derived from same Sequel::Model(table)
43 44 45 |
# File 'lib/odata/model_ext.rb', line 43 def @cols_metadata end |
#data_fields ⇒ Object (readonly)
Returns the value of attribute data_fields.
32 33 34 |
# File 'lib/odata/model_ext.rb', line 32 def data_fields @data_fields end |
#default_template ⇒ Object (readonly)
Returns the value of attribute default_template.
33 34 35 |
# File 'lib/odata/model_ext.rb', line 33 def default_template @default_template end |
#deferred_iblock ⇒ Object
initialising block of code to be executed at end of ServerApp.publish_service after all model classes have been registered (without the associations/relationships) typically the block should contain the publication of the associations
49 50 51 |
# File 'lib/odata/model_ext.rb', line 49 def deferred_iblock @deferred_iblock end |
#entity_id_url_regexp ⇒ Object (readonly)
Returns the value of attribute entity_id_url_regexp.
29 30 31 |
# File 'lib/odata/model_ext.rb', line 29 def entity_id_url_regexp @entity_id_url_regexp end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
37 38 39 |
# File 'lib/odata/model_ext.rb', line 37 def namespace @namespace end |
#nav_collection_attribs ⇒ Object (readonly)
Returns the value of attribute nav_collection_attribs.
30 31 32 |
# File 'lib/odata/model_ext.rb', line 30 def nav_collection_attribs @nav_collection_attribs end |
#nav_collection_url_regexp ⇒ Object (readonly)
Returns the value of attribute nav_collection_url_regexp.
27 28 29 |
# File 'lib/odata/model_ext.rb', line 27 def nav_collection_url_regexp @nav_collection_url_regexp end |
#nav_entity_attribs ⇒ Object (readonly)
Returns the value of attribute nav_entity_attribs.
31 32 33 |
# File 'lib/odata/model_ext.rb', line 31 def nav_entity_attribs @nav_entity_attribs end |
#nav_entity_url_regexp ⇒ Object (readonly)
Returns the value of attribute nav_entity_url_regexp.
28 29 30 |
# File 'lib/odata/model_ext.rb', line 28 def nav_entity_url_regexp @nav_entity_url_regexp end |
#odata_upk_parts ⇒ Object (readonly)
Returns the value of attribute odata_upk_parts.
35 36 37 |
# File 'lib/odata/model_ext.rb', line 35 def odata_upk_parts @odata_upk_parts end |
#pk_castfunc ⇒ Object
Returns the value of attribute pk_castfunc.
54 55 56 |
# File 'lib/odata/model_ext.rb', line 54 def pk_castfunc @pk_castfunc end |
#type_mappings ⇒ Object
allows to override standard types
52 53 54 |
# File 'lib/odata/model_ext.rb', line 52 def type_mappings @type_mappings end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
34 35 36 |
# File 'lib/odata/model_ext.rb', line 34 def uri @uri end |
Instance Method Details
#add_metadata_navs_rexml(schema_enty, relman) ⇒ Object
and their Nav attributes == Sequel Model association
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/odata/model_ext.rb', line 218 def (schema_enty, relman) @nav_entity_attribs&.each do |ne, klass| nattr = (ne, klass, relman) schema_enty.add_element('NavigationProperty', nattr) end @nav_collection_attribs&.each do |nc, klass| nattr = (nc, klass, relman) schema_enty.add_element('NavigationProperty', nattr) end end |
#add_metadata_rexml(schema) ⇒ Object
add metadata xml to the passed REXML schema object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/odata/model_ext.rb', line 183 def (schema) enty = if @media_handler schema.add_element('EntityType', 'Name' => to_s, 'HasStream' => 'true') else schema.add_element('EntityType', 'Name' => to_s) end # with their properties db_schema.each do |pnam, prop| = @cols_metadata[pnam] if prop[:primary_key] == true enty.add_element('Key').add_element('PropertyRef', 'Name' => pnam.to_s) end attrs = { 'Name' => pnam.to_s, # 'Type' => Safrano.get_edm_type(db_type: prop[:db_type]) } 'Type' => [:edm_type] } attrs['Nullable'] = 'false' if prop[:allow_null] == false attrs['Precision'] = '0' if [:edm_type] == 'Edm.DateTime' enty.add_element('Property', attrs) end enty end |
#add_nav_prop_collection(assoc_symb, attr_name_str = nil) ⇒ Object
this functionally similar to the Sequel Rels (many_to_one etc) We need to base this on the Sequel rels, or extend them
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/odata/model_ext.rb', line 305 def add_nav_prop_collection(assoc_symb, attr_name_str = nil) @nav_collection_attribs = (@nav_collection_attribs || {}) @nav_collection_attribs_keys = (@nav_collection_attribs_keys || []) # DONE: Error handling. This requires that associations # have been properly defined with Sequel before assoc = all_association_reflections.find do |a| a[:name] == assoc_symb && a[:model] == self end raise Safrano::API::ModelAssociationNameError.new(self, assoc_symb) unless assoc attr_class = assoc[:class_name].constantize lattr_name_str = (attr_name_str || assoc_symb.to_s) # check duplicate attributes names raise Safrano::API::ModelDuplicateAttributeError.new(self, lattr_name_str) if @columns.include? lattr_name_str.to_sym if @nav_entity_attribs_keys raise Safrano::API::ModelDuplicateAttributeError.new(self, lattr_name_str) if @nav_entity_attribs_keys.include? lattr_name_str end @nav_collection_attribs[lattr_name_str] = attr_class @nav_collection_attribs_keys << lattr_name_str @nav_collection_url_regexp = @nav_collection_attribs_keys.join('|') end |
#add_nav_prop_single(assoc_symb, attr_name_str = nil) ⇒ Object
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/odata/model_ext.rb', line 331 def add_nav_prop_single(assoc_symb, attr_name_str = nil) @nav_entity_attribs = (@nav_entity_attribs || {}) @nav_entity_attribs_keys = (@nav_entity_attribs_keys || []) # DONE: Error handling. This requires that associations # have been properly defined with Sequel before assoc = all_association_reflections.find do |a| a[:name] == assoc_symb && a[:model] == self end raise Safrano::API::ModelAssociationNameError.new(self, assoc_symb) unless assoc attr_class = assoc[:class_name].constantize lattr_name_str = (attr_name_str || assoc_symb.to_s) # check duplicate attributes names raise Safrano::API::ModelDuplicateAttributeError.new(self, lattr_name_str) if @columns.include? lattr_name_str.to_sym if @nav_collection_attribs_keys raise Safrano::API::ModelDuplicateAttributeError.new(self, lattr_name_str) if @nav_collection_attribs_keys.include? lattr_name_str end @nav_entity_attribs[lattr_name_str] = attr_class @nav_entity_attribs_keys << lattr_name_str @nav_entity_url_regexp = @nav_entity_attribs_keys.join('|') end |
#attrib_path_valid?(path) ⇒ Boolean
117 118 119 |
# File 'lib/odata/model_ext.rb', line 117 def attrib_path_valid?(path) @attribute_path_list.include? path end |
#attribute_path_list(depth_path = []) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/odata/model_ext.rb', line 147 def attribute_path_list(depth_path = []) ret = @columns_str.dup # break circles return ret if depth_path.size > MAX_DEPTH return ret if depth_path.count(self) > MAX_TYPE_REPETITION @nav_entity_attribs&.each do |a, k| ret.concat(k.attribute_path_list(depth_path + [self]).map { |kc| "#{a}/#{kc}" }) end @nav_collection_attribs&.each do |a, k| ret.concat(k.attribute_path_list(depth_path + [self]).map { |kc| "#{a}/#{kc}" }) end ret end |
#build_all_props_list ⇒ Object
list of table columns + all nav attribs –> all props
134 135 136 137 138 139 |
# File 'lib/odata/model_ext.rb', line 134 def build_all_props_list @all_props = @columns_str.dup (@all_props += @nav_entity_attribs_keys.map(&:to_s)) if @nav_entity_attribs (@all_props += @nav_collection_attribs_keys.map(&:to_s)) if @nav_collection_attribs @all_props = @all_props.to_set end |
#build_attribute_path_list ⇒ Object
141 142 143 |
# File 'lib/odata/model_ext.rb', line 141 def build_attribute_path_list @attribute_path_list = attribute_path_list end |
#build_casted_cols(service) ⇒ Object
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
# File 'lib/odata/model_ext.rb', line 371 def build_casted_cols(service) # cols needed catsting before final json output @casted_cols = {} db_schema.each do |col, props| # first check if we have user-defined regexp based global type mapping usermap = nil dbtyp = props[:db_type] = @cols_metadata[col] if service.type_mappings.values.find { |map| usermap = map.match(dbtyp) } [:edm_type] = usermap.edm_type if usermap.castfunc @casted_cols[col] = usermap.castfunc next # this will override our rules below ! end end # attribute specific type mapping if (colmap = @type_mappings[col]) [:edm_type] = colmap.edm_type if colmap.castfunc @casted_cols[col] = colmap.castfunc next # this will override our rules below ! end end if [:edm_precision] && ([:edm_type] =~ /\AEdm.Decimal\(/i) # we save the precision and/or scale in the lambda (binding!) @casted_cols[col] = if [:edm_scale] lambda { |x| # not sure if these copies are really needed, but feels better that way # output decimal with precision and scale x&.toDecimalPrecisionScaleString([:edm_precision], [:edm_scale]) } else lambda { |x| # not sure if these copies are really needed, but feels better that way # output decimal with precision only x&.toDecimalPrecisionString([:edm_precision]) } end next end if [:edm_type] == 'Edm.Decimal' @casted_cols[col] = ->(x) { x&.toDecimalString } next end # Odata V2 Spec: # Edm.Binary Base64 encoded value of an EDM.Binary value represented as a JSON string # See for example https://services.odata.org/V2/Northwind/Northwind.svc/Categories(1)?$format=json if [:edm_type] == 'Edm.Binary' @casted_cols[col] = ->(x) { Base64.encode64(x) unless x.nil? } # Base64 next end # Odata V2 Spec: # Literal form of Edm.Guid as used in URIs formatted as a JSON string if [:edm_type] == 'Edm.Guid' if props[:type] == :blob # Edm.Guid but as 16 byte binary Blob on DB level, eg in Sqlite @casted_cols[col] = lambda { |x| UUIDTools::UUID.parse_raw(x).to_s unless x.nil? } # Base64 next end end # TODO: check this more in details # NOTE: here we use :type which is the sequel defined ruby-type if props[:type] == :datetime || props[:type] == :date # @casted_cols[col] = ->(x) { x&.iso8601 } @casted_cols[col] = ->(x) { x&.to_edm_json } end end # db_schema.each do |col, props| # check if key needs casting. Important for later entity-uri generation ! return unless primary_key.is_a? Symbol # single key field # guid key as guid'xxx-yyy-zzz' = @cols_metadata[primary_key] if [:edm_type] == 'Edm.Guid' props = db_schema[primary_key] @pk_castfunc = if props[:type] == :blob # Edm.Guid but as 16 byte binary Blob on DB level, eg in Sqlite lambda { |x| "guid'#{UUIDTools::UUID.parse_raw(x)}'" unless x.nil? } else lambda { |x| "guid'#{x}'" unless x.nil? } end else @pk_castfunc = @casted_cols[primary_key] end end |
#build_default_template ⇒ Object
366 367 368 369 |
# File 'lib/odata/model_ext.rb', line 366 def build_default_template @default_template = { meta: EMPTYH, all_values: EMPTYH } @default_template[:deferr] = (@nav_entity_attribs&.keys || []) + (@nav_collection_attribs&.keys || EMPTY_ARRAY) if @nav_entity_attribs || @nav_collection_attribs end |
#build_expand_path_list ⇒ Object
129 130 131 |
# File 'lib/odata/model_ext.rb', line 129 def @expand_path_list = end |
#build_type_name ⇒ Object
70 71 72 73 |
# File 'lib/odata/model_ext.rb', line 70 def build_type_name @type_name = @namespace.to_s.empty? ? to_s : "#{@namespace}.#{self}" @default_entity_set_name = to_s end |
#build_uri(uribase) ⇒ Object
93 94 95 |
# File 'lib/odata/model_ext.rb', line 93 def build_uri(uribase) @uri = "#{uribase}/#{entity_set_name}" end |
#cast_odata_val(val, pk_cast) ⇒ Object
super-minimal type check, but better as nothing
581 582 583 584 585 |
# File 'lib/odata/model_ext.rb', line 581 def cast_odata_val(val, pk_cast) pk_cast ? Contract.valid(pk_cast.call(val)) : Contract.valid(val) # no cast needed, eg for string rescue StandardError => e RubyStandardErrorException.new(e) end |
#default_entity_set_name ⇒ Object
66 67 68 |
# File 'lib/odata/model_ext.rb', line 66 def default_entity_set_name @default_entity_set_name end |
#entity_set_name ⇒ Object
default for entity_set_name is @default_entity_set_name
76 77 78 |
# File 'lib/odata/model_ext.rb', line 76 def entity_set_name @entity_set_name = (@entity_set_name || @default_entity_set_name) end |
#execute_deferred_iblock ⇒ Object
105 106 107 |
# File 'lib/odata/model_ext.rb', line 105 def execute_deferred_iblock instance_eval { @deferred_iblock.call } if @deferred_iblock end |
#expand_path_list(depth_path = []) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/odata/model_ext.rb', line 163 def (depth_path = []) ret = [] ret.concat(@nav_entity_attribs_keys) if @nav_entity_attribs ret.concat(@nav_collection_attribs_keys) if @nav_collection_attribs # break circles return ret if depth_path.size > MAX_DEPTH return ret if depth_path.count(self) > MAX_TYPE_REPETITION @nav_entity_attribs&.each do |a, k| ret.concat(k.(depth_path + [self]).map { |kc| "#{a}/#{kc}" }) end @nav_collection_attribs&.each do |a, k| ret.concat(k.(depth_path + [self]).map { |kc| "#{a}/#{kc}" }) end ret end |
#expand_path_valid?(path) ⇒ Boolean
121 122 123 |
# File 'lib/odata/model_ext.rb', line 121 def (path) @expand_path_list.include? path end |
#finalize_publishing(service) ⇒ Object
build_casted_cols(service)
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/odata/model_ext.rb', line 464 def finalize_publishing(service) build_type_name # build default output template structure build_default_template # add edm_types into metadata store db_schema.each do |col, props| = @cols_metadata.key?(col) ? @cols_metadata[col] : (@cols_metadata[col] = {}) Safrano.add_edm_types(, props) end build_casted_cols(service) # build pk regexps prepare_pk # and finally build the path lists and allowed tr's build_attribute_path_list build_all_props_list build_allowed_transitions build_entity_allowed_transitions # for media finalize_media if respond_to? :finalize_media end |
#find_invalid_props(propsset) ⇒ Object
125 126 127 |
# File 'lib/odata/model_ext.rb', line 125 def find_invalid_props(propsset) (propsset - @all_props) unless propsset.subset?(@all_props) end |
#invalid_hash_data?(data) ⇒ Boolean
568 569 570 |
# File 'lib/odata/model_ext.rb', line 568 def invalid_hash_data?(data) data.keys.map(&:to_sym).find { |ksym| !(@columns.include? ksym) } end |
#metadata_nav_rexml_attribs(assoc, to_klass, relman) ⇒ Object
metadata REXML data for a single Nav attribute
207 208 209 210 211 212 213 214 215 |
# File 'lib/odata/model_ext.rb', line 207 def (assoc, to_klass, relman) from = to_s to = to_klass.to_s relman.(from, to, association_reflection(assoc.to_sym)[:type], @namespace, assoc) end |
#new_from_hson_h(hash) ⇒ Object
Factory json-> Model Object instance
110 111 112 113 114 115 |
# File 'lib/odata/model_ext.rb', line 110 def new_from_hson_h(hash) # enty = new # enty.set_fields(hash, data_fields, missing: :skip) create(hash) # enty.set(hash) end |
#odata_create_save_entity_and_rel(req, new_entity, assoc, parent) ⇒ Object
600 601 602 603 604 605 606 607 608 609 |
# File 'lib/odata/model_ext.rb', line 600 def odata_create_save_entity_and_rel(req, new_entity, assoc, parent) if req.in_changeset # in-changeset requests get their own transaction CREATE_AND_SAVE_ENTY_AND_REL.call(new_entity, assoc, parent) else db.transaction do CREATE_AND_SAVE_ENTY_AND_REL.call(new_entity, assoc, parent) end end end |
#output_template(expand_list:, select: Safrano::SelectBase::ALL) ⇒ Object
Recursive this method is performance critical. Called at least once for every request
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/odata/model_ext.rb', line 236 def output_template(expand_list:, select: Safrano::SelectBase::ALL) return @default_template.dup if .empty? && select.all_props? template = { meta: EMPTYH } = {} = {} deferr = [] # 1. handle non-navigation properties, only consider $select # 2. handle navigations properties, need to check $select and $expand if select.all_props? template[:all_values] = EMPTYH # include all nav attributes --> @nav_entity_attribs&.each do |attr, klass| if .key?(attr) [attr] = klass.output_template(expand_list: [attr]) else deferr << attr end end @nav_collection_attribs&.each do |attr, klass| if .key?(attr) [attr] = klass.output_template(expand_list: [attr]) else deferr << attr end end else template[:selected_vals] = @columns_str & select.props # include only selected nav attribs-->need additional intersection step if @nav_entity_attribs selected_nav_e = @nav_entity_attribs_keys & select.props selected_nav_e&.each do |attr| if .key?(attr) klass = @nav_entity_attribs[attr] [attr] = klass.output_template(expand_list: [attr]) else deferr << attr end end end if @nav_collection_attribs selected_nav_c = @nav_collection_attribs_keys & select.props selected_nav_c&.each do |attr| if .key?(attr) klass = @nav_collection_attribs[attr] [attr] = klass.output_template(expand_list: [attr]) else deferr << attr end end end end template[:expand_e] = template[:expand_c] = template[:deferr] = deferr template end |
#prepare_fields ⇒ Object
559 560 561 562 563 564 565 566 |
# File 'lib/odata/model_ext.rb', line 559 def prepare_fields # columns as strings @columns_str = @columns.map(&:to_s) @data_fields = db_schema.map do |col, cattr| cattr[:primary_key] ? nil : col end.select { |col| col } end |
#prepare_pk ⇒ Object
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 |
# File 'lib/odata/model_ext.rb', line 496 def prepare_pk if primary_key.is_a? Array @pk_names = [] @pk_cast_from_string = {} odata_upk_build = [] primary_key.each do |pk| @pk_names << pk.to_s kvpredicate = case db_schema[pk][:type] when :integer @pk_cast_from_string[pk] = ->(str) { Integer(str) } '?' else "'?'" end odata_upk_build << "#{pk}=#{kvpredicate}" end @odata_upk_parts = odata_upk_build.join(',').split('?') # regex parts for unordered matching @iuk_rgx_parts = primary_key.map do |pk| kvpredicate = case db_schema[pk][:type] when :integer '(\\d+)' else "'(\\w+)'" end [pk, "#{pk}=#{kvpredicate}"] end.to_h # single regex assuming the key fields are ordered ! @iuk_rgx = /\A#{@iuk_rgx_parts.values.join(',\s*')}\z/ @iuk_rgx_parts.transform_values! { |v| /\A#{v}\z/ } else @pk_names = [primary_key.to_s] @pk_cast_from_string = nil kvpredicate = case db_schema[primary_key][:type] when :integer # TODO: Harmonize this with primitive_types.rb convert_from_url @pk_cast_from_string = ->(str) { Integer(str) } /(\d+)/.freeze else = @cols_metadata[primary_key] case [:edm_type] when 'Edm.Guid' if db_schema[primary_key][:type] == :blob # Edm.Guid but as 16byte binary Blob on DB @pk_cast_from_string = lambda { |str| Sequel::SQL::Blob.new(UUIDTools::UUID.parse(str).raw) } end GUIDRGX else /'(\w+)'/.freeze end end @iuk_rgx = /\A\s*#{kvpredicate}\s*\z/ end # @entity_id_url_regexp = /\A\(\s*#{kvpredicate}\s*\)(.*)/.freeze @entity_id_url_regexp = KEYPRED_URL_REGEXP end |
#reset ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/odata/model_ext.rb', line 80 def reset @deferred_iblock = nil @entity_set_name = nil @uri = nil @odata_upk_parts = nil @uparms = nil @params = nil @cx = nil @cols_metadata = {} @type_mappings = {} @pk_castfunc = nil end |
#return_as_collection_descriptor ⇒ Object
97 98 99 |
# File 'lib/odata/model_ext.rb', line 97 def return_as_collection_descriptor Safrano::FunctionImport::ResultDefinition.asEntityColl(self) end |
#return_as_instance_descriptor ⇒ Object
101 102 103 |
# File 'lib/odata/model_ext.rb', line 101 def return_as_instance_descriptor Safrano::FunctionImport::ResultDefinition.asEntity(self) end |
#transition_attribute_regexp ⇒ Object
A regexp matching all allowed attributes of the Entity (eg ID|name|size etc… ) at start position and returning the rest
574 575 576 577 578 |
# File 'lib/odata/model_ext.rb', line 574 def transition_attribute_regexp # db_schema.map { |sch| sch[0] }.join('|') # @columns is from Sequel Model %r{\A/(#{@columns.join('|')})(.*)\z} end |
#type_name ⇒ Object
convention: entityType is the namepsaced Ruby Model class –> name is just to_s Warning: for handling Navigation relations, we use anonymous collection classes dynamically subtyped from a Model class, and in such an anonymous class the class-name is not the OData Type. In these subclass we redefine “type_name” thus when we need the Odata type name, we shall use this method instead of just the collection class name
62 63 64 |
# File 'lib/odata/model_ext.rb', line 62 def type_name @type_name end |
#with_attribute(asymb, &proc) ⇒ Object
allow to override default type settings on attribute level for example use Edm.Guid instead of Binary(Blob) or Edm.DateTimeOffset instead of Edm.DateTime
359 360 361 362 |
# File 'lib/odata/model_ext.rb', line 359 def with_attribute(asymb, &proc) am = AttributeTypeMapping.builder(asymb, &proc).type_mapping @type_mappings[asymb] = am end |