Module: GitDS::ModelItemClass

Included in:
DbModelItemClass, FsModelItemClass
Defined in:
lib/git-ds/model/item.rb

Overview

ModelItem class methods.

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

Instance Method Summary collapse

Instance Method Details

#binary_property(name, on_fs = true) ⇒ Object

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.



263
264
265
# 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) ⇒ Object

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



68
69
70
71
# 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 = {}) ⇒ Object

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.



152
153
154
155
156
157
158
# 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) ⇒ Object

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.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# 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) ⇒ Object

Define a property for this ModelItem class.



236
237
238
# 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) ⇒ Object

Define an on-filesystem property for this ModelItem class.



243
244
245
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


86
87
88
# 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) ⇒ Object

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.



207
208
209
# 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) ⇒ Object

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


221
222
223
224
225
226
227
228
229
230
231
# 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) ⇒ Object

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

To be overridden by a modelitem class.



119
120
121
# File 'lib/git-ds/model/item.rb', line 119

def ident(args)
  args[ident_key()].to_s
end

#ident_keyObject

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.



129
130
131
# File 'lib/git-ds/model/item.rb', line 129

def ident_key
  :ident
end

#instance_path(parent_path, ident) ⇒ Object

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



76
77
78
79
80
# 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.



273
274
275
# 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(parent) ⇒ Object

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].



103
104
105
# 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) ⇒ Object

List all children of the ModelItem class inside parent_path.



110
111
112
# 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) ⇒ Object

The name of the ModelItemClass in the database.

To be overridden by a modelitem class.



42
43
44
45
46
# 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) ⇒ Object

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’.



60
61
62
63
# File 'lib/git-ds/model/item.rb', line 60

def path(parent)
  return name if not parent
  build_path(parent.path)
end

#propertiesObject

Hash of properties associated with this MOdelItem class.



282
283
284
# File 'lib/git-ds/model/item.rb', line 282

def properties
  @properties ||= {}
end

#property(name, default = 0, &block) ⇒ Object

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

This can be overridden to change how properties are stored.



252
253
254
# File 'lib/git-ds/model/item.rb', line 252

def property(name, default=0, &block)
  define_db_property(name, default, &block)
end