class GHFS::File

Attributes

branch[R]
mode[R]
options[R]
path[R]
repository[R]

Public Class Methods

new(path, mode="w", options={}) click to toggle source
# 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
open(path, mode=nil, options={}) { |file| ... } click to toggle source
# 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

Public Instance Methods

api_entity() click to toggle source
# 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?() click to toggle source
# File lib/github-fs/file.rb, line 114
def append?
  mode == "a" || mode == "a+"
end
contents() click to toggle source
# File lib/github-fs/file.rb, line 22
def contents
  @contents || read
end
delete() click to toggle source
# 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_contents() click to toggle source
# File lib/github-fs/file.rb, line 92
def encoded_contents
  # the octokit gem handles encoding for us
  contents.to_s
end
exists?() click to toggle source
# File lib/github-fs/file.rb, line 106
def exists?
  !new_file?
end
latest_sha() click to toggle source
# File lib/github-fs/file.rb, line 102
def latest_sha
  !new_file? && api_entity && api_entity.sha
end
new_file?() click to toggle source
# File lib/github-fs/file.rb, line 97
def new_file?
  api_entity unless @api_entity
  !@new_file.nil?
end
puts(contents) click to toggle source
# File lib/github-fs/file.rb, line 62
def puts(contents)
  contents = "#{ contents }\n" unless contents.to_s.match(/\n$/m)
  write(contents)
end
read() click to toggle source
# 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?() click to toggle source
# File lib/github-fs/file.rb, line 110
def read_only?
  mode == "r"
end
ref() click to toggle source
# File lib/github-fs/file.rb, line 126
def ref
  options.fetch(:ref) do
    branch || options[:commit] || options[:tag] || "master"
  end
end
should_create?() click to toggle source
# File lib/github-fs/file.rb, line 122
def should_create?
  mode == "w+" || mode == "a+"
end
with_contents_persisted() click to toggle source
# 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?() click to toggle source
# File lib/github-fs/file.rb, line 118
def writable?
  append? || mode == "w" || mode == "w+"
end
write(contents) click to toggle source
# 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