Sha256: e4a79ea870d58518b12e769a9d7d9388d113c2a851222c6ffbc096843e2f3ba2

Contents?: true

Size: 1.54 KB

Versions: 6

Compression:

Stored size: 1.54 KB

Contents

module Munge
  class System
    class ItemFactory
      def initialize(text_extensions:,
                     ignore_extensions:)
        @text_extensions = Set.new(text_extensions)
        @item_identifier = ItemIdentifier.new(remove_extensions: ignore_extensions)
      end

      def build(relpath:,
                content:,
                frontmatter: {},
                stat: nil)
        type = compute_file_type(relpath)

        id = @item_identifier.call(relpath)

        Munge::Item.new(
          relpath: relpath,
          content: content,
          frontmatter: frontmatter,
          stat: stat,
          type: type,
          id: id
        )
      end

      def parse(relpath:,
                content:,
                stat: nil)
        type = compute_file_type(relpath)

        if type == :text
          parsed = ContentParser.new(content)

          build(
            relpath: relpath,
            content: parsed.content,
            frontmatter: parsed.frontmatter,
            stat: stat
          )
        else
          build(
            relpath: relpath,
            content: content,
            frontmatter: {},
            stat: stat
          )
        end
      end

      private

      def file_extensions(filepath)
        extensions = File.basename(filepath).split(".")[1..-1]
        Set.new(extensions)
      end

      def compute_file_type(abspath)
        exts = file_extensions(abspath)

        if exts.intersect?(@text_extensions)
          :text
        else
          :binary
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
munge-0.11.1 lib/munge/system/item_factory.rb
munge-0.11.0 lib/munge/system/item_factory.rb
munge-0.10.0 lib/munge/system/item_factory.rb
munge-0.9.0 lib/munge/system/item_factory.rb
munge-0.8.0 lib/munge/system/item_factory.rb
munge-0.7.1 lib/munge/system/item_factory.rb