Class: Hank::Hankfile

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/hank/hankfile.rb

Overview

Manages the Hankfile configuration

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHankfile

Returns a new instance of Hankfile.



13
14
15
# File 'lib/hank/hankfile.rb', line 13

def initialize
  @mappings = T.let({}, T::Hash[String, String])
end

Instance Attribute Details

#mappingsObject (readonly)

Returns the value of attribute mappings.



10
11
12
# File 'lib/hank/hankfile.rb', line 10

def mappings
  @mappings
end

Class Method Details

.from_file(file_path) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hank/hankfile.rb', line 56

def self.from_file(file_path)
  content = File.read(file_path)
  data = TomlRB.parse(content)

  hankfile = Hankfile.new
  if data['mappings'].is_a?(Hash)
    data['mappings'].each do |source_path, target_path|
      hankfile.add_mapping(source_path, target_path.to_s)
    end
  end

  hankfile
end

Instance Method Details

#add_mapping(source_path, target_path) ⇒ Object



18
19
20
# File 'lib/hank/hankfile.rb', line 18

def add_mapping(source_path, target_path)
  @mappings[source_path] = target_path
end

#remove_mapping(source_path) ⇒ Object



23
24
25
# File 'lib/hank/hankfile.rb', line 23

def remove_mapping(source_path)
  @mappings.delete(source_path)
end

#saveObject



45
46
47
48
# File 'lib/hank/hankfile.rb', line 45

def save
  path = ENV['HANKFILE'] || 'Hankfile'
  File.write(path, to_s)
end

#to_sObject



51
52
53
# File 'lib/hank/hankfile.rb', line 51

def to_s
  TomlRB.dump({ 'mappings' => @mappings })
end

#update_mappings(selected_files) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hank/hankfile.rb', line 28

def update_mappings(selected_files)
  # Keep mappings for selected files, add new ones
  new_mappings = {}

  selected_files.each do |source_path|
    new_mappings[source_path] = if @mappings.key?(source_path)
                                  @mappings[source_path]
                                else
                                  PathUtils.flatten_path(source_path)
                                end
  end

  @mappings = new_mappings
  save
end