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)


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

def initialize(path)
  @store_file_path = path
  @all_files = YAML.load_file(path) || {}
rescue
  @all_files = {}
end

Instance Attribute Details

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

Returns:



15
16
17
# File 'lib/epuber/compiler/file_database.rb', line 15

def all_files
  @all_files
end

#store_file_pathString (readonly)

Returns:

  • (String)


19
20
21
# File 'lib/epuber/compiler/file_database.rb', line 19

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)


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

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)


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

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>)


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

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:



51
52
53
# File 'lib/epuber/compiler/file_database.rb', line 51

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)


111
112
113
114
115
# File 'lib/epuber/compiler/file_database.rb', line 111

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)


57
58
59
# File 'lib/epuber/compiler/file_database.rb', line 57

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

#update_all_metadataObject



70
71
72
73
74
# File 'lib/epuber/compiler/file_database.rb', line 70

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

#update_metadata(file_path, load_stats: true) ⇒ Object

Parameters:

  • file_path (String)


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

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