Class: Pathname

Inherits:
Object show all
Includes:
Comparable
Defined in:
opal/stdlib/pathname.rb,
opal/stdlib/nodejs/pathname.rb

Overview

Portions from Author:: Tanaka Akira [email protected]

Constant Summary collapse

SEPARATOR_PAT =
/#{Regexp.quote File::SEPARATOR}/
SAME_PATHS =
if File::FNM_SYSCASE.nonzero?
  # Avoid #zero? here because #casecmp can return nil.
  proc { |a, b| a.casecmp(b) == 0 }
else
  proc { |a, b| a == b }
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Pathname

Returns a new instance of Pathname.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'opal/stdlib/pathname.rb', line 8

def initialize(path)
  if Pathname === path
    @path = path.path.to_s
  elsif path.respond_to?(:to_path)
    @path = path.to_path
  elsif path.is_a?(String)
    @path = path
  elsif path.nil?
    raise TypeError, 'no implicit conversion of nil into String'
  else
    raise TypeError, "no implicit conversion of #{path.class} into String"
  end
  raise ArgumentError if @path == "\0"
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



27
28
29
# File 'opal/stdlib/pathname.rb', line 27

def path
  @path
end

Class Method Details

.pwdObject



23
24
25
# File 'opal/stdlib/pathname.rb', line 23

def self.pwd
  new(Dir.pwd)
end

Instance Method Details

#+(other) ⇒ Object



85
86
87
88
# File 'opal/stdlib/pathname.rb', line 85

def +(other)
  other = Pathname.new(other) unless Pathname === other
  Pathname.new(plus(@path, other.to_s))
end

#<=>(other) ⇒ Object



164
165
166
# File 'opal/stdlib/pathname.rb', line 164

def <=>(other)
  path <=> other.path
end

#==(other) ⇒ Object Also known as: eql?, ===



29
30
31
# File 'opal/stdlib/pathname.rb', line 29

def ==(other)
  other.path == @path
end

#absolute?Boolean

Returns:



33
34
35
# File 'opal/stdlib/pathname.rb', line 33

def absolute?
  !relative?
end

#basenameObject



152
153
154
# File 'opal/stdlib/pathname.rb', line 152

def basename
  Pathname.new(File.basename(@path))
end

#chop_basename(path) ⇒ Object

:nodoc:



45
46
47
48
49
50
51
52
53
# File 'opal/stdlib/pathname.rb', line 45

def chop_basename(path) # :nodoc:
  base = File.basename(path)
  # ruby uses /^#{SEPARATOR_PAT}?$/o but having issues with interpolation
  if Regexp.new("^#{Pathname::SEPARATOR_PAT.source}?$") =~ base
    return nil
  else
    return path[0, path.rindex(base)], base
  end
end

#cleanpathObject



69
70
71
# File 'opal/stdlib/pathname.rb', line 69

def cleanpath
  `return Opal.normalize(#{@path})`
end

#directory?Boolean

Returns:



156
157
158
# File 'opal/stdlib/pathname.rb', line 156

def directory?
  File.directory?(@path)
end

#dirnameObject



148
149
150
# File 'opal/stdlib/pathname.rb', line 148

def dirname
  Pathname.new(File.dirname(@path))
end

#entriesObject



217
218
219
# File 'opal/stdlib/pathname.rb', line 217

def entries
  Dir.entries(@path).map { |f| self.class.new(f) }
end

#expand_pathObject



81
82
83
# File 'opal/stdlib/pathname.rb', line 81

def expand_path
  Pathname.new(File.expand_path(@path))
end

#extnameObject



160
161
162
# File 'opal/stdlib/pathname.rb', line 160

def extname
  File.extname(@path)
end

#hashObject



77
78
79
# File 'opal/stdlib/pathname.rb', line 77

def hash
  @path
end

#join(*args) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# File 'opal/stdlib/pathname.rb', line 131

def join(*args)
  return self if args.empty?
  result = args.pop
  result = Pathname.new(result) unless Pathname === result
  return result if result.absolute?
  args.reverse_each do |arg|
    arg = Pathname.new(arg) unless Pathname === arg
    result = arg + result
    return result if result.absolute?
  end
  self + result
end

#parentObject



59
60
61
62
63
# File 'opal/stdlib/pathname.rb', line 59

def parent
  new_path = @path.sub(%r{/([^/]+/?$)}, '')
  new_path = absolute? ? '/' : '.' if new_path == ''
  Pathname.new(new_path)
end

#plus(path1, path2) ⇒ Object

-> path # :nodoc:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'opal/stdlib/pathname.rb', line 90

def plus(path1, path2) # -> path # :nodoc:
  prefix2 = path2
  index_list2 = []
  basename_list2 = []
  while (r2 = chop_basename(prefix2))
    prefix2, basename2 = r2
    index_list2.unshift prefix2.length
    basename_list2.unshift basename2
  end
  return path2 if prefix2 != ''
  prefix1 = path1
  while true
    while !basename_list2.empty? && basename_list2.first == '.'
      index_list2.shift
      basename_list2.shift
    end
    break unless (r1 = chop_basename(prefix1))
    prefix1, basename1 = r1
    next if basename1 == '.'
    if basename1 == '..' || basename_list2.empty? || basename_list2.first != '..'
      prefix1 += basename1
      break
    end
    index_list2.shift
    basename_list2.shift
  end
  r1 = chop_basename(prefix1)
  if !r1 && /#{SEPARATOR_PAT}/ =~ File.basename(prefix1)
    while !basename_list2.empty? && basename_list2.first == '..'
      index_list2.shift
      basename_list2.shift
    end
  end
  if !basename_list2.empty?
    suffix2 = path2[index_list2.first..-1]
    r1 ? File.join(prefix1, suffix2) : prefix1 + suffix2
  else
    r1 ? prefix1 : File.dirname(prefix1)
  end
end

#relative?Boolean

Returns:



37
38
39
40
41
42
43
# File 'opal/stdlib/pathname.rb', line 37

def relative?
  path = @path
  while (r = chop_basename(path))
    path, = r
  end
  path == ''
end

#relative_path_from(base_directory) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'opal/stdlib/pathname.rb', line 181

def relative_path_from(base_directory)
  dest_directory = cleanpath.to_s
  base_directory = base_directory.cleanpath.to_s
  dest_prefix = dest_directory
  dest_names = []
  while (r = chop_basename(dest_prefix))
    dest_prefix, basename = r
    dest_names.unshift basename if basename != '.'
  end
  base_prefix = base_directory
  base_names = []
  while (r = chop_basename(base_prefix))
    base_prefix, basename = r
    base_names.unshift basename if basename != '.'
  end
  unless SAME_PATHS[dest_prefix, base_prefix]
    raise ArgumentError, "different prefix: #{dest_prefix.inspect} and #{base_directory.inspect}"
  end
  while !dest_names.empty? &&
        !base_names.empty? &&
        SAME_PATHS[dest_names.first, base_names.first]
    dest_names.shift
    base_names.shift
  end
  if base_names.include? '..'
    raise ArgumentError, "base_directory has ..: #{base_directory.inspect}"
  end
  base_names.fill('..')
  relpath_names = base_names + dest_names
  if relpath_names.empty?
    Pathname.new('.')
  else
    Pathname.new(File.join(*relpath_names))
  end
end

#root?Boolean

Returns:



55
56
57
# File 'opal/stdlib/pathname.rb', line 55

def root?
  @path == '/'
end

#splitObject



144
145
146
# File 'opal/stdlib/pathname.rb', line 144

def split
  [dirname, basename]
end

#sub(*args) ⇒ Object



65
66
67
# File 'opal/stdlib/pathname.rb', line 65

def sub(*args)
  Pathname.new(@path.sub(*args))
end

#to_pathObject



73
74
75
# File 'opal/stdlib/pathname.rb', line 73

def to_path
  @path
end

#to_sObject



172
173
174
# File 'opal/stdlib/pathname.rb', line 172

def to_path
  @path
end

#to_strObject



171
172
173
# File 'opal/stdlib/pathname.rb', line 171

def to_path
  @path
end