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.



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

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

Instance Attribute Details

#contentsObject



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

def contents
  @contents ||= blob_contents
end

#pathObject Also known as: key

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


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

def changed?
  contents != blob_contents
end

#delete(file = path) ⇒ Object



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

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



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

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

#repoObject



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

def repo
  @client.repo
end

#saveObject Also known as: write



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

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

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