Sha256: 93211b3bedcb848df83d2c0bcbed4cff4abfdce20281cf534c280d89b30d2d8f
Contents?: true
Size: 1.88 KB
Versions: 1
Compression:
Stored size: 1.88 KB
Contents
module Bunto class DataReader attr_reader :site, :content def initialize(site) @site = site @content = {} @entry_filter = EntryFilter.new(site) end # Read all the files in <source>/<dir>/_drafts and create a new Draft # object with each one. # # dir - The String relative path of the directory to read. # # Returns nothing. def read(dir) base = site.in_source_dir(dir) read_data_to(base, @content) @content end # Read and parse all yaml files under <dir> and add them to the # <data> variable. # # dir - The string absolute path of the directory to read. # data - The variable to which data will be added. # # Returns nothing def read_data_to(dir, data) return unless File.directory?(dir) && !@entry_filter.symlink?(dir) entries = Dir.chdir(dir) do Dir["*.{yaml,yml,json,csv}"] + Dir["*"].select { |fn| File.directory?(fn) } end entries.each do |entry| path = @site.in_source_dir(dir, entry) next if @entry_filter.symlink?(path) key = sanitize_filename(File.basename(entry, ".*")) if File.directory?(path) read_data_to(path, data[key] = {}) else data[key] = read_data_file(path) end end end # Determines how to read a data file. # # Returns the contents of the data file. def read_data_file(path) case File.extname(path).downcase when ".csv" CSV.read(path, { :headers => true, :encoding => site.config["encoding"] }).map(&:to_hash) else SafeYAML.load_file(path) end end def sanitize_filename(name) name.gsub!(%r![^\w\s-]+!, "") name.gsub!(%r!(^|\b\s)\s+($|\s?\b)!, '\\1\\2') name.gsub(%r!\s+!, "_") end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
bunto-3.2.1 | lib/bunto/readers/data_reader.rb |