class Dotfiler::Dotfiles

Constants

DOTFILES_PLACEHOLDER
HOME_PLACEHOLDER

Public Class Methods

new(args) click to toggle source
Calls superclass method
# File lib/dotfiler/dotfiles.rb, line 17
def initialize(args)
  super(args)
  load_data!
end

Public Instance Methods

add!(*args) click to toggle source
# File lib/dotfiler/dotfiles.rb, line 39
def add!(*args)
  dotfile = Dotfile.new(*args)

  File.open(config.dotfiles_file_path.to_s, "a") do |file|
    file << dotfile_to_line(dotfile)
  end

  reload!
end
each(&block) click to toggle source
# File lib/dotfiler/dotfiles.rb, line 26
def each(&block)
  @dotfiles.each(&block)
end
exists?(name)
Alias for: name_taken?
find(name) click to toggle source
# File lib/dotfiler/dotfiles.rb, line 22
def find(name)
  @dotfiles.find { |dotfile| dotfile.name == name }
end
list() click to toggle source
# File lib/dotfiler/dotfiles.rb, line 61
def list
  @dotfiles.map(&:to_h)
end
load_data!() click to toggle source
# File lib/dotfiler/dotfiles.rb, line 65
def load_data!
  @dotfiles = if config.set?
                parse(File.readlines(config.dotfiles_file_path.to_s))
              else
                {}
              end
end
Also aliased as: reload!
name_taken?(name) click to toggle source
# File lib/dotfiler/dotfiles.rb, line 34
def name_taken?(name)
  names.include?(name)
end
Also aliased as: exists?
names() click to toggle source
# File lib/dotfiler/dotfiles.rb, line 30
def names
  @dotfiles.map(&:name)
end
reload!()
Alias for: load_data!
remove!(name) click to toggle source
# File lib/dotfiler/dotfiles.rb, line 49
def remove!(name)
  content = @dotfiles.each_with_object("") do |dotfile, result|
    next if dotfile.name == name

    result << dotfile_to_line(dotfile)
  end

  File.open(config.dotfiles_file_path.to_s, "w+") { |file| file << content }

  reload!
end

Private Instance Methods

dotfile_to_line(dotfile) click to toggle source
# File lib/dotfiler/dotfiles.rb, line 92
def dotfile_to_line(dotfile)
  name, link, path = dotfile.to_h.values_at(:name, :link, :path)

  link = link.sub(home_path.to_s, HOME_PLACEHOLDER)
  path = path.sub(config[:dotfiles], DOTFILES_PLACEHOLDER)

  [name, link, path].join(" :: ") + "\n"
end
parse(lines) click to toggle source
# File lib/dotfiler/dotfiles.rb, line 77
def parse(lines)
  lines.sort.each_with_object([]) do |line, result|
    sanitized_line = line.gsub("\n", "")

    next if sanitized_line.empty?

    name, link, path = sanitized_line.split(" :: ")

    link = link.sub(HOME_PLACEHOLDER, home_path.to_s)
    path = path.sub(DOTFILES_PLACEHOLDER, config[:dotfiles])

    result << Dotfile.new(name: name, link: link, path: path)
  end
end