class Examplify

Attributes

files[R]

Public Class Methods

new(paths) click to toggle source
# File lib/examplify.rb, line 6
def initialize(paths)
  @files = Rake::FileList[get_list_of_files(paths)]
end

Public Instance Methods

exclude(glob) click to toggle source
# File lib/examplify.rb, line 10
def exclude(glob)
  files.exclude do |path|
    glob_matches_file(glob, path)
  end
end
filter(glob) click to toggle source

inverse of exclude

# File lib/examplify.rb, line 17
def filter(glob)
  files.exclude do |path|
    !glob_matches_file(glob, path)
  end
end
output() click to toggle source
# File lib/examplify.rb, line 23
def output
  files.map { |path|
    title   = ["# ", path].join
    content = File.read(path)

    [title, content].join("\n")
  }.join("\n")
end

Private Instance Methods

get_all_files_inside(folder) click to toggle source
# File lib/examplify.rb, line 55
def get_all_files_inside(folder)
  # get paths of files recursively, ignoring paths to folders
  Dir.glob(File.join(folder, '**/*')).reject { |path| File.directory? path }
end
get_list_of_files(paths) click to toggle source

given a path or a list of paths to folders or files returns an array of paths to all files given, and all files contained in subdirectories of folders, but without the folders

# File lib/examplify.rb, line 43
def get_list_of_files(paths)
  paths = Array(paths)

  paths.flat_map do |path|
    if File.directory? path
      get_all_files_inside(path)
    else
      [path]
    end
  end
end
glob_matches_file(glob, path) click to toggle source
# File lib/examplify.rb, line 34
def glob_matches_file(glob, path)
  filename = path.pathmap("%f")
  File.fnmatch(glob, filename, File::FNM_DOTMATCH)
end