Class: Hiiro::Paths

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/paths.rb

Defined Under Namespace

Classes: Symlink

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base = Dir.pwd, hiiro: nil) ⇒ Paths

Returns a new instance of Paths.



10
11
12
13
# File 'lib/hiiro/paths.rb', line 10

def initialize(base=Dir.pwd, hiiro: nil)
  @base = Pathname.new base
  @hiiro = hiiro
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



8
9
10
# File 'lib/hiiro/paths.rb', line 8

def base
  @base
end

#hiiroObject (readonly)

Returns the value of attribute hiiro.



8
9
10
# File 'lib/hiiro/paths.rb', line 8

def hiiro
  @hiiro
end

Class Method Details

.unique_path(dir, base_name, ext: '.md') ⇒ Object

Returns [full_path, unique_name] — appends -2, -3, etc. if a file with the same base_name already exists in dir.



30
31
32
33
34
35
36
37
38
# File 'lib/hiiro/paths.rb', line 30

def self.unique_path(dir, base_name, ext: '.md')
  name = base_name
  counter = 1
  while File.exist?(File.join(dir, "#{name}#{ext}"))
    counter += 1
    name = "#{base_name}-#{counter}"
  end
  [File.join(dir, "#{name}#{ext}"), name]
end

Instance Method Details

#from_base(file) ⇒ Object



15
16
17
18
# File 'lib/hiiro/paths.rb', line 15

def from_base(file)
  path = Pathname.new(file)
  path.relative_path_from(base)
end


20
21
22
23
24
25
26
# File 'lib/hiiro/paths.rb', line 20

def symlinks_in(path)
  subdir = Pathname.new(path).expand_path

  subdir.find
    .select(&:symlink?)
    .map { |link| Symlink.new(link, hiiro) }
end