Class: GHFS::File
- Inherits:
-
Object
- Object
- GHFS::File
- Defined in:
- lib/github-fs/file.rb
Instance Attribute Summary collapse
-
#branch ⇒ Object
readonly
Returns the value of attribute branch.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#repository ⇒ Object
readonly
Returns the value of attribute repository.
Class Method Summary collapse
Instance Method Summary collapse
- #api_entity ⇒ Object
- #append? ⇒ Boolean
- #contents ⇒ Object
- #delete ⇒ Object
- #encoded_contents ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(path, mode = "w", options = {}) ⇒ File
constructor
A new instance of File.
- #latest_sha ⇒ Object
- #new_file? ⇒ Boolean
- #puts(contents) ⇒ Object
- #read ⇒ Object
- #read_only? ⇒ Boolean
- #ref ⇒ Object
- #should_create? ⇒ Boolean
- #unlink ⇒ Object
- #with_contents_persisted ⇒ Object
- #writable? ⇒ Boolean
- #write(contents) ⇒ Object
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", ={}) @path = path @mode = mode || "w" @options = @repository = .fetch(:repository) { GHFS.config.repository } @branch = .fetch(:branch) { GHFS.config.branch } @contents = [:contents] || [:content] end |
Instance Attribute Details
#branch ⇒ Object (readonly)
Returns the value of attribute branch.
11 12 13 |
# File 'lib/github-fs/file.rb', line 11 def branch @branch end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
11 12 13 |
# File 'lib/github-fs/file.rb', line 11 def mode @mode end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/github-fs/file.rb', line 11 def @options end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/github-fs/file.rb', line 11 def path @path end |
#repository ⇒ Object (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
4 5 6 7 8 |
# File 'lib/github-fs/file.rb', line 4 def open(path, mode=nil, ={}, &block) file = new(path, mode, ) yield(file) if block_given? file end |
Instance Method Details
#api_entity ⇒ Object
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
114 115 116 |
# File 'lib/github-fs/file.rb', line 114 def append? mode == "a" || mode == "a+" end |
#contents ⇒ Object
22 23 24 |
# File 'lib/github-fs/file.rb', line 22 def contents @contents || read end |
#delete ⇒ Object
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_contents ⇒ Object
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
106 107 108 |
# File 'lib/github-fs/file.rb', line 106 def exists? !new_file? end |
#latest_sha ⇒ Object
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
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 |
#read ⇒ Object
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
110 111 112 |
# File 'lib/github-fs/file.rb', line 110 def read_only? mode == "r" end |
#ref ⇒ Object
126 127 128 129 130 |
# File 'lib/github-fs/file.rb', line 126 def ref .fetch(:ref) do branch || [:commit] || [:tag] || "master" end end |
#should_create? ⇒ Boolean
122 123 124 |
# File 'lib/github-fs/file.rb', line 122 def should_create? mode == "w+" || mode == "a+" end |
#unlink ⇒ Object
42 43 44 |
# File 'lib/github-fs/file.rb', line 42 def unlink delete end |
#with_contents_persisted ⇒ Object
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 = "Creating #{ path }" GHFS.api.create_contents(repository, path, , encoded_contents, branch: branch) when !new_file? @new_file = nil = "Updating #{ path }" GHFS.api.update_contents(repository, path, , latest_sha, encoded_contents, branch: branch) end end |
#writable? ⇒ 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 |