paths.rb 650 B

123456789101112131415161718192021222324252627
  1. require 'pathname'
  2. require 'hike/normalized_array'
  3. module Hike
  4. # `Paths` is an internal collection for tracking path strings.
  5. class Paths < NormalizedArray
  6. def initialize(root = ".")
  7. @root = Pathname.new(root)
  8. super()
  9. end
  10. # Relative paths added to this array are expanded relative to `@root`.
  11. #
  12. # paths = Paths.new("/usr/local")
  13. # paths << "tmp"
  14. # paths << "/tmp"
  15. #
  16. # paths
  17. # # => ["/usr/local/tmp", "/tmp"]
  18. #
  19. def normalize_element(path)
  20. path = Pathname.new(path)
  21. path = @root.join(path) if path.relative?
  22. path.expand_path.to_s
  23. end
  24. end
  25. end