Class: AocCli::Day::Pages

Inherits:
Init
  • Object
show all
Defined in:
lib/aoc_cli/day.rb

Direct Known Subclasses

Cache

Instance Attribute Summary collapse

Attributes inherited from Init

#day, #paths, #user, #year

Instance Method Summary collapse

Methods inherited from Init

#meta, #mkdir

Constructor Details

#initialize(u: Metafile.get(:user), y: Metafile.get(:year), d: Metafile.get(:day), f: [:Input, :Puzzle], use_cache: true) ⇒ Pages

Returns a new instance of Pages.



25
26
27
28
29
30
31
32
33
# File 'lib/aoc_cli/day.rb', line 25

def initialize(u:Metafile.get(:user), 
			   y:Metafile.get(:year),
			   d:Metafile.get(:day), 
			   f:[:Input, :Puzzle],
			   use_cache:true)
	super(u:u, y:y, d:d)
	@files = f
	@use_cache = use_cache
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



24
25
26
# File 'lib/aoc_cli/day.rb', line 24

def files
  @files
end

#use_cacheObject (readonly)

Returns the value of attribute use_cache.



24
25
26
# File 'lib/aoc_cli/day.rb', line 24

def use_cache
  @use_cache
end

Instance Method Details

#cacheObject



38
39
40
# File 'lib/aoc_cli/day.rb', line 38

def cache
	@cache ||= Cache.new(d:day, f:files).query
end

#copy(file:) ⇒ Object



41
42
43
# File 'lib/aoc_cli/day.rb', line 41

def copy(file:)
	File.write(paths.local(f:file), cache[file])
end

#download(page:) ⇒ Object



44
45
46
47
48
49
# File 'lib/aoc_cli/day.rb', line 44

def download(page:)
	req = Requests.const_get(page)
		.new(u:user, y:year, d:day)
		.write(to:paths.cache_and_local(f:page))
	req.init_stats if page == :Puzzle
end

#loadObject



34
35
36
37
# File 'lib/aoc_cli/day.rb', line 34

def load
	files.each do |file| use_cache && cache[file] ?
		copy(file:file) : download(page:file) end
end