Class: Rush::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/rush/entry.rb

Overview

Rush::Entry is the base class for Rush::File and Rush::Dir. One or more of these is instantiated whenever you use square brackets to access the filesystem on a box, as well as any other operation that returns an entry or list of entries.

Direct Known Subclasses

Dir, File

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(full_path, box = nil) ⇒ Entry

Initialize with full path to the file or dir, and the box it resides on.



9
10
11
12
13
14
# File 'lib/rush/entry.rb', line 9

def initialize(full_path, box=nil)
  full_path = ::File.expand_path(full_path, '/')
  @path = ::File.dirname(full_path)
  @name = ::File.basename(full_path)
  @box = box || Rush::Box.new('localhost')
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/rush/entry.rb', line 16

def method_missing(meth, *args, &block)
  if executables.include? meth.to_s
    open_with meth, *args
  else
    super
  end
end

Instance Attribute Details

#boxObject (readonly)

Returns the value of attribute box.



6
7
8
# File 'lib/rush/entry.rb', line 6

def box
  @box
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/rush/entry.rb', line 6

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/rush/entry.rb', line 6

def path
  @path
end

Class Method Details

.factory(full_path, box = nil) ⇒ Object

The factory checks to see if the full path has a trailing slash for creating a Rush::Dir rather than the default Rush::File.



32
33
34
35
36
37
38
39
40
# File 'lib/rush/entry.rb', line 32

def self.factory(full_path, box=nil)
  if full_path.tail(1) == '/'
    Rush::Dir.new(full_path, box)
  elsif File.directory?(full_path)
    Rush::Dir.new(full_path, box)
  else
    Rush::File.new(full_path, box)
  end
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



220
221
222
# File 'lib/rush/entry.rb', line 220

def ==(other)       # :nodoc:
  full_path == other.full_path and box == other.box
end

#accessObject

Returns a hash with up to nine values, combining user/group/other with read/write/execute. The key is omitted if the value is false.

Examples:

entry.access                   # -> { :user_can_read => true, :user_can_write => true, :group_can_read => true }
entry.access[:other_can_read]  # -> true or nil


176
177
178
# File 'lib/rush/entry.rb', line 176

def access
  Rush::Access.new.from_octal(stat[:mode]).display_hash
end

#access=(options) ⇒ Object

Set the access permissions for the entry.

Permissions are set by role and permissions combinations which can be specified individually or grouped together. :user_can => :read, :user_can => :write is the same as :user_can => :read_write.

You can also insert ‘and’ if you find it reads better, like :user_and_group_can => :read_and_write.

Any permission excluded is set to deny access. The access call does not set partial permissions which combine with the existing state of the entry, like “chmod o+r” would.

Examples:

file.access = { :user_can => :read_write, :group_other_can => :read }
dir.access = { :user => 'adam', :group => 'users', :read_write_execute => :user_group }


164
165
166
# File 'lib/rush/entry.rb', line 164

def access=(options)
  connection.set_access(full_path, Rush::Access.parse(options))
end

#changed_atObject

Timestamp of most recent change to the entry (permissions, contents, etc).



80
81
82
# File 'lib/rush/entry.rb', line 80

def changed_at
  stat[:ctime]
end

#chown(user = nil, group = nil, options = {}) ⇒ Object

Change entry ownership

Changes owner and group on the named files (in list) to the user user and the group group. user and group may be an ID (Integer/String) or a name (String). If user or group is nil, this method does not change the attribute.

Parameters:

  • user (string/integer) (defaults to: nil)

    The user to own the file

  • group (string/integer) (defaults to: nil)

    The group to own the file

  • options (hash) (defaults to: {})

    the options to pass to FileUtils.chown (eg. ‘noop’, ‘verbose’ or ‘recursive’ )



188
189
190
191
# File 'lib/rush/entry.rb', line 188

def chown(user = nil, group = nil, options = {})
  connection.chown(full_path, user, group, options)
  self
end

#chown_R(user = nil, group = nil, options = {}) ⇒ Object

Shortcut to Entry::chown to pass the ‘recursive’ option by default



195
196
197
198
# File 'lib/rush/entry.rb', line 195

def chown_R(user = nil, group = nil, options = {})
  options[:recursive] = true
  chown(user, group, options)
end

#connectionObject



54
55
56
# File 'lib/rush/entry.rb', line 54

def connection
  box ? box.connection : Rush::Connection::Local.new
end

#copy_to(dir) ⇒ Object

Copy the entry to another dir. Returns an object representing the new copy.

Raises:



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rush/entry.rb', line 115

def copy_to(dir)
  raise Rush::NotADir unless dir.class == Rush::Dir

  if box == dir.box
    connection.copy(full_path, dir.full_path)
  else
    archive = connection.read_archive(full_path)
    dir.box.connection.write_archive(archive, dir.full_path)
  end

  new_full_path = "#{dir.full_path}#{name}"
  self.class.new(new_full_path, dir.box)
end

#destroyObject

Destroy the entry. If it is a dir, everything inside it will also be destroyed.



216
217
218
# File 'lib/rush/entry.rb', line 216

def destroy
  connection.destroy(full_path)
end

#duplicate(new_name) ⇒ Object

Rename an entry to another name within the same dir. The existing object will not be affected, but a new object representing the newly-created entry will be returned.



106
107
108
109
110
111
# File 'lib/rush/entry.rb', line 106

def duplicate(new_name)
  raise Rush::NameCannotContainSlash if new_name.match(/\//)
  new_full_path = "#{@path}/#{new_name}"
  connection.copy(full_path, new_full_path)
  self.class.new(new_full_path, box)
end

#executablesObject



24
25
26
27
28
# File 'lib/rush/entry.rb', line 24

def executables
  ENV['PATH'].split(':')
    .map { |x| Rush::Dir.new(x).entries.map(&:name) }
    .flatten
end

#exists?Boolean

Return true if the entry currently exists on the filesystem of the box.

Returns:

  • (Boolean)


72
73
74
75
76
77
# File 'lib/rush/entry.rb', line 72

def exists?
  stat
  true
rescue Rush::DoesNotExist
  false
end

#full_pathObject



63
64
65
# File 'lib/rush/entry.rb', line 63

def full_path
  "#{@path}/#{@name}"
end

#hidden?Boolean

Unix convention considers entries starting with a . to be hidden.

Returns:

  • (Boolean)


144
145
146
# File 'lib/rush/entry.rb', line 144

def hidden?
  name.slice(0, 1) == '.'
end

#inspectObject

:nodoc:



50
51
52
# File 'lib/rush/entry.rb', line 50

def inspect   # :nodoc:
  "#{box}:#{full_path}"
end

#last_accessedObject

Timestamp that entry was last accessed (read from or written to).



90
91
92
# File 'lib/rush/entry.rb', line 90

def last_accessed
  stat[:atime]
end

#last_modifiedObject

Timestamp of last modification of the contents.



85
86
87
# File 'lib/rush/entry.rb', line 85

def last_modified
  stat[:mtime]
end

#mimic(from) ⇒ Object

:nodoc:



137
138
139
140
141
# File 'lib/rush/entry.rb', line 137

def mimic(from)      # :nodoc:
  @box = from.box
  @path = from.path
  @name = from.name
end

#move_to(dir) ⇒ Object

Move the entry to another dir. The object will be updated to show its new location.



131
132
133
134
135
# File 'lib/rush/entry.rb', line 131

def move_to(dir)
  moved = copy_to(dir)
  destroy
  mimic(moved)
end

#ownerObject

Chown in ruby way. Ruby way is creating accessors.



201
202
203
204
# File 'lib/rush/entry.rb', line 201

def owner
  stat = ::File.stat(full_path)
  { user: Etc.getpwuid(stat.uid).name, group: Etc.getgrgid(stat.gid).name }
end

#owner=(params) ⇒ Object



206
207
208
209
210
211
212
213
# File 'lib/rush/entry.rb', line 206

def owner=(params)
  case params
  when Hash then chown(params.delete(:user), params.delete(:group), params)
  when String then chown(params)
  when Numeric then chown(Etc.getpwuid(params).name)
  else raise 'Something wrong with params for chown'
  end
end

#parentObject

The parent dir. For example, box.parent == box



59
60
61
# File 'lib/rush/entry.rb', line 59

def parent
  @parent ||= Rush::Dir.new(@path)
end

#quoted_pathObject



67
68
69
# File 'lib/rush/entry.rb', line 67

def quoted_path
  Rush.quote(full_path)
end

#rename(new_name) ⇒ Object Also known as: mv

Rename an entry to another name within the same dir. The object’s name will be updated to match the change on the filesystem.



96
97
98
99
100
# File 'lib/rush/entry.rb', line 96

def rename(new_name)
  connection.rename(@path, @name, new_name)
  @name = new_name
  self
end

#to_sObject

:nodoc:



42
43
44
45
46
47
48
# File 'lib/rush/entry.rb', line 42

def to_s      # :nodoc:
  if box.host == 'localhost'
    "#{full_path}"
  else
    inspect
  end
end