Class: Inprovise::LocalFile

Inherits:
Object
  • Object
show all
Defined in:
lib/inprovise/local_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#pathObject (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

#contentObject



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

Returns:

  • (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

Returns:

  • (Boolean)


31
32
33
# File 'lib/inprovise/local_file.rb', line 31

def exists?
  File.exists?(@path)
end

#file?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/inprovise/local_file.rb', line 39

def file?
  File.file?(path)
end

#groupObject



126
127
128
# File 'lib/inprovise/local_file.rb', line 126

def group
  Etc.getgrgid(File.stat(path).gid).name
end

#hashObject



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

Returns:

  • (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?

Returns:

  • (Boolean)


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

#permissionsObject



113
114
115
# File 'lib/inprovise/local_file.rb', line 113

def permissions
   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 set_permissions(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

#userObject



122
123
124
# File 'lib/inprovise/local_file.rb', line 122

def user
  Etc.getpwuid(File.stat(path).uid).name
end