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(context, path) ⇒ LocalFile
constructor
A new instance of LocalFile.
- #is_local? ⇒ Boolean
-
#matches?(other) ⇒ Boolean
deosnt check permissions or user.
- #move_to(destination) ⇒ Object
- #permissions ⇒ Object
- #resolve(path) ⇒ Object
- #set_owner(user, group = nil) ⇒ Object
- #set_permissions(mask) ⇒ Object
- #upload(destination) ⇒ Object
- #user ⇒ Object
Constructor Details
#initialize(context, path) ⇒ LocalFile
Returns a new instance of LocalFile.
13 14 15 16 |
# File 'lib/inprovise/local_file.rb', line 13 def initialize(context, path) @context = context @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
43 44 45 46 |
# File 'lib/inprovise/local_file.rb', line 43 def content return File.read(@path) if exists? nil end |
#copy_from(source) ⇒ Object
72 73 74 75 |
# File 'lib/inprovise/local_file.rb', line 72 def copy_from(source) source = self.class.new(source) if String === source source.copy_to(self) end |
#copy_to(destination) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/inprovise/local_file.rb', line 53 def copy_to(destination) destination = self.class.new(destination) if String === destination if destination.is_local? duplicate(destination) else upload(destination) end end |
#delete! ⇒ Object
103 104 105 106 |
# File 'lib/inprovise/local_file.rb', line 103 def delete! FileUtils.rm(path) if exists? self end |
#directory? ⇒ Boolean
35 36 37 |
# File 'lib/inprovise/local_file.rb', line 35 def directory? File.directory?(path) end |
#download(source) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/inprovise/local_file.rb', line 93 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
77 78 79 80 81 |
# File 'lib/inprovise/local_file.rb', line 77 def duplicate(destination) destination = self.class.new(destination) if String === destination FileUtils.cp(path, destination.path) destination end |
#exists? ⇒ Boolean
31 32 33 |
# File 'lib/inprovise/local_file.rb', line 31 def exists? File.exists?(@path) end |
#file? ⇒ Boolean
39 40 41 |
# File 'lib/inprovise/local_file.rb', line 39 def file? File.file?(path) end |
#group ⇒ Object
126 127 128 |
# File 'lib/inprovise/local_file.rb', line 126 def group Etc.getgrgid(File.stat(path).gid).name end |
#hash ⇒ Object
26 27 28 29 |
# File 'lib/inprovise/local_file.rb', line 26 def hash return nil unless exists? Digest::SHA1.file(path).hexdigest end |
#is_local? ⇒ Boolean
130 131 132 |
# File 'lib/inprovise/local_file.rb', line 130 def is_local? true end |
#matches?(other) ⇒ Boolean
deosnt check permissions or user. should it?
49 50 51 |
# File 'lib/inprovise/local_file.rb', line 49 def matches?(other) self.exists? && other.exists? && self.hash == other.hash end |
#move_to(destination) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/inprovise/local_file.rb', line 62 def move_to(destination) destination = self.class.new(destination) if String === destination if destination.is_local? FileUtils.mv(path, destination.path) else upload(destination) end destination end |
#permissions ⇒ Object
113 114 115 |
# File 'lib/inprovise/local_file.rb', line 113 def File.stat(path).mode & 0777 end |
#resolve(path) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/inprovise/local_file.rb', line 18 def resolve(path) if path =~ /^\// path else File.join(Inprovise.root, path) end end |
#set_owner(user, group = nil) ⇒ Object
117 118 119 120 |
# File 'lib/inprovise/local_file.rb', line 117 def set_owner(user, group=nil) FileUtils.chown_R(user, group, path) self end |
#set_permissions(mask) ⇒ Object
108 109 110 111 |
# File 'lib/inprovise/local_file.rb', line 108 def (mask) FileUtils.chmod_R(mask, path) self end |
#upload(destination) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/inprovise/local_file.rb', line 83 def upload(destination) destination = @context.remote(destination) if String === destination if destination.is_local? duplicate(destination) else destination.upload(self) end destination end |
#user ⇒ Object
122 123 124 |
# File 'lib/inprovise/local_file.rb', line 122 def user Etc.getpwuid(File.stat(path).uid).name end |