Sha256: 71f9c316b0f6832e8dde86f1b59f6432d98083be3bcd975182ea4c0cde329dc6

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

module Flutterby
  class Folder < Entity
    attr_reader :children

    def list(indent: 0)
      super
      children.each { |c| c.list(indent: indent + 1) }
    end

    def read
      @children = Dir[::File.join(fs_path, "*")].map do |entry|
        Flutterby.from(entry, parent: self)
      end.compact
    end

    def write_static(path)
      Dir.mkdir(path) unless ::File.exists?(path)

      @children.each do |child|
        child.export(path)
      end
    end

    def find(path)
      (name, slash, rest) = path.partition("/")

      name = name.split('.').first
      child = @children.find { |c| c.name == name }
      rest.empty? ? child : child.find(rest)
    end

    # Returns all children that will compile to a HTML page.
    #
    def pages
      children.select { |c| c.ext == "html" }
    end



    def serve(parts, req, res)
      # If no further parts are requested, let's look for an index
      # document and serve that instead.
      if child = find(parts.empty? ? "index" : parts.shift)
        child.serve(parts, req, res)
      else
        res.headers["Content-Type"] = "text/html"
        res.body = ["404"]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flutterby-0.0.4 lib/flutterby/folder.rb