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)


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

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:



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

def all_files
  @all_files
end

#store_file_pathString (readonly)

Returns:

  • (String)


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

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)


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

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)


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

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


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

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

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

#file_stat_for(file_path) ⇒ FileStat

Parameters:

  • file_path (String)

Returns:



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

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)


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

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)


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

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

#update_all_metadataObject



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

def 
  @all_files.each_key do |file_path|
    (file_path)
  end
end

#update_metadata(file_path, load_stats: true) ⇒ Object

Parameters:

  • file_path (String)


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

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