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[RW]

Default value, e.g. ‘0’.

default_value[RW]

Default value, e.g. ‘0’.

name[RW]

Name of the property, e.g. ‘size’.

on_fs[RW]

Property exists on-disk as well as in the object db.

validation_block[RW]

Block used to validate data when set.

Public Class Methods

new(name, default=nil, fs=false, &block) click to toggle source
# 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(value) click to toggle source

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
get(model, parent_path) click to toggle source

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
path(parent_path) click to toggle source

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
set(model, parent_path, value) click to toggle source

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
valid?(value) click to toggle source

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(model, path, value) click to toggle source

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