Module: Hackpad::Cli::Store
- Defined in:
- lib/hackpad/cli/store.rb
Class Method Summary collapse
- .count_pads ⇒ Object
- .exists?(*path) ⇒ Boolean
- .last_refresh ⇒ Object
- .prepare(config) ⇒ Object
- .prepare_dirs(base) ⇒ Object
- .read(id, ext) ⇒ Object
- .read_list ⇒ Object
- .read_options(id) ⇒ Object
- .save(pad, ext) ⇒ Object
- .save_list(pads) ⇒ Object
- .save_options(id, options) ⇒ Object
Class Method Details
.count_pads ⇒ Object
64 65 66 |
# File 'lib/hackpad/cli/store.rb', line 64 def count_pads Dir.glob(File.join(@pads_dir, 'meta', '*')).count end |
.exists?(*path) ⇒ Boolean
22 23 24 |
# File 'lib/hackpad/cli/store.rb', line 22 def exists?(*path) !@refresh && File.exist?(File.join(@pads_dir, *path)) end |
.last_refresh ⇒ Object
68 69 70 |
# File 'lib/hackpad/cli/store.rb', line 68 def last_refresh File.mtime(@list_cache) if File.exist?(@list_cache) end |
.prepare(config) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/hackpad/cli/store.rb', line 10 def prepare(config) @refresh = config[:refresh] dir = File.join(config[:configdir], config[:workspace]) @pads_dir = File.join(dir, 'pads') @list_cache = File.join(@pads_dir, 'padlist') prepare_dirs @pads_dir end |
.prepare_dirs(base) ⇒ Object
18 19 20 |
# File 'lib/hackpad/cli/store.rb', line 18 def prepare_dirs(base) (Hackpad::Cli::FORMATS + ['meta']).each { |f| FileUtils.mkdir_p File.join(base, f) } end |
.read(id, ext) ⇒ Object
46 47 48 49 |
# File 'lib/hackpad/cli/store.rb', line 46 def read(id, ext) file = File.join(@pads_dir, ext, id) File.read(file) end |
.read_list ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/hackpad/cli/store.rb', line 56 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
51 52 53 54 |
# File 'lib/hackpad/cli/store.rb', line 51 def (id) file = File.join(@pads_dir, 'meta', id) JSON.parse File.read(file) end |
.save(pad, ext) ⇒ Object
26 27 28 29 30 |
# File 'lib/hackpad/cli/store.rb', line 26 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
38 39 40 41 42 43 44 |
# File 'lib/hackpad/cli/store.rb', line 38 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
32 33 34 35 36 |
# File 'lib/hackpad/cli/store.rb', line 32 def (id, ) File.open(File.join(@pads_dir, 'meta', id), 'w') do |f| f.puts JSON.pretty_generate() end end |