module GitDS::ModelItemObject
# ———————————————————————- Instance methods used by repo-backed objects.
Note: this is an instance-method module. It should be included, not extended.
Attributes
The GitDS::Model
that contains the object.
Public Instance Methods
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
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
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 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
# File lib/git-ds/model/item.rb, line 462 def delete ensure_valid @model.delete_item(@path) # invalidate object @path = nil end
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
Primary key (ident) for instance.
# File lib/git-ds/model/item.rb, line 332 def ident ensure_valid @ident end
# 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
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
Full path to this item in the repo.
# File lib/git-ds/model/item.rb, line 324 def path ensure_valid @path end
Return list of property names.
# File lib/git-ds/model/item.rb, line 340 def properties self.class.properties.keys end
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
Return Hash of cached property values.
# File lib/git-ds/model/item.rb, line 356 def property_cache ensure_valid @property_cache ||= {} end
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
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
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
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