Class: WaxTasks::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/wax_tasks/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Config

Returns a new instance of Config.



8
9
10
11
# File 'lib/wax_tasks/config.rb', line 8

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

Instance Attribute Details

#collectionsObject (readonly)

Returns the value of attribute collections.



6
7
8
# File 'lib/wax_tasks/config.rb', line 6

def collections
  @collections
end

Instance Method Details

#collections_dirObject



25
26
27
# File 'lib/wax_tasks/config.rb', line 25

def collections_dir
  @config.dig 'collections_dir'
end

#extString

Contructs permalink extension from site ‘permalink` variable

Returns:

  • (String)

    the end of the permalink, either ‘/’ or ‘.html’



33
34
35
36
37
38
39
40
# File 'lib/wax_tasks/config.rb', line 33

def ext
  case @config.dig 'permalink'
  when 'pretty' || '/'
    '/'
  else
    '.html'
  end
end

#find_collection(name) ⇒ Object



72
73
74
75
76
77
# File 'lib/wax_tasks/config.rb', line 72

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

#process_collectionsObject



44
45
46
47
48
49
50
51
52
# File 'lib/wax_tasks/config.rb', line 44

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

#search(name) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/wax_tasks/config.rb', line 56

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

#selfObject



13
14
15
# File 'lib/wax_tasks/config.rb', line 13

def self
  @config
end

#sourceObject



19
20
21
# File 'lib/wax_tasks/config.rb', line 19

def source
  @config.dig 'source'
end