Class: Nanoc::Core::ItemRepRouter Private

Inherits:
Object
  • Object
show all
Includes:
ContractsSupport
Defined in:
lib/nanoc/core/item_rep_router.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Assigns paths to reps.

Defined Under Namespace

Classes: IdenticalRoutesError, RouteWithoutSlashError

Instance Method Summary collapse

Methods included from ContractsSupport

enabled?, included, setup_once, warn_about_performance

Constructor Details

#initialize(reps, action_provider, site) ⇒ ItemRepRouter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ItemRepRouter.



24
25
26
27
28
# File 'lib/nanoc/core/item_rep_router.rb', line 24

def initialize(reps, action_provider, site)
  @reps = reps
  @action_provider = action_provider
  @site = site
end

Instance Method Details

#route_rep(rep, paths, snapshot_names, assigned_paths) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/nanoc/core/item_rep_router.rb', line 56

def route_rep(rep, paths, snapshot_names, assigned_paths)
  # Encode
  paths = paths.map { |path| path.encode('UTF-8') }

  # Validate format
  paths.each do |path|
    unless path.start_with?('/')
      raise RouteWithoutSlashError.new(path, rep)
    end
  end

  # Validate uniqueness
  paths.each do |path|
    if assigned_paths.include?(path)
      # TODO: Include snapshot names in error message
      reps = [assigned_paths[path], rep].sort_by { |r| [r.item.identifier, r.name] }
      raise IdenticalRoutesError.new(path, *reps)
    end
  end
  paths.each do |path|
    assigned_paths[path] = rep
  end

  # Assign
  snapshot_names.each do |snapshot_name|
    rep.raw_paths[snapshot_name] = paths.map { |path| @site.config.output_dir + path }
    rep.paths[snapshot_name] = paths.map { |path| strip_index_filename(path) }
  end
end

#runObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/nanoc/core/item_rep_router.rb', line 30

def run
  action_sequences = {}
  assigned_paths = {}
  @reps.each do |rep|
    # Sigh. We route reps twice, because the first time, the paths might not have converged
    # yet. This isn’t ideal, but it’s the only way to work around the divergence issues that
    # I can think of. For details, see
    # https://github.com/nanoc/nanoc/pull/1085#issuecomment-280628426.

    @action_provider.action_sequence_for(rep).paths.each do |(snapshot_names, paths)|
      route_rep(rep, paths, snapshot_names, {})
    end

    seq = @action_provider.action_sequence_for(rep)
    action_sequences[rep] = seq
    seq.paths.each do |(snapshot_names, paths)|
      route_rep(rep, paths, snapshot_names, assigned_paths)
    end

    # TODO: verify that paths converge
  end

  action_sequences
end

#strip_index_filename(basic_path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



87
88
89
90
91
92
93
94
95
96
# File 'lib/nanoc/core/item_rep_router.rb', line 87

def strip_index_filename(basic_path)
  @site.config[:index_filenames].each do |index_filename|
    slashed_index_filename = '/' + index_filename
    if basic_path.end_with?(slashed_index_filename)
      return basic_path[0..-index_filename.length - 1]
    end
  end

  basic_path
end