class GitDS::PropertyDefinition
A definition of a ModelItem
property.
These are stored in the class, and are used to wrap access to properties by ModelItem
objects.
Attributes
Default value, e.g. ‘0’.
Default value, e.g. ‘0’.
Name of the property, e.g. ‘size’.
Property exists on-disk as well as in the object db.
Block used to validate data when set.
Public Class Methods
# File lib/git-ds/model/property.rb, line 59 def initialize(name, default=nil, fs=false, &block) @name = name @default_value = default @on_fs = fs @validation_block = (block_given?) ? block : nil end
Public Instance Methods
Convert value to its internal (in-Git) representation.
# File lib/git-ds/model/property.rb, line 101 def convert_value(value) val = value.to_s if value.kind_of?(Array) val = value.join("\n") elsif value.kind_of?(Hash) val = value.inspect end val end
Read value from ModelItem
at path in Model
. This just returns the String value of the property file contents; subclasses should wrap this with a call that will generate an Integer, List, etc from the contents.
# File lib/git-ds/model/property.rb, line 79 def get(model, parent_path) val = model.get_item(path(parent_path)) val ? val.chomp : '' end
Get full path to BLOB for property based on parent directory.
# File lib/git-ds/model/property.rb, line 69 def path(parent_path) parent_path + ::File::SEPARATOR + name.to_s end
Write value to ModelItem
at path in Model
.
Note: this returns the String representation of the value as written to the Property BLOB.
# File lib/git-ds/model/property.rb, line 90 def set(model, parent_path, value) raise InvalidPropertyValueError.new(name, value.inspect) if not \ valid?(value) val = convert_value(value) write(model, path(parent_path), val + "\n") val end
If property has a validation block, invoke it to determine if value is valid. Otherwise, return true.
# File lib/git-ds/model/property.rb, line 117 def valid?(value) blk = self.validation_block blk ? blk.call(value) : true end
Protected Instance Methods
Write
# File lib/git-ds/model/property.rb, line 129 def write(model, path, value) on_fs ? model.add_fs_item(path, value) : model.add_item(path, value) end