Module: OMF::SFA::Resource::Base::InstanceMethods
- Included in:
- CompGroup, GroupComponent, Ip, OComponent, OLease
- Defined in:
- lib/omf-sfa/resource/sfa_base.rb
Overview
ClassMethods
Instance Method Summary collapse
- #_to_sfa_property_hash(value, pdef, href2obj, opts) ⇒ Object
- #_to_sfa_property_xml(pname, value, res_el, pdef, obj2id, opts) ⇒ Object
- #_to_sfa_xml(parent, obj2id, opts) ⇒ Object
- #_xml_name ⇒ Object
- #component_id ⇒ Object
-
#component_manager_id ⇒ Object
end.
-
#default_domain ⇒ Object
end.
- #from_sfa(resource_el, context = {}) ⇒ Object
- #resource_type ⇒ Object
- #sfa_class ⇒ Object
-
#sfa_id ⇒ Object
end.
-
#sfa_property(name) ⇒ Object
end.
-
#to_sfa_hash(href2obj = {}, opts = {}) ⇒ Object
Return all SFA related properties as a hash.
- #to_sfa_hash_short(opts = {}) ⇒ Object
- #to_sfa_short_xml(parent) ⇒ Object
-
#to_sfa_xml(parent = nil, obj2id = {}, opts = {}) ⇒ Object
opts:detail - detail to reveal about resource 0..min, 99 ..
Instance Method Details
#_to_sfa_property_hash(value, pdef, href2obj, opts) ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 388 def _to_sfa_property_hash(value, pdef, href2obj, opts) if !value.kind_of?(String) && value.kind_of?(Enumerable) value.collect do |o| if o.respond_to? :to_sfa_hash o.to_sfa_hash_short(opts) else o.to_s end end else value.to_s end end |
#_to_sfa_property_xml(pname, value, res_el, pdef, obj2id, opts) ⇒ Object
459 460 461 462 463 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 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 459 def _to_sfa_property_xml(pname, value, res_el, pdef, obj2id, opts) pname = self.class._sfa_add_ns(pname, pdef) if pdef[:attribute] res_el.set_attribute(pname, value.to_s) elsif aname = pdef[:attr_value] el = res_el.add_child(Nokogiri::XML::Element.new(pname, res_el.document)) el.set_attribute(aname, value.to_s) else if pdef[:inline] == true cel = res_el else cel = res_el.add_child(Nokogiri::XML::Element.new(pname, res_el.document)) end if !value.kind_of?(String) && value.kind_of?(Enumerable) value.each do |v| if v.respond_to?(:to_sfa_xml) v.to_sfa_xml(cel, obj2id, opts) else el = cel.add_child(Nokogiri::XML::Element.new(pname, cel.document)) #puts (el.methods - Object.new.methods).sort.inspect el.content = v.to_s #el.set_attribute('type', (pdef[:type] || 'string').to_s) end end else cel.content = value.to_s #cel.set_attribute('type', (pdef[:type] || 'string').to_s) end end end |
#_to_sfa_xml(parent, obj2id, opts) ⇒ Object
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 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 414 def _to_sfa_xml(parent, obj2id, opts) new_element = parent.add_child(Nokogiri::XML::Element.new(_xml_name(), parent.document)) if parent.document == parent # first time around, add namespace self.class.sfa_add_namespaces_to_document(parent) end defs = self.class.sfa_defs() if (id = obj2id[self]) new_element.set_attribute('idref', id) return parent end id = sfa_id() obj2id[self] = id new_element.set_attribute('id', id) #if detail_level > 0 #if href = self.href(opts) # new_element.set_attribute('omf:href', href) #end level = opts[:level] ? opts[:level] : 0 opts[:level] = level + 1 defs.keys.sort.each do |key| next if key.start_with?('_') pdef = defs[key] if (ilevel = pdef[:include_level]) #next if level > ilevel end #puts ">>>> #{k} <#{self}> #{pdef.inspect}" value = send(key.to_sym) #puts "#{k} <#{v}> #{pdef.inspect}" if value.nil? value = pdef[:default] end unless value.nil? #if detail_level > 0 || k == 'component_name' if value.is_a?(Time) value = value.xmlschema # xs:dateTime end _to_sfa_property_xml(key, value, new_element, pdef, obj2id, opts) #end end end opts[:level] = level # restore original level new_element end |
#_xml_name ⇒ Object
321 322 323 324 325 326 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 321 def _xml_name() if pd = self.sfa_class return pd end self.class.name.gsub('::', '_') end |
#component_id ⇒ Object
270 271 272 273 274 275 276 277 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 270 def component_id unless id = attribute_get(:component_id) #self.component_id ||= GURN.create(self.uuid.to_s, self) #return GURN.create(self.uuid.to_s, { :model => self.class }) return GURN.create(self.urn, { :model => self.class }) end id end |
#component_manager_id ⇒ Object
end
283 284 285 286 287 288 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 283 def component_manager_id unless uuid = attribute_get(:component_manager_id) return (self.class.default_component_manager_id ||= GURN.create("authority+am")) end uuid end |
#default_domain ⇒ Object
end
294 295 296 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 294 def default_domain self.class.default_domain() end |
#from_sfa(resource_el, context = {}) ⇒ Object
493 494 495 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 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 493 def from_sfa(resource_el, context = {}) els = {} # this doesn't work with generic namespaces resource_el.children.each do |el| next unless el.is_a? Nokogiri::XML::Element unless ns = el.namespace raise "Missing namespace declaration for '#{el}'" end unless ns.href == SFA_NAMESPACE_URI warn "WARNING: '#{el.name}' Can't handle non-default namespaces '#{ns.href}'" next end (els[el.name] ||= []) << el end #puts ">>>>> #{self} - #{self.class.sfa_defs.keys}" self.class.sfa_defs.each do |name, props| mname = "_from_sfa_#{name}_property_xml".to_sym #puts "#{self}; Checking for #{mname} - #{props[:attribute]} - #{self.respond_to?(mname)}" if self.respond_to?(mname) send(mname, resource_el, props, context) elsif props[:attribute] == true #puts "Checking '#{name}'" next if name.to_s == 'component_name' # skip that one for the moment if v = resource_el.attributes[props[:attribute_name] || name] #puts "#{name}::#{name.class} = #{v}--#{v.class} (#{props})" name = props[:prop_name] || name send("#{name}=".to_sym, v.value) end elsif arr = els[name.to_s] #puts "Handling #{name} -- #{props}" name = props[:prop_name] || name arr.each do |el| #puts "#{self}: #{name} = #{el.text}" send("#{name}=".to_sym, el.text) end # else # puts "Don't know how to handle '#{name}' (#{props})" end end unless self.save raise "Couldn't save resource '#{self}'" end return self end |
#resource_type ⇒ Object
266 267 268 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 266 def resource_type sfa_class end |
#sfa_class ⇒ Object
308 309 310 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 308 def sfa_class() self.class.sfa_class() end |
#sfa_id ⇒ Object
end
303 304 305 306 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 303 def sfa_id() #@sfa_id ||= "c#{object_id}" self.uuid.to_s end |
#sfa_property(name) ⇒ Object
end
317 318 319 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 317 def sfa_property(name) instance_variable_get("sfa_#{name}") end |
#to_sfa_hash(href2obj = {}, opts = {}) ⇒ Object
Return all SFA related properties as a hash
opts
:detail - detail to reveal about resource 0..min, 99 .. max
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 342 def to_sfa_hash(href2obj = {}, opts = {}) res = to_sfa_hash_short(opts) res['comp_gurn'] = self.urn href = res['href'] if obj = href2obj[href] # have described myself before raise "Different object with same href '#{href}'" unless obj == self return res end href2obj[href] = self defs = self.class.sfa_defs() #puts ">> #{defs.inspect}" defs.keys.sort.each do |k| next if k.start_with?('_') pdef = defs[k] pname = pdef[:prop_name] ||k v = send(pname.to_sym) if v.nil? v = pdef[:default] end #puts "!#{k} => '#{v}' - #{self}" unless v.nil? m = "_to_sfa_#{k}_property_hash".to_sym if self.respond_to? m res[k] = send(m, v, pdef, href2obj, opts) #puts ">>>> #{k}::#{res[k]}" else res[k] = _to_sfa_property_hash(v, pdef, href2obj, opts) end end end res end |
#to_sfa_hash_short(opts = {}) ⇒ Object
377 378 379 380 381 382 383 384 385 386 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 377 def to_sfa_hash_short(opts = {}) uuid = self.uuid.to_s href_prefix = opts[:href_prefix] ||= default_href_prefix { 'name' => self.name, 'uuid' => uuid, 'sfa_class' => sfa_class(), 'href' => "#{href_prefix}/#{uuid}" } end |
#to_sfa_short_xml(parent) ⇒ Object
328 329 330 331 332 333 334 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 328 def to_sfa_short_xml(parent) n = parent.add_child(Nokogiri::XML::Element.new('resource', parent.document)) n.set_attribute('type', _xml_name()) n.set_attribute('status', 'unimplemented') n.set_attribute('name', component_name()) n end |
#to_sfa_xml(parent = nil, obj2id = {}, opts = {}) ⇒ Object
opts
:detail - detail to reveal about resource 0..min, 99 .. max
406 407 408 409 410 411 412 |
# File 'lib/omf-sfa/resource/sfa_base.rb', line 406 def to_sfa_xml(parent = nil, obj2id = {}, opts = {}) if parent.nil? parent = Nokogiri::XML::Document.new end _to_sfa_xml(parent, obj2id, opts) parent end |