Class: GHFS::File

Inherits:
Object
  • Object
show all
Defined in:
lib/github-fs/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, mode = "w", options = {}) ⇒ File

Returns a new instance of File.



13
14
15
16
17
18
19
20
# File 'lib/github-fs/file.rb', line 13

def initialize(path, mode="w", options={})
  @path       = path
  @mode       = mode || "w"
  @options    = options
  @repository = options.fetch(:repository) { GHFS.config.repository }
  @branch     = options.fetch(:branch) { GHFS.config.branch }
  @contents   = options[:contents] || options[:content]
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



11
12
13
# File 'lib/github-fs/file.rb', line 11

def branch
  @branch
end

#modeObject (readonly)

Returns the value of attribute mode.



11
12
13
# File 'lib/github-fs/file.rb', line 11

def mode
  @mode
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/github-fs/file.rb', line 11

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/github-fs/file.rb', line 11

def path
  @path
end

#repositoryObject (readonly)

Returns the value of attribute repository.



11
12
13
# File 'lib/github-fs/file.rb', line 11

def repository
  @repository
end

Class Method Details

.open(path, mode = nil, options = {}) {|file| ... } ⇒ Object

Yields:

  • (file)


4
5
6
7
8
# File 'lib/github-fs/file.rb', line 4

def open(path, mode=nil, options={}, &block)
  file = new(path, mode, options)
  yield(file) if block_given?
  file
end

Instance Method Details

#api_entityObject



67
68
69
70
71
72
73
# File 'lib/github-fs/file.rb', line 67

def api_entity
  @api_entity ||= GHFS.api.contents(repository, path: path, ref: ref)
rescue Octokit::NotFound
  @new_file = true
rescue
  nil
end

#append?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/github-fs/file.rb', line 114

def append?
  mode == "a" || mode == "a+"
end

#contentsObject



22
23
24
# File 'lib/github-fs/file.rb', line 22

def contents
  @contents || read
end

#deleteObject

Raises:



36
37
38
39
40
# File 'lib/github-fs/file.rb', line 36

def delete
  raise GHFS::NotFound if new_file?
  raise GHFS::ReadOnly if read_only?
  GHFS.api.delete_contents(repo, path, "Deleting #{ path }", branch: branch)
end

#encoded_contentsObject



92
93
94
95
# File 'lib/github-fs/file.rb', line 92

def encoded_contents
  # the octokit gem handles encoding for us
  contents.to_s
end

#exists?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/github-fs/file.rb', line 106

def exists?
  !new_file?
end

#latest_shaObject



102
103
104
# File 'lib/github-fs/file.rb', line 102

def latest_sha
  !new_file? && api_entity && api_entity.sha
end

#new_file?Boolean

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/github-fs/file.rb', line 97

def new_file?
  api_entity unless @api_entity
  !@new_file.nil?
end

#puts(contents) ⇒ Object



62
63
64
65
# File 'lib/github-fs/file.rb', line 62

def puts(contents)
  contents = "#{ contents }\n" unless contents.to_s.match(/\n$/m)
  write(contents)
end

#readObject



26
27
28
29
30
31
32
33
34
# File 'lib/github-fs/file.rb', line 26

def read
  @contents ||= begin
                  encoded = api_entity.content rescue nil

                  if encoded.to_s.length > 0
                    Base64.decode64(encoded)
                  end
                end
end

#read_only?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/github-fs/file.rb', line 110

def read_only?
  mode == "r"
end

#refObject



126
127
128
129
130
# File 'lib/github-fs/file.rb', line 126

def ref
  options.fetch(:ref) do
    branch || options[:commit] || options[:tag] || "master"
  end
end

#should_create?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/github-fs/file.rb', line 122

def should_create?
  mode == "w+" || mode == "a+"
end


42
43
44
# File 'lib/github-fs/file.rb', line 42

def unlink
  delete
end

#with_contents_persistedObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/github-fs/file.rb', line 75

def with_contents_persisted
  case
  when read_only?
    raise GHFS::ReadOnly
  when !should_create? && new_file?
    raise GHFS::NotFound
  when new_file? && should_create?
    @new_file = nil
    message = "Creating #{ path }"
    GHFS.api.create_contents(repository, path, message, encoded_contents, branch: branch)
  when !new_file?
    @new_file = nil
    message = "Updating #{ path }"
    GHFS.api.update_contents(repository, path, message, latest_sha, encoded_contents, branch: branch)
  end
end

#writable?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/github-fs/file.rb', line 118

def writable?
  append? || mode == "w" || mode == "w+"
end

#write(contents) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/github-fs/file.rb', line 46

def write(contents)
  read

  if append?
    @contents = @contents.to_s
    @contents += contents
  else
    @contents = contents
  end

  @contents
ensure
  with_contents_persisted
  @contents
end