Module: GitDS::ModelObject

Included in:
Model
Defined in:
lib/git-ds/model.rb

Overview

Instance methods used by a Model object.

Note: This is an instance-method module. It should be included, not extended.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dbObject (readonly)

The database connection for the model. This is expected to be a GitDS::Database object.



44
45
46
# File 'lib/git-ds/model.rb', line 44

def db
  @db
end

#nameObject (readonly)

The name of the model. This is only used for storing configuration variables.



49
50
51
# File 'lib/git-ds/model.rb', line 49

def name
  @name
end

#rootObject (readonly)

The Root item for the Model.



38
39
40
# File 'lib/git-ds/model.rb', line 38

def root
  @root
end

Instance Method Details

#add_fs_item(path, data) ⇒ Object

Add an item to the object DB and the filesystem.



92
93
94
95
# File 'lib/git-ds/model.rb', line 92

def add_fs_item(path, data)
  # note: @db.add uses exec {} so there is no need to here.
  @db.add(path, data, true)
end

#add_item(path, data) ⇒ Object

Might be better as add child?



84
85
86
87
# File 'lib/git-ds/model.rb', line 84

def add_item(path, data)
  # note: @db.add uses exec {} so there is no need to here.
  @db.add(path, data)
end

#batch(&block) ⇒ Object

Execute a block using an in-memory Staging index.

This isjust a wrapper for Database#batch.



141
142
143
# File 'lib/git-ds/model.rb', line 141

def batch(&block)
  @db.batch(&block)
end

#branched_transaction(name = @db.next_branch_tag(), &block) ⇒ Object

Execute a transaction in a branch, then merge if it was successful.

See Database#branch_and_merge.



131
132
133
134
# File 'lib/git-ds/model.rb', line 131

def branched_transaction(name=@db.next_branch_tag(), &block)
  raise 'Branched transactions cannot be nested' if @db.staging?
  @db.branch_and_merge(name, &block)
end

#configObject

Provides access to the Hash of Model-specific config variables.



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

def config
  @git_config ||= RepoConfig.new(@db, 'model-' + @name)
end

#delete_item(path) ⇒ Object

Delete an item from the object DB (and the filesystem, if it exists).



107
108
109
110
# File 'lib/git-ds/model.rb', line 107

def delete_item(path)
  # note: @db.delete uses exec {} so there is no need to here.
  @db.delete(path)
end

#exec(&block) ⇒ Object

Execute block as a database ExecCmd.



115
116
117
# File 'lib/git-ds/model.rb', line 115

def exec(&block)
  @db.exec(&block)
end

#get_item(path) ⇒ Object

Return the contents of the BLOB at path.



100
101
102
# File 'lib/git-ds/model.rb', line 100

def get_item(path)
  @db.object_data(path)
end

#include?(path) ⇒ Boolean Also known as: exist?

Returns true if Model contains path.

Returns:

  • (Boolean)


67
68
69
# File 'lib/git-ds/model.rb', line 67

def include?(path)
  @db.include? path
end

#initialize_model(db, name = 'generic', root = nil) ⇒ Object



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

def initialize_model(db, name='generic', root=nil)
  @db = db
  @root = root ? root : RootItem.new(self)
  @name = name
end

#list_children(path = root.path) ⇒ Object

List children (filenames) of path. Returns [] if path is not a directory.



76
77
78
# File 'lib/git-ds/model.rb', line 76

def list_children(path=root.path)
  @db.list(path).keys.sort
end

#transaction(&block) ⇒ Object

Execute block as a database transaction.



122
123
124
# File 'lib/git-ds/model.rb', line 122

def transaction(&block)
  @db.transaction(&block)
end