Class: Folder::Magic

Inherits:
Object
  • Object
show all
Defined in:
lib/folder/magic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMagic

Returns a new instance of Magic.



9
10
11
12
13
# File 'lib/folder/magic.rb', line 9

def initialize      
  @dir_stack = []
  @current_path = FileUtils.pwd
  
end

Instance Attribute Details

#current_pathObject

Returns the value of attribute current_path.



5
6
7
# File 'lib/folder/magic.rb', line 5

def current_path
  @current_path
end

#dir_stackObject

Returns the value of attribute dir_stack.



6
7
8
# File 'lib/folder/magic.rb', line 6

def dir_stack
  @dir_stack
end

#relative_pathObject

Returns the value of attribute relative_path.



7
8
9
# File 'lib/folder/magic.rb', line 7

def relative_path
  @relative_path
end

Instance Method Details

#all(*globs) ⇒ Object



54
55
56
57
58
# File 'lib/folder/magic.rb', line 54

def all(*globs)
  globs = '*.rb' if globs.empty?      
  list = FileList.new(globs) 
  magic_list(list) 
end

#all_recursive(*globs) ⇒ Object



48
49
50
51
52
# File 'lib/folder/magic.rb', line 48

def all_recursive(*globs)
  globs = '**/*.rb' if globs.empty?      
  list = FileList.new(globs) 
  magic_list(list) 
end

#enter(dir) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/folder/magic.rb', line 27

def enter(dir)   
  path = FileUtils.pwd
  dir_stack.push path    
  FileUtils.cd dir if !dir.empty?
  @current_path = FileUtils.pwd
  self.relative_to_me
  if block_given?
    yield self                    
    exit              
  end      
  self
end

#exitObject



40
41
42
43
44
45
# File 'lib/folder/magic.rb', line 40

def exit
  current_path = dir_stack.last
  old_dir = dir_stack.last 
  dir_stack.pop 
  FileUtils.cd old_dir if old_dir               
end

#meObject



23
24
25
# File 'lib/folder/magic.rb', line 23

def me       
  Pathname.new(current_path).basename.to_s
end

#relative_to_meObject



15
16
17
# File 'lib/folder/magic.rb', line 15

def relative_to_me
  @relative_path = me
end

#require_all(*folders) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/folder/magic.rb', line 61

def require_all(*folders)  
  return require_all_here(folders[0]) if folders.size == 1 && folders[0] =~ /.rb$/
  relative = {:relative_to => relative_path || ''}
  relative = folders.pop if folders && !folders.empty? && folders.last.kind_of?(Hash)      

  # puts "relative_to: #{relative[:relative_to]}"

  if folders.empty?
    files = all.dup.extend(MagicList)
    files.rel_path = relative[:relative_to]
    return files.do_require        
  end

  # puts "iterate"
  folders.each do |folder|   
    enter folder do |f|   
      file = f.all.dup.extend(MagicList)
      require_relative_to = relative ? relative_to(folder, relative) : folder                     
      file.do_require(require_relative_to)
    end
  end
end

#require_all_here(file_name) ⇒ Object



118
119
120
# File 'lib/folder/magic.rb', line 118

def require_all_here(file_name) 
  require_all File.dirname(file_name)               
end

#require_all_recursive(*folders) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/folder/magic.rb', line 84

def require_all_recursive(*folders)  
  return require_all_here(folders[0]) if folders.size == 1 && folders[0] =~ /.rb$/
  relative = {:relative_to => relative_path || ''}
  relative = folders.pop if folders && !folders.empty? && folders.last.kind_of?(Hash)      

  # puts "relative_to: #{relative[:relative_to]}"

  if folders.empty?
    files = all_recursive.dup.extend(MagicList)
    files.rel_path = relative[:relative_to]
    return files.do_require        
  end

  # puts "iterate"
  folders.each do |folder|   
    enter folder do |f|   
      file = f.all_recursive.dup.extend(MagicList)
      require_relative_to = relative ? relative_to(folder, relative) : folder                     
      file.do_require(require_relative_to)
    end
  end
end

#require_all_recursive_here(file_name) ⇒ Object



122
123
124
# File 'lib/folder/magic.rb', line 122

def require_all_recursive_here(file_name) 
  require_all_recursive File.dirname(file_name)               
end

#require_me(*files) ⇒ Object



108
109
110
111
# File 'lib/folder/magic.rb', line 108

def require_me(*files)
  file = all(files).dup.extend(MagicList)
  file.do_require(relative_path)     
end

#require_me_recursive(*files) ⇒ Object



113
114
115
116
# File 'lib/folder/magic.rb', line 113

def require_me_recursive(*files)
  file = all_recursive(files).dup.extend(MagicList)
  file.do_require(relative_path)     
end

#to_sObject



19
20
21
# File 'lib/folder/magic.rb', line 19

def to_s
  "path: #{current_path}, directory stack: #{dir_stack.inspect}"
end