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

Methods included from View

#each_entry

Constructor Details

#initialize(input, options) ⇒ RawFiles

Returns a new instance of RawFiles.



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

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



67
68
69
70
71
72
73
74
# File 'lib/hx/backend/rawfiles.rb', line 67

def each_entry_path(selector)
  Find.find @entry_dir.to_s do |fs_path|
    next unless FileTest.file? fs_path
    path = Pathname.new(fs_path).relative_path_from(@entry_dir).to_s
    yield path if selector.accept_path? path
  end
  self
end

#edit_entry(path, prototype = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hx/backend/rawfiles.rb', line 45

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



76
77
78
79
80
81
82
83
84
85
# File 'lib/hx/backend/rawfiles.rb', line 76

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