Module: Eco::Data::Files::RelativePath

Included in:
ClassMethods, Folder
Defined in:
lib/eco/data/files/relative_path.rb

Instance Method Summary collapse

Instance Method Details

#to_relative_path(of, from: '.') ⇒ Object



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
# File 'lib/eco/data/files/relative_path.rb', line 7

def to_relative_path(of, from: '.')
  require 'pathname'

  pn_from = to_pathname(from)

  case of
  when String, Pathname
    pn_of = to_pathname(of)

    begin
      pn_of.relative_path_from(pn_from).to_s
    rescue ArgumentError => err
      puts err
      of
    end
  when Array
    of.map do |path|
      to_relative_path(path, from: pn_from)
    end
  else
    msg  = 'Expected `of` argument to be '
    msg << 'String, Pathname, Array<String>, or Array<Pathname>. '
    msg << "Given: #{of.class}"
    raise ArgumentError, msg
  end
end