Class: Inprovise::LocalFile
- Inherits:
-
Object
- Object
- Inprovise::LocalFile
- Defined in:
- lib/inprovise/local_file.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #content ⇒ Object
- #copy_from(source) ⇒ Object
- #copy_to(destination) ⇒ Object
- #delete! ⇒ Object
- #directory? ⇒ Boolean
- #download(source) ⇒ Object
- #duplicate(destination) ⇒ Object
- #exists? ⇒ Boolean
- #file? ⇒ Boolean
- #group ⇒ Object
- #hash ⇒ Object
-
#initialize(path) ⇒ LocalFile
constructor
A new instance of LocalFile.
- #is_local? ⇒ Boolean
-
#matches?(other) ⇒ Boolean
deosnt check permissions or user.
- #permissions ⇒ Object
- #resolve(path) ⇒ Object
- #set_owner(user, group = nil) ⇒ Object
- #set_permissions(mask) ⇒ Object
- #upload(destination) ⇒ Object
- #user ⇒ Object
Constructor Details
#initialize(path) ⇒ LocalFile
Returns a new instance of LocalFile.
13 14 15 |
# File 'lib/inprovise/local_file.rb', line 13 def initialize(path) @path = resolve(path) end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/inprovise/local_file.rb', line 11 def path @path end |
Instance Method Details
#content ⇒ Object
42 43 44 45 |
# File 'lib/inprovise/local_file.rb', line 42 def content return File.read(@path) if exists? nil end |
#copy_from(source) ⇒ Object
61 62 63 |
# File 'lib/inprovise/local_file.rb', line 61 def copy_from(source) source.copy_to(self) end |
#copy_to(destination) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/inprovise/local_file.rb', line 52 def copy_to(destination) if destination.is_local? duplicate(destination) else upload(destination) end destination end |
#delete! ⇒ Object
90 91 92 93 |
# File 'lib/inprovise/local_file.rb', line 90 def delete! FileUtils.rm(path) if exists? self end |
#directory? ⇒ Boolean
34 35 36 |
# File 'lib/inprovise/local_file.rb', line 34 def directory? File.directory?(path) end |
#download(source) ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/inprovise/local_file.rb', line 80 def download(source) source = @context.remote(source) if String === source if source.is_local? FileUtils.cp(source.path, path) else source.download(self) end self end |
#duplicate(destination) ⇒ Object
65 66 67 68 |
# File 'lib/inprovise/local_file.rb', line 65 def duplicate(destination) FileUtils.cp(path, destination.path) destination end |
#exists? ⇒ Boolean
30 31 32 |
# File 'lib/inprovise/local_file.rb', line 30 def exists? File.exists?(@path) end |
#file? ⇒ Boolean
38 39 40 |
# File 'lib/inprovise/local_file.rb', line 38 def file? File.file?(path) end |
#group ⇒ Object
113 114 115 |
# File 'lib/inprovise/local_file.rb', line 113 def group Etc.getgrgid(File.stat(path).gid).name end |
#hash ⇒ Object
25 26 27 28 |
# File 'lib/inprovise/local_file.rb', line 25 def hash return nil unless exists? Digest::SHA1.file(path).hexdigest end |
#is_local? ⇒ Boolean
117 118 119 |
# File 'lib/inprovise/local_file.rb', line 117 def is_local? true end |
#matches?(other) ⇒ Boolean
deosnt check permissions or user. should it?
48 49 50 |
# File 'lib/inprovise/local_file.rb', line 48 def matches?(other) self.exists? && other.exists? && self.hash == other.hash end |
#permissions ⇒ Object
100 101 102 |
# File 'lib/inprovise/local_file.rb', line 100 def File.stat(path).mode & 0777 end |
#resolve(path) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/inprovise/local_file.rb', line 17 def resolve(path) if path =~ /^\// path else File.join(Inprovise.root, path) end end |
#set_owner(user, group = nil) ⇒ Object
104 105 106 107 |
# File 'lib/inprovise/local_file.rb', line 104 def set_owner(user, group=nil) FileUtils.chown_R(user, group, path) self end |
#set_permissions(mask) ⇒ Object
95 96 97 98 |
# File 'lib/inprovise/local_file.rb', line 95 def (mask) FileUtils.chmod_R(mask, path) self end |
#upload(destination) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/inprovise/local_file.rb', line 70 def upload(destination) destination = @context.remote(destination) if String === destination if destination.is_local? FileUtils.cp(path, destination.path) else destination.upload(self) end destination end |
#user ⇒ Object
109 110 111 |
# File 'lib/inprovise/local_file.rb', line 109 def user Etc.getpwuid(File.stat(path).uid).name end |