Class: Hx::Backend::RawFiles

Inherits:
Object
  • Object
show all
Includes:
Filter
Defined in:
lib/hx/backend/rawfiles.rb

Instance Method Summary collapse

Methods included from Filter

#each_entry

Constructor Details

#initialize(input, options) ⇒ RawFiles

Returns a new instance of RawFiles.



33
34
35
36
37
38
39
# File 'lib/hx/backend/rawfiles.rb', line 33

def initialize(input, options)
  @entry_dir = Hx.get_pathname(options, :entry_dir)
  @postedit_cmd = options[:postedit_cmd]
  @postdelete_cmd = options[:postdelete_cmd]
  @postedit_cmd = @postedit_cmd.split(/\s+/) if @postedit_cmd
  @postdelete_cmd = @postdelete_cmd.split(/\s+/) if @postdelete_cmd
end

Instance Method Details

#each_entry_path(selector) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/hx/backend/rawfiles.rb', line 71

def each_entry_path(selector)
  Pathname.glob(@entry_dir + '**/*') do |pathname|
    next unless pathname.file?
    path = pathname_to_path(pathname)
    yield path if selector.accept_path? path
  end
  self
end

#edit_entry(path, prototype = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hx/backend/rawfiles.rb', line 49

def edit_entry(path, prototype=nil)
  pathname = path_to_pathname(path)
  begin
    text = pathname.read
  rescue Errno::ENOENT
    raise NoSuchEntryError, path unless prototype
    text = prototype['content'].to_s
  end
  text = yield text
  if text
    Hx.write_file(pathname, text)
    system *(@postedit_cmd + [pathname]) if @postedit_cmd
  else
    begin
      File.unlink(pathname)
      system *(@postdelete_cmd + [pathname]) if @postdelete_cmd
    rescue Errno::ENOENT
    end
  end
  self
end

#get_entry(path) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/hx/backend/rawfiles.rb', line 80

def get_entry(path)
  pathname = path_to_pathname(path)
  begin
    { 'updated' => pathname.mtime,
      'executable' => pathname.executable?,
      'content' => Hx::LazyContent.new { pathname.read } }
  rescue Errno::ENOENT
    raise Hx::NoSuchEntryError, path
  end
end