Class: Crackup::DirectoryObject

Inherits:
Object
  • Object
show all
Includes:
FileSystemObject, Enumerable
Defined in:
lib/crackup/directory_object.rb

Overview

Represents a directory on the local filesystem. Can contain any number of Crackup::FileSystemObjects as children.

Instance Attribute Summary collapse

Attributes included from FileSystemObject

#name, #name_hash

Instance Method Summary collapse

Methods included from FileSystemObject

from

Constructor Details

#initialize(name) ⇒ DirectoryObject

– Public Instance Methods ++



17
18
19
20
21
22
23
24
25
# File 'lib/crackup/directory_object.rb', line 17

def initialize(name)
  unless File.directory?(name)
    raise ArgumentError, "#{name} is not a directory"
  end

  super(name)

  refresh_children()
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



11
12
13
# File 'lib/crackup/directory_object.rb', line 11

def children
  @children
end

Instance Method Details

#==(directory) ⇒ Object

Compares the specified Crackup::DirectoryObject to this one. Returns true if the directories and all their children are the same, false otherwise.



30
31
32
33
# File 'lib/crackup/directory_object.rb', line 30

def ==(directory)
  return false unless directory.name == @name      
  return directory.all?{|child| child == @children[child.name] }
end

#[](key) ⇒ Object



35
36
37
# File 'lib/crackup/directory_object.rb', line 35

def [](key)
  return @children[key]
end

#eachObject



39
40
41
# File 'lib/crackup/directory_object.rb', line 39

def each
  @children.each {|child| yield child }
end

#find(pattern) ⇒ Object

Gets an array of files contained in this directory or its children whose local filenames match pattern.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/crackup/directory_object.rb', line 45

def find(pattern)
  files = []
  
  @children.each do |name, child|
    if File.fnmatch?(pattern, child.name)
      files << child
      next
    end
    
    if child.is_a?(Crackup::DirectoryObject)
      files << result if result = child.find(pattern)
    end
  end
  
  return files
end

#refresh_childrenObject

Builds a Hash of child objects by analyzing the local filesystem. A refresh is automatically performed when the object is instantiated.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/crackup/directory_object.rb', line 64

def refresh_children
  @children = {}

  Dir.open(@name) do |dir|
    dir.each do |filename|
      next if filename == '.' || filename == '..'

      path = File.join(dir.path, filename).gsub("\\", "/")

      # Skip this file if it's in the exclusion list.

      unless Crackup::options[:exclude].nil?
        next if Crackup::options[:exclude].any? do |pattern|
          File.fnmatch?(pattern, path)
        end
      end

      @children[path] = Crackup::FileSystemObject.from(path)
    end
  end
  
  return @children
end

#removeObject

Removes the remote copy of this directory and all its children.



88
89
90
# File 'lib/crackup/directory_object.rb', line 88

def remove
  @children.each_value {|child| child.remove }
end

#restore(path) ⇒ Object

Restores the remote copy of this directory to the specified local path.



93
94
95
# File 'lib/crackup/directory_object.rb', line 93

def restore(path)
  @children.each_value {|child| child.restore(path) }
end

#to_sObject



97
98
99
100
101
# File 'lib/crackup/directory_object.rb', line 97

def to_s
  childnames = []
  @children.each_value {|child| childnames << child.to_s }
  return childnames.join("\n")
end

#updateObject

Uploads this directory and all its children to the remote location.



104
105
106
# File 'lib/crackup/directory_object.rb', line 104

def update
  @children.each_value {|child| child.update }
end