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
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
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 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 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 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 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
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
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 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
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
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
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
Define a property that is a link to a ModelItem
object.
Note: this is a link to a single ModelItem
class. For a list of links to ModelItems, use a ModelItem
List of ProxyModelItemClass objects.
# File lib/git-ds/model/item.rb, line 273 def link_property(name, cls, &block) add_property ProxyProperty.new(name, cls, false, &block) end
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 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
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
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
Hash of properties associated with this MOdelItem class.
# File lib/git-ds/model/item.rb, line 282 def properties @properties ||= {} end
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
Private Instance Methods
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