module GitDS::ModelItemObject

# ———————————————————————- Instance methods used by repo-backed objects.

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

Attributes

model[R]

The GitDS::Model that contains the object.

Public Instance Methods

a_property(name, delim="\n")
Alias for: array_property
array_property(name, delim="\n") click to toggle source

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.

# 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
Also aliased as: a_property
b_property(name)
Alias for: bool_property
bool_property(name) click to toggle source

Convenience method for reading Boolean properties.

# File lib/git-ds/model/item.rb, line 421
def bool_property(name)
  val = property(name)
  (val && val == 'true')
end
Also aliased as: b_property
children() click to toggle source

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

# File lib/git-ds/model/item.rb, line 349
def children
  properties                                                              
end
clear_cache() click to toggle source

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

# File lib/git-ds/model/item.rb, line 365
def clear_cache
  property_cache.clear
end
delete() click to toggle source
# File lib/git-ds/model/item.rb, line 462
def delete
  ensure_valid
  @model.delete_item(@path)
  # invalidate object
  @path = nil
end
f_property(name)
Alias for: float_property
float_property(name) click to toggle source

Convenience method for reading Float properties.

# 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
Also aliased as: f_property
i_property(name)
Alias for: integer_property
ident() click to toggle source

Primary key (ident) for instance.

# File lib/git-ds/model/item.rb, line 332
def ident
  ensure_valid
  @ident
end
initialize_item(model, path) click to toggle source
# 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) click to toggle source

Convenience method for reading Integer properties.

# 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
Also aliased as: i_property
path() click to toggle source

Full path to this item in the repo.

# File lib/git-ds/model/item.rb, line 324
def path
  ensure_valid
  @path
end
properties() click to toggle source

Return list of property names.

# File lib/git-ds/model/item.rb, line 340
def properties
  self.class.properties.keys
end
property(name) click to toggle source

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.

# 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_cache() click to toggle source

Return Hash of cached property values.

# File lib/git-ds/model/item.rb, line 356
def property_cache
  ensure_valid
  @property_cache ||= {}
end
set_property(name, data) click to toggle source

Set the value of a specific property.

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

# 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) click to toggle source

Convenience method for reading Time (aka timestamp) properties.

# 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
Also aliased as: ts_property
ts_property(name)
Alias for: timestamp_property
valid?() click to toggle source

Return true if item is valid, false otherwise.

# File lib/git-ds/model/item.rb, line 472
def valid?
  @path   # an invalid item has a nil path
end

Protected Instance Methods

ensure_valid() click to toggle source

Raises an InvalidModelItemError if item is not valid.

Note: accessors for non-property children should invoke this before touching the object.

# File lib/git-ds/model/item.rb, line 484
def ensure_valid
  raise InvalidModelItemError if not valid?
end