Class: GitDS::PropertyDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/git-ds/model/property.rb

Overview

A definition of a ModelItem property.

These are stored in the class, and are used to wrap access to properties by ModelItem objects.

Direct Known Subclasses

BinaryPropertyDefinition, ProxyProperty

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, default = nil, fs = false, &block) ⇒ PropertyDefinition

Returns a new instance of PropertyDefinition.



59
60
61
62
63
64
# 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

Instance Attribute Details

#default_valueObject Also known as: default

Default value, e.g. ‘0’.



47
48
49
# File 'lib/git-ds/model/property.rb', line 47

def default_value
  @default_value
end

#nameObject

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



42
43
44
# File 'lib/git-ds/model/property.rb', line 42

def name
  @name
end

#on_fsObject

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



52
53
54
# File 'lib/git-ds/model/property.rb', line 52

def on_fs
  @on_fs
end

#validation_blockObject

Block used to validate data when set.



57
58
59
# File 'lib/git-ds/model/property.rb', line 57

def validation_block
  @validation_block
end

Instance Method Details

#convert_value(value) ⇒ Object

Convert value to its internal (in-Git) representation.



101
102
103
104
105
106
107
108
109
110
111
# 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) ⇒ Object

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.



79
80
81
82
# 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) ⇒ Object

Get full path to BLOB for property based on parent directory.



69
70
71
# 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) ⇒ Object

Write value to ModelItem at path in Model.

Note: this returns the String representation of the value as written to the Property BLOB.



90
91
92
93
94
95
96
# 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) ⇒ Boolean

If property has a validation block, invoke it to determine if value is valid. Otherwise, return true.

Returns:

  • (Boolean)


117
118
119
120
# File 'lib/git-ds/model/property.rb', line 117

def valid?(value)
  blk = self.validation_block
  blk ? blk.call(value) : true
end