module GitDS::ModelItemClass

# ———————————————————————- ModelItem class methods.

Note: this is a class-method module. It should be extended in a class, not included.

Public Instance Methods

binary_property(name, on_fs=true) click to toggle source

Define a BinaryProperty for this ModelItem class. The property will be on-FS unless on_fs is set to false.

Note: Properties that store raw binary data have no default value and no validation function. They are assumed to be on-disk by default.

# File lib/git-ds/model/item.rb, line 263
def binary_property(name, on_fs=true)
  add_property BinaryPropertyDefinition.new(name, nil, on_fs)
end
build_path(parent_path) click to toggle source

Return the path to the ModelItem class inside the specified directory.

# File lib/git-ds/model/item.rb, line 68
def build_path(parent_path)
  return name if (not parent_path) || parent_path.empty?
  parent_path + ::File::SEPARATOR + name
end
create(parent, args={}) click to toggle source

Create a new instance of the ModelItemClass owned by the specified parent.

This will create a subdirectory under ModelItemClass.path(parent); the name of the directory is determined by ModelItemClass.ident.

For example, given the following database:

ClassA/a1/ClassB/b1
ClassA/a1/ClassB/b2
ClassA/a2/ClassB/b3

ModelItemClass.create(a2, { :ident => ‘b4’ } ) will create the directory ‘ClassA/a2/ClassB/b4’.

The directory will then be filled by calling ModelItemClass.fill.

Note that this returns the path to the created item, not an instance.

# File lib/git-ds/model/item.rb, line 152
def create(parent, args={})
  raise "Use Database.root instead of nil for parent" if not parent
  raise "parent is not a ModelItem" if not parent.respond_to? :model

  model = parent.model
  create_in_path(parent.model, parent.path, args)
end
create_in_path(model, parent_path, args) click to toggle source

Create a new instance of the ModelItemClass in the specified directory.

This will create a subdirectory under parent_path; the name of the directory is determined by ModelItemClass.ident.

For example, given the following database:

ClassA/a1/ClassB/b1
ClassA/a1/ClassB/b2
ClassA/a2/ClassB/b3

ModelItemClass.create(a2, { :ident => ‘b4’ } ) will create the directory ‘ClassA/a2/ClassB/b4’.

The directory will then be filled by calling ModelItemClass.fill.

The creation of the objects in the model takes place within a DB transaction. If this is called from within a transaction, it will use the existing staging index; otherwise, it will create a new index and auto-commit on success.

Note that this returns the ident of the created item, not an instance.

# File lib/git-ds/model/item.rb, line 183
def create_in_path(model, parent_path, args)
  id = ident(args)
  item_path = build_path(parent_path) + ::File::SEPARATOR + id
  raise InvalidModelItemPath if (not item_path) || item_path =~ /\000/

  # Ensure that nested calls (e.g. to create children) share index#write
  cls = self
  model.transaction {
    propagate
    cls.fill(model, item_path, args)
  }

  item_path
end
define_db_property(name, default=0, &block) click to toggle source

Define a property for this ModelItem class.

# File lib/git-ds/model/item.rb, line 236
def define_db_property(name, default=0, &block)
  add_property PropertyDefinition.new(name, default, false, &block)
end
define_fs_property(name, default=0, &block) click to toggle source

Define an on-filesystem property for this ModelItem class.

# File lib/git-ds/model/item.rb, line 243
def define_fs_property(name, default=0, &block)
  add_property PropertyDefinition.new(name, default, true, &block)
end
exist?(parent, ident) click to toggle source

Return true if the specified instance of this ModelItem class exists in the model (i.e. database has ‘ident’ in it already).

# File lib/git-ds/model/item.rb, line 86
def exist?(parent, ident)
  parent.model.include? instance_path(parent.path, ident)
end
fill(model, item_path, args) click to toggle source

Create all subdirectories and files needed to represent a ModelItemClass instance in the object repository.

item_path is the full path to the item in the model. args is a hash of arguments used to construct the item.

Can be overridden by ModelItem classes and invoked via super.

# File lib/git-ds/model/item.rb, line 207
def fill(model, item_path, args)
  fill_properties(model, item_path, args)
end
fill_properties(model, item_path, args) click to toggle source

Fill all properties either with their value in ‘args’ or their default value.

Foreach key in properties,

if args.include?(key) && property.valid?(key, args[key])
   set property to key
elsif properties[key].default
   set property to default
else ignore
# File lib/git-ds/model/item.rb, line 221
def fill_properties(model, item_path, args)
  hash = properties
  hash.keys.each do |key|
    prop = hash[key]
    if args.include?(key)
      prop.set(model, item_path, args[key])
    elsif hash[key].default
      prop.set(model, item_path, prop.default)
    end
  end
end
ident(args) click to toggle source

Generate an ident (String) from a Hash of arguments.

To be overridden by a modelitem class.

# File lib/git-ds/model/item.rb, line 119
def ident(args)
  args[ident_key()].to_s
end
ident_key() click to toggle source

The key containing the object ident in the args Hash passed to create.

This can be used to change the name of the ident key in the hash without having to override the ident method.

# File lib/git-ds/model/item.rb, line 129
def ident_key
  :ident
end
instance_path(parent_path, ident) click to toggle source

Return the path to an instance of the object under parent_path.

# File lib/git-ds/model/item.rb, line 76
def instance_path(parent_path, ident)
  path = build_path(parent_path)
  path += ::File::SEPARATOR if not path.empty?
  path += ident.to_s
end
list(parent) click to toggle source

List all children of this ModelItem class.

This will list all instances of the class owned by the specified parent. For example, given the following database:

ClassA/a1/ClassB/b1
ClassA/a1/ClassB/b2
ClassA/a2/ClassB/b3

ClassA.list(@model.root) will return [a1, a2], ClassB.list(a1) will return [b1, b2], and ClassB.list(a2) will return [b3].

# File lib/git-ds/model/item.rb, line 103
def list(parent)
  list_in_path parent.model, parent.path
end
list_in_path(model, parent_path) click to toggle source

List all children of the ModelItem class inside parent_path.

# File lib/git-ds/model/item.rb, line 110
def list_in_path(model, parent_path)
  model.list_children build_path(parent_path)
end
name(name=nil) click to toggle source

The name of the ModelItemClass in the database.

To be overridden by a modelitem class.

# File lib/git-ds/model/item.rb, line 42
def name(name=nil)
  @name ||= nil
  raise 'ModelItemClass has no name defined' if (not name) && (not @name)
  name ? @name = name : @name
end
path(parent) click to toggle source

Return the path to the ModelItem class owned by the specified parent.

For example, given the following database:

ClassA/a1/ClassB/b1
ClassA/a1/ClassB/b2
ClassA/a2/ClassB/b3

ClassA.path(@model.root) will return ‘ClassA’, ClassB.path(a1) will return ‘ClassA/a1/ClassB’, and ClassB.path(a2) will return ‘ClassA/a2/ClassB’.

# File lib/git-ds/model/item.rb, line 60
def path(parent)
  return name if not parent
  build_path(parent.path)
end
properties() click to toggle source

Hash of properties associated with this MOdelItem class.

# File lib/git-ds/model/item.rb, line 282
def properties
  @properties ||= {}
end
property(name, default=0, &block) click to toggle source

Define a Property for this ModelItem class. The property will be DB-only.

This can be overridden to change how properties are stored.

# File lib/git-ds/model/item.rb, line 252
def property(name, default=0, &block)
  define_db_property(name, default, &block)
end
proxy_property(name, cls, &block)
Alias for: link_property

Private Instance Methods

add_property(p) click to toggle source

Add a property to the properties Hash. This throws GitDS::DuplicatePropertyError if a property with the same name already exists in the class.

# File lib/git-ds/model/item.rb, line 292
def add_property(p)
  hash = properties
  raise DuplicatePropertyError.new(p.name) if hash.include? p.name
  hash[p.name] = p
end