Class: OMF::SFA::Resource::OPropertyArray

Inherits:
Object
  • Object
show all
Defined in:
lib/omf-sfa/resource/oproperty.rb

Instance Method Summary collapse

Constructor Details

#initialize(resource, name) ⇒ OPropertyArray

Returns a new instance of OPropertyArray.



363
364
365
366
# File 'lib/omf-sfa/resource/oproperty.rb', line 363

def initialize(resource, name)
  @resource = resource
  @name = name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



359
360
361
# File 'lib/omf-sfa/resource/oproperty.rb', line 359

def method_missing(m, *args, &block)
  self.to_a().send(m, &block)
end

Instance Method Details

#<<(val) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/omf-sfa/resource/oproperty.rb', line 268

def <<(val)
  #puts "------------------ Checking #{@resource.name} => #{@name}"
  if @on_set_block
    val = @on_set_block.call(val)
    return if val.nil? #
  end
  if val.is_a? OResource
    # Make sure we only have a single reference
    return if OProperty.count(name: @name, resource: @resource, value: val) > 0
  end
  #puts ">>> Adding #{val} to #{@name} - #{@on_set_block}"
  p = OProperty.create(name: @name, o_resource: @resource, value: val)
  if @on_modified_block
    @on_modified_block.call(val, true)
  end
  #p.value = val
  self
end

#[](index) ⇒ Object



314
315
316
317
318
319
320
# File 'lib/omf-sfa/resource/oproperty.rb', line 314

def [](index)
  if p = OProperty.all(name: @name, o_resource: @resource).all[index]
    p.value
  else
    nil
  end
end

#clearObject

Delete all members



301
302
303
304
# File 'lib/omf-sfa/resource/oproperty.rb', line 301

def clear
  OProperty.all(name: @name, o_resource: @resource).destroy
  self
end

#delete(val) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/omf-sfa/resource/oproperty.rb', line 287

def delete(val)
  # this is rather inefficient
  p = OProperty.all(name: @name, o_resource: @resource).find do |p|
    p.value == val
  end
  return nil unless p
  p.destroy
  if @on_removed_block
    @on_removed_block.call(val)
  end
  val
end

#empty?Boolean

Returns:

  • (Boolean)


327
328
329
# File 'lib/omf-sfa/resource/oproperty.rb', line 327

def empty?
  self.length == 0
end

#lengthObject



322
323
324
325
# File 'lib/omf-sfa/resource/oproperty.rb', line 322

def length
  #raise "LENGTH"
  OProperty.count(name: @name, resource: @resource)
end

#on_modified(&block) ⇒ Object

Callback to support ‘reverse’ operation



332
333
334
335
# File 'lib/omf-sfa/resource/oproperty.rb', line 332

def on_modified(&block)
  #raise "Not implemented"
  @on_modified_block = block
end

#on_removed(&block) ⇒ Object



337
338
339
# File 'lib/omf-sfa/resource/oproperty.rb', line 337

def on_removed(&block)
  @on_removed_block = block
end

#on_set(&block) ⇒ Object



341
342
343
# File 'lib/omf-sfa/resource/oproperty.rb', line 341

def on_set(&block)
  @on_set_block = block
end

#to_aObject



345
346
347
# File 'lib/omf-sfa/resource/oproperty.rb', line 345

def to_a
  OProperty.all(name: @name, o_resource: @resource).all.map {|p| p.value }
end

#to_json(*args) ⇒ Object



349
350
351
352
353
# File 'lib/omf-sfa/resource/oproperty.rb', line 349

def to_json(*args)
  OProperty.all(name: @name, o_resource: @resource).map do |p|
    p.value
  end.to_json(*args)
end

#to_sObject



355
356
357
# File 'lib/omf-sfa/resource/oproperty.rb', line 355

def to_s
  "<#{self.class}: name=#{@name} resource=#{@resource.name || @resource.uuid} >"
end