Class: OMF::SFA::Resource::OResource
- Inherits:
-
Object
- Object
- OMF::SFA::Resource::OResource
- Extended by:
- Base::Loggable
- Includes:
- DataMapper::Resource, DataMapper::Validations, Base::Loggable
- Defined in:
- lib/omf-sfa/resource/oresource.rb
Overview
This is the basic resource from which all other resources descend.
Note: Can’t call it ‘Resource’ to avoid any confusion with DataMapper::Resource
Direct Known Subclasses
AbstractService, DiskImage, Ip, OComponent, OGroup, OLease, OReference, Project, SliverType, User
Constant Summary collapse
- @@default_href_prefix =
@@default_href_prefix = ‘somehost/resources/’
'/resources'
- @@href_resolvers =
{}
- @@oprops =
{}
Class Method Summary collapse
- .href_resolver(&block) ⇒ Object
-
.init(persistence_url = 'sqlite::memory:') ⇒ Object
belongs_to :account, :model => ‘OAccount’, :child_key => [ :account_id ], :required => false.
- .json_create(o) ⇒ Object
- .oproperty(name, type, opts = {}) ⇒ Object
-
.prop_all(query, opts = {}) ⇒ Object
param opts :limit param opts :offset.
Instance Method Summary collapse
-
#_dirty_self? ⇒ Object
alias_method :_dirty_children?, :dirty_children? def dirty_children? puts “CHECKING CHILDREN DIRTY: #_dirty_children?” _dirty_children? end.
- #_oprops_to_hash(h, objs, opts) ⇒ Object
-
#all_resources(set = Set.new) ⇒ Object
Add this resource and all contained to
set
. -
#clone ⇒ Object
Clone this resource this resource.
- #default_href_prefix ⇒ Object
- #destroy ⇒ Object
- #destroy! ⇒ Object
- #dirty_self? ⇒ Boolean
- #each_resource(&block) ⇒ Object
-
#group? ⇒ Boolean
Return true if this resource is a Group.
- #href(opts = {}) ⇒ Object
- #oproperties_as_hash ⇒ Object
- #oproperty(pname) ⇒ Object
- #oproperty_array_get(pname, set_filter) ⇒ Object
- #oproperty_get(pname) ⇒ Object (also: #[])
- #oproperty_set(pname, value, type = nil) ⇒ Object (also: #[]=)
-
#remove_from_all_groups ⇒ Object
Remove this resource from all groups it currently belongs.
- #resource_type ⇒ Object
-
#status ⇒ Object
Return the status of the resource.
- #to_hash(objs = {}, opts = {}) ⇒ Object
- #to_hash_brief(opts = {}) ⇒ Object
- #to_hash_long(h, objs = {}, opts = {}) ⇒ Object
- #to_json(*a) ⇒ Object
- #uuid ⇒ Object
Class Method Details
.href_resolver(&block) ⇒ Object
85 86 87 |
# File 'lib/omf-sfa/resource/oresource.rb', line 85 def self.href_resolver(&block) @@href_resolvers[self] = block end |
.init(persistence_url = 'sqlite::memory:') ⇒ Object
belongs_to :account, :model => ‘OAccount’, :child_key => [ :account_id ], :required => false
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/omf-sfa/resource/oresource.rb', line 71 def self.init(persistence_url = 'sqlite::memory:') DataMapper::Logger.new($stdout, :info) # :note DataMapper.setup(:default, persistence_url) DataMapper::Model.raise_on_save_failure = true DataMapper.finalize #DataMapper.auto_upgrade! require 'dm-migrations' DataMapper.auto_migrate! self end |
.json_create(o) ⇒ Object
def self.from_json(o)
puts "FROM_JSON"
klass = o['json_class']
id = o['id']
eval(klass).first(:id => id)
end
435 436 437 438 439 440 |
# File 'lib/omf-sfa/resource/oresource.rb', line 435 def self.json_create(o) klass = o['json_class'] id = o['id'] r = eval(klass).first(:id => id) r end |
.oproperty(name, type, opts = {}) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/omf-sfa/resource/oresource.rb', line 89 def self.oproperty(name, type, opts = {}) name = name.to_s if self.properties.find {|p| p.name.to_s == name } raise "Can't define an oproperty '#{name}' as it is already used as a base property." end # should check if +name+ is already used op = @@oprops[self] ||= {} opts[:__type__] = type if opts[:functional] == false # property is an array pname = DataMapper::Inflector.pluralize(name) op[pname] = opts define_method pname do res = oproperty_array_get(pname, opts[:set_filter]) if res == nil oproperty_set(pname, []) # We make a oproperty_get in order to get the extended Array with # the overidden '<<' method. Check module ArrayProxy in oproperty.rb res = oproperty_get(pname) end #puts "PROPERTY_GET(#{pname}) #{res}" if rev_m = opts[:inverse] res.on_modified do |v, added| if added if v.respond_to?(m = "#{rev_m}_add".to_sym) v.send(m, self) elsif v.respond_to?(m = "#{rev_m}=".to_sym) v.send(m, self) else raise "Can't find any setter '#{rev_m}' on '#{v}'" end else # TODO: should remove this one form the reverse side end end end res end # Add method to add a single element to a non-functional property. This # helps other entities to learn if this property is functional or not. # define_method "#{pname}_add" do |v| oproperty_array_get(pname, opts[:set_filter]) << v #self.send(pname.to_sym) << v end define_method "#{pname}=" do |v| unless v.is_a? Array raise "Property '#{pname}' in '#{self.class}' requires an Array in setter - #{v.inspect}" end #res = self.send(pname.to_sym) res = oproperty_array_get(pname, opts[:set_filter]) res.clear # clear any old values #puts ">>>> SET: #{v} - #{res}" v.each {|it| res << it } res end else op[name] = opts define_method name do res = oproperty_get(name) if res.nil? res = opts[:default] if res.nil? && (self.respond_to?(m = "default_#{name}".to_sym)) res = send(m) end end res end define_method "#{name}=" do |v| if sf = opts[:set_filter] v = self.send(sf, v) return if v.nil? # end return if (old = oproperty_get(name)) == v oproperty_set(name, v) if rev_m = opts[:inverse] if v if v.respond_to?(m = "#{rev_m}_add".to_sym) v.send(m, self) elsif v.respond_to?(m = "#{rev_m}=".to_sym) v.send(m, self) else raise "Can't find any setter '#{rev_m}' on '#{v}'" end else # TODO: should remove this one form the reverse side end end end end end |
Instance Method Details
#_dirty_self? ⇒ Object
alias_method :_dirty_children?, :dirty_children? def dirty_children? puts “CHECKING CHILDREN DIRTY: #_dirty_children?” _dirty_children? end
322 |
# File 'lib/omf-sfa/resource/oresource.rb', line 322 alias_method :_dirty_self?, :dirty_self? |
#_oprops_to_hash(h, objs, opts) ⇒ Object
484 485 486 487 488 489 490 491 492 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 |
# File 'lib/omf-sfa/resource/oresource.rb', line 484 def _oprops_to_hash(h, objs, opts) klass = self.class while klass if op = @@oprops[klass] op.each do |k, v| k = k.to_sym unless (value = send(k)).nil? #puts "OPROPS_TO_HAHS(#{k}): #{value}::#{value.class}--#{oproperty_get(k)}" #puts "OPROPS_TO_HAHS(#{k}): #{opts[:level]} >= #{opts[:max_level]}" if value.is_a? OResource href_only = opts[:level] >= opts[:max_level] value = href_only ? value.href : value.to_hash(objs, opts) #value = value.to_hash(objs, opts) end if value.is_a? Time value = value.iso8601 end if value.kind_of? Array next if value.empty? opts = opts.merge(level: opts[:level] + 1) value = value.collect do |e| #(e.kind_of? OResource) ? (href_only ? e.href : e.to_hash(objs, opts)) : e (e.kind_of? OResource) ? e.to_hash(objs, opts) : e end end h[k] = value end end end klass = klass.superclass end h end |
#all_resources(set = Set.new) ⇒ Object
Add this resource and all contained to set
.
352 353 354 355 |
# File 'lib/omf-sfa/resource/oresource.rb', line 352 def all_resources(set = Set.new) set << self set end |
#clone ⇒ Object
Clone this resource this resource. However, the clone will have a unique UUID
199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/omf-sfa/resource/oresource.rb', line 199 def clone() clone = self.class.new attributes.each do |k, v| next if k == :id || k == :uuid clone.attribute_set(k, DataMapper::Ext.try_dup(v)) end oproperties.each do |p| clone.oproperty_set(p.name, DataMapper::Ext.try_dup(p.value)) end clone.uuid = UUIDTools::UUID.random_create return clone end |
#default_href_prefix ⇒ Object
480 481 482 |
# File 'lib/omf-sfa/resource/oresource.rb', line 480 def default_href_prefix @@default_href_prefix end |
#destroy ⇒ Object
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/omf-sfa/resource/oresource.rb', line 379 def destroy #debug "ORESOURCE destroy #{self}" self.remove_from_all_groups #if p = self.provided_by # pa = p.provides # pa.delete self # r = p.save # i = 0 #end # first destroy all properties self.oproperties.all().each do |p| #debug "ORESOURCE destroying property '#{p.inspect}'" r = p.destroy r end #p = self.oproperties.all() super end |
#destroy! ⇒ Object
400 401 402 403 404 |
# File 'lib/omf-sfa/resource/oresource.rb', line 400 def destroy! #debug "ORESOURCE destroy! #{self}" destroy super end |
#dirty_self? ⇒ Boolean
323 324 325 326 327 328 329 330 |
# File 'lib/omf-sfa/resource/oresource.rb', line 323 def dirty_self? #puts "CHECKING DIRTY #{_dirty_self?}" return true if _dirty_self? o_properties.each do |p| return true if p.dirty_self? end false end |
#each_resource(&block) ⇒ Object
312 313 314 |
# File 'lib/omf-sfa/resource/oresource.rb', line 312 def each_resource(&block) # resources don't contain other resources, groups do' end |
#group? ⇒ Boolean
Return true if this resource is a Group
340 341 342 |
# File 'lib/omf-sfa/resource/oresource.rb', line 340 def group? false end |
#href(opts = {}) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/omf-sfa/resource/oresource.rb', line 220 def href(opts = {}) klass = self.class while (resolver = @@href_resolvers[klass]).nil? break if (klass = klass.superclass) == Object end if resolver href = resolver.call(self, opts) end unless href href = "/resources/#{self.uuid.to_s}" end # if prefix = opts[:name_prefix] # href = "#{prefix}/#{self.name || self.uuid.to_s}" # elsif opts[:href_use_class_prefix] # #href = "/#{self.resource_type}/#{self.name || self.uuid.to_s}" # href = "/#{self.resource_type.pluralize}/#{self.uuid.to_s}" # elsif prefix = opts[:href_prefix] || @@default_href_prefix # href = "#{prefix}/#{self.uuid.to_s}" # end href end |
#oproperties_as_hash ⇒ Object
304 305 306 307 308 309 310 |
# File 'lib/omf-sfa/resource/oresource.rb', line 304 def oproperties_as_hash res = {} oproperties.each do |p| res[p.name] = p.value end res end |
#oproperty(pname) ⇒ Object
258 259 260 |
# File 'lib/omf-sfa/resource/oresource.rb', line 258 def oproperty(pname) self.oproperties.first(:name => pname.to_sym) end |
#oproperty_array_get(pname, set_filter) ⇒ Object
289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/omf-sfa/resource/oresource.rb', line 289 def oproperty_array_get(pname, set_filter) pname = pname.to_sym unless ap = (@array_properties ||= {})[pname] ap = OPropertyArray.new(self, pname) if set_filter # TODO: The following block can most likely be optimised away ap.on_set do |v| self.send(set_filter, v) end end @array_properties[pname] = ap end ap end |
#oproperty_get(pname) ⇒ Object Also known as: []
263 264 265 266 267 268 269 270 |
# File 'lib/omf-sfa/resource/oresource.rb', line 263 def oproperty_get(pname) #puts "OPROPERTY_GET: pname:'#{pname}':#{pname.class}" pname = pname.to_sym return self.name if pname == :name prop = self.oproperties.first(:name => pname) prop.nil? ? nil : prop.value end |
#oproperty_set(pname, value, type = nil) ⇒ Object Also known as: []=
273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/omf-sfa/resource/oresource.rb', line 273 def oproperty_set(pname, value, type = nil) #puts "OPROPERTY_SET pname:'#{pname}', value:'#{value.class}'::#{type}, self:'#{self.inspect}'" pname = pname.to_sym if pname == :name self.name = value else self.save #puts ">>>>>" + @@oprops[self.class][pname.to_s].to_s prop = self.oproperties.first_or_create(:name => pname) prop.value = value #puts ">> #{prop.inspect}" end value end |
#remove_from_all_groups ⇒ Object
Remove this resource from all groups it currently belongs.
347 348 349 |
# File 'lib/omf-sfa/resource/oresource.rb', line 347 def remove_from_all_groups self.group_memberships.each {|m| m.destroy} end |
#resource_type ⇒ Object
243 244 245 246 247 248 |
# File 'lib/omf-sfa/resource/oresource.rb', line 243 def resource_type() unless rt = attribute_get(:resource_type) rt = self.class.to_s.split('::')[-1].downcase end rt end |
#status ⇒ Object
Return the status of the resource. Should be one of: configuring, ready, failed, and unknown
254 255 256 |
# File 'lib/omf-sfa/resource/oresource.rb', line 254 def status 'unknown' end |
#to_hash(objs = {}, opts = {}) ⇒ Object
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/omf-sfa/resource/oresource.rb', line 442 def to_hash(objs = {}, opts = {}) max_level = opts[:max_level] ||= 0 level = opts[:level] ||= 0 if level > max_level return self.href end #debug "to_hash(self):opts: #{opts.keys.inspect}::#{objs.keys.inspect}::" h = to_hash_brief(opts) return h if objs.key?(self) objs[self] = true return h if opts[:brief] || (level >= max_level) opts = opts.merge(level: level + 1) opts[:brief] = true if level > max_level #puts ">>>> #{opts}" to_hash_long(h, objs, opts) h end |
#to_hash_brief(opts = {}) ⇒ Object
462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/omf-sfa/resource/oresource.rb', line 462 def to_hash_brief(opts = {}) h = {} uuid = h[:uuid] = self.uuid.to_s h[:href] = self.href name = self.name if name && ! name.start_with?('_') h[:name] = self.name end h[:type] = self.resource_type h end |
#to_hash_long(h, objs = {}, opts = {}) ⇒ Object
474 475 476 477 478 |
# File 'lib/omf-sfa/resource/oresource.rb', line 474 def to_hash_long(h, objs = {}, opts = {}) h[:urn] = self.urn if self.urn _oprops_to_hash(h, objs, opts) h end |
#to_json(*a) ⇒ Object
406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/omf-sfa/resource/oresource.rb', line 406 def to_json(*a) to_hash_brief().to_json(*a) # unless self.id # # need an id, means I haven't been saved yet # save # end # { # 'json_class' => self.class.name, # 'id' => self.id # }.to_json(*a) end |
#uuid ⇒ Object
213 214 215 216 217 218 |
# File 'lib/omf-sfa/resource/oresource.rb', line 213 def uuid() unless uuid = attribute_get(:uuid) uuid = self.uuid = UUIDTools::UUID.random_create end uuid end |