Class: Codependency::Path

Inherits:
Array
  • Object
show all
Defined in:
lib/codependency/path.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(extensions = %w| .rb | ) ⇒ Path

Returns a new instance of Path.



6
7
8
# File 'lib/codependency/path.rb', line 6

def initialize( extensions=%w| .rb | )
  @extensions = Array(extensions)
end

Instance Attribute Details

#extensionsObject (readonly)

Returns the value of attribute extensions.



9
10
11
# File 'lib/codependency/path.rb', line 9

def extensions
  @extensions
end

Instance Method Details

#<<(str) ⇒ Object

Appends a path to this path set. If the path exists, it will be expanded. Raises Errno::ENOENT if the path does not exist. Raises Errno::ENOTDIR if the path is not a directory.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/codependency/path.rb', line 16

def <<( str )
  path = Pathname( str )

  case
  when !path.exist?
    raise Errno::ENOENT, path.to_path
  when !path.directory?
    raise Errno::ENOTDIR, path.to_path
  else
    super path.expand_path
  end
end

#[](path_or_index) ⇒ Object

Sugar syntax for calling #find if given a string. Otherwise, behaves like normal array access.



32
33
34
35
36
37
38
# File 'lib/codependency/path.rb', line 32

def []( path_or_index )
  if path_or_index.is_a? String
    find path_or_index
  else
    super
  end
end

#find(str) ⇒ Object

Attempts to find the given file in this path set. Raises Errno::ENOENT if the file cannot be found.



43
44
45
46
47
48
49
50
51
# File 'lib/codependency/path.rb', line 43

def find( str )
  super lambda { raise Errno::ENOENT, str } do |dir|
    path = extensions.find do |ext|
      file = dir.join str + ext
      break file if file.exist?
    end
    break path if path
  end
end