Module: FSDB::Formats

Included in:
FSDB, Database
Defined in:
lib/fsdb/formats.rb

Constant Summary collapse

HIDDEN_FILE_PAT =

Files of the form ‘..*’, as well as ‘.’, are excluded from dir lists.

/^\.(?:$|\.)/
DIR_PAT =
/\/$/
DIR_LOAD_FROM_PATH =
proc do |path|
  begin
    files = Dir.entries(path).reject { |e| HIDDEN_FILE_PAT =~ e }
  rescue Errno::ENOENT
    []
  else
    Thread.exclusive do # Dir.chdir is not threadsafe
      Dir.chdir path do
        files.each do |entry|
          if not DIR_PAT =~ entry and File.directory?(entry)
            entry << ?/
          end
        end
      end
    end
  end
end
DIR_LOAD =
proc do |f|
  path = f.path
  DIR_LOAD_FROM_PATH[path]
end
DIR_FORMAT =
Format.new(DIR_PAT, :name => "DIR_FORMAT", :load => DIR_LOAD)
TEXT_FORMAT =
Format.new(
  /\.txt$/i, /\.text$/i,
  :name => "TEXT_FORMAT",
  :load => proc {|f| f.read},
  :dump => proc {|string, f| f.syswrite(string)}
)
BINARY_FORMAT =

Use this for image files, native executables, etc.

Format.new(
  /\.jpg$/i, /\.so$/i, /\.dll$/i, /\.exe$/i, /\.bin$/i,
    # add more with the Format#when construct
  :binary,
  :name => "BINARY_FORMAT",
  :load => proc {|f| f.read},
  :dump => proc {|string, f| f.syswrite(string)}
)
MARSHAL_FORMAT =
Format.new(
  //, :binary,
  :name => "MARSHAL_FORMAT",
  :load => marshal_load,
  :dump => marshal_dump
)
YAML_FORMAT =

Actually, Marshal does binmode=true automatically.

Format.new(
  /\.yml$/, /\.yaml$/,
  :name => "YAML_FORMAT",
  :load => proc {|f| YAML.load(f)},
  :dump => proc {|object, f| f.syswrite(YAML.dump(object))}
)