Sha256: c6fb7546db5209b6d56eb60c619c2a69306eef6959e9ee3e20c2408780111ebf

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module WaxTasks
  #
  class Config
    attr_reader :collections

    def initialize(config)
      @config           = config
      @collections      = process_collections
    end

    #
    #
    def source
      @config.dig 'source'
    end

    #
    #
    def collections_dir
      @config.dig 'collections_dir'
    end

    #
    # Contructs permalink extension from site `permalink` variable
    #
    # @return [String] the end of the permalink, either '/' or '.html'
    def ext
      case @config.dig 'permalink'
      when 'pretty' || '/'
        '/'
      else
        '.html'
      end
    end

    #
    #
    def process_collections
      if @config.key? 'collections'
        @config['collections'].map do |k, v|
          WaxTasks::Collection.new(k, v, source, collections_dir, ext)
        end
      else
        []
      end
    end

    #
    #
    def search(name)
      search_config = @config.dig 'search', name
      raise WaxTasks::Error::InvalidConfig if search_config.nil?
      raise WaxTasks::Error::InvalidConfig unless search_config.dig('collections').is_a? Hash

      search_config['collections'] = search_config['collections'].map do |k, v|
        fields = v.fetch('fields', [])
        fields << 'content' if v.fetch('content', false)
        find_collection(k).tap { |c| c.search_fields = fields }
      end

      search_config
    end

    #
    #
    def find_collection(name)
      collection = @collections.find { |c| c.name == name }
      raise WaxTasks::Error::InvalidCollection, "Cannot find requested collection '#{name}'" if collection.nil?

      collection
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wax_tasks-1.0.2 lib/wax_tasks/config.rb
wax_tasks-1.0.1 lib/wax_tasks/config.rb
wax_tasks-1.0.0 lib/wax_tasks/config.rb