Class: LedgerRest::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/ledger-rest/git.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.branchObject (readonly)

Returns the value of attribute branch.



8
9
10
# File 'lib/ledger-rest/git.rb', line 8

def branch
  @branch
end

.read_pull_block_timeObject (readonly)

Returns the value of attribute read_pull_block_time.



8
9
10
# File 'lib/ledger-rest/git.rb', line 8

def read_pull_block_time
  @read_pull_block_time
end

.remoteObject (readonly)

Returns the value of attribute remote.



8
9
10
# File 'lib/ledger-rest/git.rb', line 8

def remote
  @remote
end

.repositoryObject (readonly)

Returns the value of attribute repository.



8
9
10
# File 'lib/ledger-rest/git.rb', line 8

def repository
  @repository
end

Class Method Details

.blocked_read_pull?Boolean



38
39
40
# File 'lib/ledger-rest/git.rb', line 38

def blocked_read_pull?
  (Time.new - @last_read_pull) > read_pull_block_time
end

.configure(options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ledger-rest/git.rb', line 10

def configure(options)
  @repository = options[:git_repository] || File.dirname(options[:ledger_file] || 'main.ledger')
  @pull_before_read = options[:git_pull_before_read] || false
  @pull_before_write = options[:git_pull_before_write] || false
  @push_after_write = options[:git_push_after_write] || false
  @remote = options[:git_remote] || 'origin'
  @branch = options[:git_branch] || 'master'
  @read_pull_block_time = options[:git_read_pull_block_time] || 10*60

  @last_read_pull = Time.new
  if pull_before_read? || pull_before_write? || push_after_write?
    @git_repo = ::Git.open(repository)
    FileUtils.touch(options[:ledger_append_file])
    @git_repo.add(options[:ledger_append_file])
  end
end

.invoke(hook) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/ledger-rest/git.rb', line 27

def invoke(hook)
  case hook
  when :before_read
    pull if pull_before_read? && !blocked?
  when :before_write
    pull if pull_before_write?
  when :after_write
    push if push_after_write?
  end
end

.pullObject

Execute the pull command.



55
56
57
58
59
60
# File 'lib/ledger-rest/git.rb', line 55

def pull
  @git_repo.pull(remote, branch)
  @last_read_pull = Time.new
rescue Exception => e
  $stderr.puts "Git pull failed: #{e}"
end

.pull_before_read?Boolean



42
43
44
# File 'lib/ledger-rest/git.rb', line 42

def pull_before_read?
  @pull_before_read
end

.pull_before_write?Boolean



46
47
48
# File 'lib/ledger-rest/git.rb', line 46

def pull_before_write?
  @pull_before_write
end

.pushObject

Execute the push command after commiting all.



63
64
65
66
67
68
# File 'lib/ledger-rest/git.rb', line 63

def push
  @git_repo.commit_all('Add transaction via ledger-rest')
  @git_repo.push(remote, branch)
rescue Exception => e
  $stderr.put "Git push failed: #{e}"
end

.push_after_write?Boolean



50
51
52
# File 'lib/ledger-rest/git.rb', line 50

def push_after_write?
  @push_after_write
end