Class: ChangeAgent::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/change_agent/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, client_or_directory = nil) ⇒ Document

Returns a new instance of Document.



9
10
11
12
13
14
15
16
# File 'lib/change_agent/document.rb', line 9

def initialize(path, client_or_directory = nil)
  @path = path
  @client = if client_or_directory.instance_of?(ChangeAgent::Client)
              client_or_directory
            else
              ChangeAgent::Client.new(client_or_directory)
            end
end

Instance Attribute Details

#contentsObject



22
23
24
# File 'lib/change_agent/document.rb', line 22

def contents
  @contents ||= blob_contents
end

#pathObject Also known as: key

Returns the value of attribute path.



6
7
8
# File 'lib/change_agent/document.rb', line 6

def path
  @path
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/change_agent/document.rb', line 26

def changed?
  contents != blob_contents
end

#delete(file = path) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/change_agent/document.rb', line 42

def delete(file = path)
  repo.index.remove(file)

  Rugged::Commit.create repo,
                        message: "Removing #{path}",
                        parents: [repo.head.target],
                        tree: repo.index.write_tree(repo),
                        update_ref: 'HEAD'
rescue Rugged::IndexError
  false
end

#inspectObject



54
55
56
# File 'lib/change_agent/document.rb', line 54

def inspect
  "#<ChangeAgent::Document path=\"#{path}\">"
end

#repoObject



18
19
20
# File 'lib/change_agent/document.rb', line 18

def repo
  @client.repo
end

#saveObject Also known as: write



30
31
32
33
34
35
36
37
38
39
# File 'lib/change_agent/document.rb', line 30

def save
  oid = repo.write contents, :blob
  repo.index.add(path: path, oid: oid, mode: 0o100644)

  Rugged::Commit.create repo,
                        message: "Updating #{path}",
                        parents: repo.empty? ? [] : [repo.head.target],
                        tree: repo.index.write_tree(repo),
                        update_ref: 'HEAD'
end