File: aruba_path.rb

package info (click to toggle)
ruby-aruba 2.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 968 kB
  • sloc: javascript: 6,850; ruby: 4,010; makefile: 5
file content (72 lines) | stat: -rw-r--r-- 1,288 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true

require 'pathname'

# Aruba
module Aruba
  # Pathname for aruba files and directories
  #
  # @private
  class ArubaPath
    def initialize(path)
      @obj = [path.to_s].flatten
    end

    def to_str
      to_pathname.to_s
    end

    def to_s
      to_str
    end

    # Add directory/file to path
    #
    # @param [String] p
    #   The path to be added
    #
    # @example
    #   path = ArubaPath.new 'path/to/dir.d'
    #   path << 'subdir.d
    #   # or path.push 'subdir.d
    #   puts path
    #   # => path/to/dir.d/subdir.d
    def push(p)
      @obj << p
    end
    alias << push

    # Remove last pushed component of path
    #
    # @example
    #   path = ArubaPath.new 'path/to'
    #   path.push 'dir'
    #   path.pop
    #   puts path # => path/to
    def pop
      @obj.pop
    end

    # Return string at index
    #
    # @param [Integer, Range] index
    def [](index)
      to_s[index]
    end

    private

    # Get path
    def to_pathname
      current_path = @obj.inject do |path, element|
        if element.start_with?('~') ||
           Aruba.platform.absolute_path?(element)
          element
        else
          File.join(path, element)
        end
      end
      ::Pathname.new(current_path)
    end
  end
end