Class: Rails::Paths::Path

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rails/paths.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, current, paths, options = {}) ⇒ Path

Returns a new instance of Path.



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rails/paths.rb', line 114

def initialize(root, current, paths, options = {})
  @paths    = paths
  @current  = current
  @root     = root
  @glob     = options[:glob]

  options[:autoload_once] ? autoload_once! : skip_autoload_once!
  options[:eager_load]    ? eager_load!    : skip_eager_load!
  options[:autoload]      ? autoload!      : skip_autoload!
  options[:load_path]     ? load_path!     : skip_load_path!
end

Instance Attribute Details

#globObject

Returns the value of attribute glob.



112
113
114
# File 'lib/rails/paths.rb', line 112

def glob
  @glob
end

Instance Method Details

#<<(path) ⇒ Object Also known as: push



161
162
163
# File 'lib/rails/paths.rb', line 161

def <<(path)
  @paths << path
end

#childrenObject



126
127
128
129
130
131
# File 'lib/rails/paths.rb', line 126

def children
  keys = @root.keys.find_all { |k|
    k.start_with?(@current) && k != @current
  }
  @root.values_at(*keys.sort)
end

#concat(paths) ⇒ Object



166
167
168
# File 'lib/rails/paths.rb', line 166

def concat(paths)
  @paths.concat paths
end

#each(&block) ⇒ Object



157
158
159
# File 'lib/rails/paths.rb', line 157

def each(&block)
  @paths.each(&block)
end

#existentObject

Returns all expanded paths but only if they exist in the filesystem.



200
201
202
# File 'lib/rails/paths.rb', line 200

def existent
  expanded.select { |f| File.exist?(f) }
end

#existent_directoriesObject



204
205
206
# File 'lib/rails/paths.rb', line 204

def existent_directories
  expanded.select { |d| File.directory?(d) }
end

#expandedObject Also known as: to_a

Expands all paths against the root and return all unique values.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rails/paths.rb', line 179

def expanded
  raise "You need to set a path root" unless @root.path
  result = []

  each do |p|
    path = File.expand_path(p, @root.path)

    if @glob && File.directory?(path)
      Dir.chdir(path) do
        result.concat(Dir.glob(@glob).map { |file| File.join path, file }.sort)
      end
    else
      result << path
    end
  end

  result.uniq!
  result
end

#firstObject



133
134
135
# File 'lib/rails/paths.rb', line 133

def first
  expanded.first
end

#lastObject



137
138
139
# File 'lib/rails/paths.rb', line 137

def last
  expanded.last
end

#to_aryObject



174
175
176
# File 'lib/rails/paths.rb', line 174

def to_ary
  @paths
end

#unshift(*paths) ⇒ Object



170
171
172
# File 'lib/rails/paths.rb', line 170

def unshift(*paths)
  @paths.unshift(*paths)
end