Class: Pagemaster::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/pagemaster/site.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, opts, config = nil) ⇒ Site

Returns a new instance of Site.

Raises:



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pagemaster/site.rb', line 11

def initialize(args, opts, config = nil)
  @args             = args
  @opts             = opts
  @config           = config || config_from_file
  @collections      = parse_collections
  @collections_dir  = @config.dig 'collections_dir'
  @source_dir       = @config.dig 'source_dir'

  raise Error::MissingArgs, 'You must specify one or more collections after `jekyll pagemaster`' if @args.empty?
  raise Error::InvalidCollection, "Cannot find collection(s) #{@args} in config" if @collections.empty?
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



7
8
9
# File 'lib/pagemaster/site.rb', line 7

def args
  @args
end

#collectionsObject (readonly)

Returns the value of attribute collections.



7
8
9
# File 'lib/pagemaster/site.rb', line 7

def collections
  @collections
end

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/pagemaster/site.rb', line 7

def config
  @config
end

#optsObject (readonly)

Returns the value of attribute opts.



7
8
9
# File 'lib/pagemaster/site.rb', line 7

def opts
  @opts
end

Instance Method Details

#config_from_fileObject



25
26
27
# File 'lib/pagemaster/site.rb', line 25

def config_from_file
  YAML.load_file "#{`pwd`.strip}/_config.yml"
end

#generate_pagesObject



45
46
47
48
49
50
51
52
# File 'lib/pagemaster/site.rb', line 45

def generate_pages
  paths = @collections.map do |c|
    c.generate_pages @opts, @collections_dir, @source_dir
  end.flatten
  puts Rainbow('Done ✔').green

  paths
end

#parse_collectionsObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pagemaster/site.rb', line 31

def parse_collections
  collections_config = @config.dig 'collections'

  raise Error::InvalidConfig, "Cannot find 'collections' key in _config.yml" if collections_config.nil?

  args.map do |a|
    raise Error::InvalidArgument, "Cannot find requested collection #{a} in _config.yml" unless collections_config.key? a

    Collection.new(a, collections_config.fetch(a))
  end
end