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
-
#model ⇒ Object
readonly
The GitDS::Model that contains the object.
Instance Method Summary collapse
-
#array_property(name, delim = "\n") ⇒ Object
(also: #a_property)
Convenience method for reading Array properties.
-
#bool_property(name) ⇒ Object
(also: #b_property)
Convenience method for reading Boolean properties.
-
#clear_cache ⇒ Object
Clear all caches in ModelItem instance.
- #delete ⇒ Object
-
#float_property(name) ⇒ Object
(also: #f_property)
Convenience method for reading Float properties.
-
#ident ⇒ Object
Primary key (ident) for instance.
- #initialize_item(model, path) ⇒ Object
-
#integer_property(name) ⇒ Object
(also: #i_property)
Convenience method for reading Integer properties.
-
#path ⇒ Object
Full path to this item in the repo.
-
#properties ⇒ Object
Return list of property names.
-
#property(name) ⇒ Object
Return the value of a specific property.
-
#property_cache ⇒ Object
Return Hash of cached property values.
-
#set_property(name, data) ⇒ Object
Set the value of a specific property.
-
#timestamp_property(name) ⇒ Object
(also: #ts_property)
Convenience method for reading Time (aka timestamp) properties.
-
#valid? ⇒ Boolean
Return true if item is valid, false otherwise.
Instance Attribute Details
#model ⇒ Object (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.
427 428 429 430 431 432 433 434 |
# File 'lib/git-ds/model/item.rb', line 427 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.
412 413 414 415 |
# File 'lib/git-ds/model/item.rb', line 412 def bool_property(name) val = property(name) (val && val == 'true') end |
#clear_cache ⇒ Object
Clear all caches in ModelItem instance. In the base class, this just clears the property cache.
356 357 358 |
# File 'lib/git-ds/model/item.rb', line 356 def clear_cache property_cache.clear end |
#delete ⇒ Object
453 454 455 456 457 458 |
# File 'lib/git-ds/model/item.rb', line 453 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.
388 389 390 391 |
# File 'lib/git-ds/model/item.rb', line 388 def float_property(name) val = property(name) val ? property_cache[name] = val.to_f : nil end |
#ident ⇒ Object
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.
378 379 380 381 |
# File 'lib/git-ds/model/item.rb', line 378 def integer_property(name) val = property(name) val ? property_cache[name] = val.to_i : nil end |
#path ⇒ Object
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 |
#properties ⇒ Object
Return list of property names.
340 341 342 |
# File 'lib/git-ds/model/item.rb', line 340 def properties self.class.properties.keys.sort 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.
367 368 369 370 371 372 373 |
# File 'lib/git-ds/model/item.rb', line 367 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 ⇒ Object
Return Hash of cached property values.
347 348 349 350 |
# File 'lib/git-ds/model/item.rb', line 347 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.
444 445 446 447 448 449 |
# File 'lib/git-ds/model/item.rb', line 444 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.
398 399 400 401 402 403 404 405 |
# File 'lib/git-ds/model/item.rb', line 398 def (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.
463 464 465 |
# File 'lib/git-ds/model/item.rb', line 463 def valid? @path # an invalid item has a nil path end |