Module: Hackpad::Cli::Store

Defined in:
lib/hackpad/cli/store.rb

Class Method Summary collapse

Class Method Details

.count_padsObject



66
67
68
# File 'lib/hackpad/cli/store.rb', line 66

def count_pads
  Dir.glob(File.join(@pads_dir, 'meta', '*')).count
end

.exist?(*path) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/hackpad/cli/store.rb', line 24

def exist?(*path)
  !@refresh && File.exist?(File.join(@pads_dir, *path))
end

.last_refreshObject



70
71
72
# File 'lib/hackpad/cli/store.rb', line 70

def last_refresh
  File.mtime(@list_cache) if File.exist?(@list_cache)
end

.prepare(config, workspace) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/hackpad/cli/store.rb', line 12

def prepare(config, workspace)
  @refresh = config.refresh
  @configdir = config.basedir
  @pads_dir = File.join(workspace.basedir, 'pads')
  @list_cache = File.join(@pads_dir, 'padlist')
  prepare_dirs @pads_dir
end

.prepare_dirs(base) ⇒ Object



20
21
22
# File 'lib/hackpad/cli/store.rb', line 20

def prepare_dirs(base)
  (Hackpad::Cli::FORMATS + ['meta']).each { |f| FileUtils.mkdir_p File.join(base, f) }
end

.read(id, ext) ⇒ Object



48
49
50
51
# File 'lib/hackpad/cli/store.rb', line 48

def read(id, ext)
  file = File.join(@pads_dir, ext, id)
  File.read(file)
end

.read_listObject



58
59
60
61
62
63
64
# File 'lib/hackpad/cli/store.rb', line 58

def read_list
  File.read(@list_cache).lines.reduce([]) do |a, line|
    /(?<id>[a-zA-Z0-9]*) (\[(?<cached_at>[-a-zA-Z0-9: ]*)\] )?(?<title>.*)/ =~ line
    a << OpenStruct.new(id: id, title: title, cached_at: cached_at)
    a
  end
end

.read_options(id) ⇒ Object



53
54
55
56
# File 'lib/hackpad/cli/store.rb', line 53

def read_options(id)
  file = File.join(@pads_dir, 'meta', id)
  JSON.parse File.read(file)
end

.save(pad, ext) ⇒ Object



28
29
30
31
32
# File 'lib/hackpad/cli/store.rb', line 28

def save(pad, ext)
  File.open(File.join(@pads_dir, ext, pad.id), 'w') do |f|
    f.puts pad.content
  end
end

.save_list(pads) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/hackpad/cli/store.rb', line 40

def save_list(pads)
  File.open(@list_cache, 'w') do |f|
    pads.each do |p|
      f.puts "#{p.id} [#{p.cached_at}] #{p.title}"
    end
  end
end

.save_options(id, options) ⇒ Object



34
35
36
37
38
# File 'lib/hackpad/cli/store.rb', line 34

def save_options(id, options)
  File.open(File.join(@pads_dir, 'meta', id), 'w') do |f|
    f.puts JSON.pretty_generate(options)
  end
end