Module: GitDS::ModelItemObject

Included in:
DbModelItemObject, FsModelItemObject
Defined in:
lib/git-ds/model/item.rb

Overview

Instance methods used by repo-backed objects.

Note: this is an instance-method module. It should be included, not extended.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#modelObject (readonly)

The GitDS::Model that contains the object.



310
311
312
# File 'lib/git-ds/model/item.rb', line 310

def model
  @model
end

Instance Method Details

#array_property(name, delim = "\n") ⇒ Object Also known as: a_property

Convenience method for reading Array properties.

Note that this returns an Array of Strings.

Note: the default delimiter is what Property uses to encode Array objects. Classes which perform their own encoding can choose a different delimiter.



436
437
438
439
440
441
442
443
# File 'lib/git-ds/model/item.rb', line 436

def array_property(name, delim="\n")
  val = property(name)
  if val && (not val.kind_of? Array)
    val = (val.empty?) ? [] : val.split(delim)
    property_cache[name] = val
  end
  val
end

#bool_property(name) ⇒ Object Also known as: b_property

Convenience method for reading Boolean properties.



421
422
423
424
# File 'lib/git-ds/model/item.rb', line 421

def bool_property(name)
  val = property(name)
  (val && val == 'true')
end

#childrenObject

List children of ModelItemClass. By default, this just lists the properties. Classes should append non-property children (usually other modelitems) to this list.



349
350
351
# File 'lib/git-ds/model/item.rb', line 349

def children
  properties                                                              
end

#clear_cacheObject

Clear all caches in ModelItem instance. In the base class, this just clears the property cache.



365
366
367
# File 'lib/git-ds/model/item.rb', line 365

def clear_cache
  property_cache.clear
end

#deleteObject



462
463
464
465
466
467
# File 'lib/git-ds/model/item.rb', line 462

def delete
  ensure_valid
  @model.delete_item(@path)
  # invalidate object
  @path = nil
end

#float_property(name) ⇒ Object Also known as: f_property

Convenience method for reading Float properties.



397
398
399
400
# File 'lib/git-ds/model/item.rb', line 397

def float_property(name)
  val = property(name)
  val ? property_cache[name] = val.to_f : nil
end

#identObject

Primary key (ident) for instance.



332
333
334
335
# File 'lib/git-ds/model/item.rb', line 332

def ident
  ensure_valid
  @ident
end

#initialize_item(model, path) ⇒ Object



312
313
314
315
316
317
318
319
# File 'lib/git-ds/model/item.rb', line 312

def initialize_item(model, path)
  # NULLS in Path objects cause corrupt trees!
  raise InvalidModelItemPath if (not path) || path =~ /\000/

  @model = model
  @path = path
  @ident = File.basename(path)
end

#integer_property(name) ⇒ Object Also known as: i_property

Convenience method for reading Integer properties.



387
388
389
390
# File 'lib/git-ds/model/item.rb', line 387

def integer_property(name)
  val = property(name)
  val ? property_cache[name] = val.to_i : nil
end

#pathObject

Full path to this item in the repo.



324
325
326
327
# File 'lib/git-ds/model/item.rb', line 324

def path
  ensure_valid
  @path
end

#propertiesObject

Return list of property names.



340
341
342
# File 'lib/git-ds/model/item.rb', line 340

def properties
  self.class.properties.keys
end

#property(name) ⇒ Object

Return the value of a specific property. If the proprty has not been set, nil is returned.

ModelItem classes will generally write property accessors that wrap the call to this method.



376
377
378
379
380
381
382
# File 'lib/git-ds/model/item.rb', line 376

def property(name)
  ensure_valid
  return property_cache[name] if property_cache.include? name
  prop = self.class.properties[name]
  raise "No such property #{name}" if not prop
  property_cache[name] = prop.get(@model, @path)
end

#property_cacheObject

Return Hash of cached property values.



356
357
358
359
# File 'lib/git-ds/model/item.rb', line 356

def property_cache
  ensure_valid
  @property_cache ||= {}
end

#set_property(name, data) ⇒ Object

Set the value of a specific property.

ModelItem classes will generally write property accessors that wrap the call to this method.



453
454
455
456
457
458
# File 'lib/git-ds/model/item.rb', line 453

def set_property(name, data)
  ensure_valid
  prop = self.class.properties[name]
  raise "No such property #{name}" if not prop
  property_cache[name] = prop.set(@model, @path, data)
end

#timestamp_property(name) ⇒ Object Also known as: ts_property

Convenience method for reading Time (aka timestamp) properties.



407
408
409
410
411
412
413
414
# File 'lib/git-ds/model/item.rb', line 407

def timestamp_property(name)
  val = property(name)
  if val && (not val.kind_of? Time)
    val = (not val.empty?) ? Time.parse(val) : nil
    property_cache[name] = val
  end
  val
end

#valid?Boolean

Return true if item is valid, false otherwise.

Returns:

  • (Boolean)


472
473
474
# File 'lib/git-ds/model/item.rb', line 472

def valid?
  @path   # an invalid item has a nil path
end