Class: Epuber::Compiler::FileDatabase

Inherits:
Object
  • Object
show all
Defined in:
lib/epuber/compiler/file_database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileDatabase

Returns a new instance of FileDatabase.

Parameters:

  • path (String)


21
22
23
24
25
26
# File 'lib/epuber/compiler/file_database.rb', line 21

def initialize(path)
  @store_file_path = path
  @all_files = Psych.load_file(path, permitted_classes: [Epuber::Compiler::FileStat, Time]) || {}
rescue StandardError
  @all_files = {}
end

Instance Attribute Details

#all_filesHash<String, Epuber::Compiler::FileStat>

Returns:



13
14
15
# File 'lib/epuber/compiler/file_database.rb', line 13

def all_files
  @all_files
end

#store_file_pathString (readonly)

Returns:

  • (String)


17
18
19
# File 'lib/epuber/compiler/file_database.rb', line 17

def store_file_path
  @store_file_path
end

Instance Method Details

#add_dependency(file_path, to: nil) ⇒ Object

Parameters:

  • file_path (Array<String>, String)

    path to file that will be dependent on

  • to (String) (defaults to: nil)

    path to original file, that will has new dependency

Raises:

  • (ArgumentError)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/epuber/compiler/file_database.rb', line 77

def add_dependency(file_path, to: nil)
  raise ArgumentError, ':to is required' if to.nil?

  file_paths = Array(file_path)

  to_stat = @all_files[to]
  raise ArgumentError, ":to (#{to}) file is not in database" if to_stat.nil?

  to_stat.add_dependency!(file_paths)

  begin
    file_paths.each do |path|
      (path, load_stats: false) if @all_files[path].nil?
    end
  rescue Errno::ENOENT
    # no action, valid case where dependant file does not exist
  end
end

#changed?(file_path, transitive: true, default_value: true) ⇒ Boolean

Parameters:

  • file_path (String)

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/epuber/compiler/file_database.rb', line 30

def changed?(file_path, transitive: true, default_value: true)
  stat = @all_files[file_path]
  return default_value if stat.nil?

  result = (stat != FileStat.new(file_path))

  if transitive
    result ||= stat.dependency_paths.any? do |path|
      changed?(path, transitive: transitive, default_value: false)
    end
  end

  result
end

#cleanup(file_paths) ⇒ Object

Parameters:

  • file_paths (Array<String>)


98
99
100
101
102
103
104
105
# File 'lib/epuber/compiler/file_database.rb', line 98

def cleanup(file_paths)
  to_remove = @all_files.keys - file_paths
  to_remove.each { |key| @all_files.delete(key) }

  @all_files.each do |_, stat|
    _cleanup_stat_dependency_list(file_paths, stat)
  end
end

#file_stat_for(file_path) ⇒ FileStat

Parameters:

  • file_path (String)

Returns:



49
50
51
# File 'lib/epuber/compiler/file_database.rb', line 49

def file_stat_for(file_path)
  @all_files[file_path]
end

#save_to_file(path = store_file_path) ⇒ Object

Parameters:

  • path (String) (defaults to: store_file_path)


109
110
111
112
113
# File 'lib/epuber/compiler/file_database.rb', line 109

def save_to_file(path = store_file_path)
  FileUtils.mkdir_p(File.dirname(path))

  File.write(path, @all_files.to_yaml)
end

#up_to_date?(file_path, transitive: true) ⇒ Boolean

Parameters:

  • file_path (String)

Returns:

  • (Boolean)


55
56
57
# File 'lib/epuber/compiler/file_database.rb', line 55

def up_to_date?(file_path, transitive: true)
  !changed?(file_path, transitive: transitive)
end

#update_all_metadataObject



68
69
70
71
72
# File 'lib/epuber/compiler/file_database.rb', line 68

def 
  @all_files.each do |file_path, _|
    (file_path)
  end
end

#update_metadata(file_path, load_stats: true) ⇒ Object

Parameters:

  • file_path (String)


61
62
63
64
65
66
# File 'lib/epuber/compiler/file_database.rb', line 61

def (file_path, load_stats: true)
  old_stat = @all_files[file_path]
  old_dependencies = old_stat ? old_stat.dependency_paths : []

  @all_files[file_path] = FileStat.new(file_path, load_stats: load_stats, dependency_paths: old_dependencies)
end