Class: GitDS::ExecCmd

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

Overview

A command to be executed in a database context (i.e. an Index).

Usage:

db.exec { |idx| … }

See also Transaction.

Direct Known Subclasses

Transaction

Constant Summary collapse

DEFAULT_MESSAGE =
'auto-commit on transaction'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, nested, msg = DEFAULT_MESSAGE, &block) ⇒ ExecCmd

Returns a new instance of ExecCmd.



51
52
53
54
55
56
57
58
59
60
# File 'lib/git-ds/exec_cmd.rb', line 51

def initialize(index, nested, msg=DEFAULT_MESSAGE, &block)
  @index = index
  @database = index.repo
  @nested = nested
  @block = block
  # Default to no commit
  @commit_msg = msg
  # Default to config[user.name] and config[user.email]
  @commit_author = nil
end

Instance Attribute Details

#blockObject (readonly)

The body of the command.



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

def block
  @block
end

#commit_authorObject (readonly)

The Git author for the commit performed at the end of the command. See commit_msg.



40
41
42
# File 'lib/git-ds/exec_cmd.rb', line 40

def commit_author
  @commit_author
end

#commit_msgObject (readonly)

The message to use for the commit at the end of the command.



34
35
36
# File 'lib/git-ds/exec_cmd.rb', line 34

def commit_msg
  @commit_msg
end

#databaseObject (readonly)

The GitDS::Database on which the command operates. Useful for nesting.



30
31
32
# File 'lib/git-ds/exec_cmd.rb', line 30

def database
  @database
end

#indexObject (readonly)

The GitDS::Index on which the command operates.



26
27
28
# File 'lib/git-ds/exec_cmd.rb', line 26

def index
  @index
end

#nestedObject (readonly)

Is command nested (inside a parent)? If true, a write and commit will not be performed.



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

def nested
  @nested
end

Instance Method Details

#actor=(actor) ⇒ Object

Set actor for commit.



80
81
82
# File 'lib/git-ds/exec_cmd.rb', line 80

def actor=(actor)
  @commit_author = actor
end

#author(name, email) ⇒ Object

Set the Git Author info for the commit. By default, this information is pulled from the Git config file.



73
74
75
# File 'lib/git-ds/exec_cmd.rb', line 73

def author(name, email)
  @commit_author = Grit::Actor.new(name, email)
end

#commitObject

Commit index.



87
88
89
# File 'lib/git-ds/exec_cmd.rb', line 87

def commit
  self.index.commit(@commit_msg, @commit_author)
end

#message(str) ⇒ Object

Set a commit message for this command.



65
66
67
# File 'lib/git-ds/exec_cmd.rb', line 65

def message(str)
  @commit_msg = str
end

#performObject

Perform command.



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/git-ds/exec_cmd.rb', line 94

def perform
  rv = instance_eval(&self.block)

  self.index.build
  if not self.nested
    commit
    @database.notify
  end

  rv
end