Class: Middleman::CoreExtensions::Collections::CollectionsExtension

Inherits:
Extension
  • Object
show all
Defined in:
lib/middleman-core/core_extensions/collections.rb

Constant Summary

Constants included from Contracts

Contracts::PATH_MATCHER

Instance Attribute Summary collapse

Attributes inherited from Extension

#app, #options

Instance Method Summary collapse

Methods inherited from Extension

activated_extension, #add_exposed_to_context, #after_build, #after_configuration, #after_extension_activated, after_extension_activated, #before_build, clear_after_extension_callbacks, config, define_setting, expose_to_application, expose_to_config, expose_to_template, global_config, helpers, option, #ready, resources

Methods included from Contracts

#Contract

Constructor Details

#initialize(app, options_hash = {}, &block) ⇒ CollectionsExtension

Returns a new instance of CollectionsExtension.



36
37
38
39
40
41
42
43
44
# File 'lib/middleman-core/core_extensions/collections.rb', line 36

def initialize(app, options_hash={}, &block)
  super

  @leaves = Set.new
  @collectors_by_name = {}
  @values_by_name = {}

  @collector_roots = []
end

Instance Attribute Details

#leavesObject

Returns the value of attribute leaves.



19
20
21
# File 'lib/middleman-core/core_extensions/collections.rb', line 19

def leaves
  @leaves
end

Instance Method Details

#before_configurationObject



46
47
48
# File 'lib/middleman-core/core_extensions/collections.rb', line 46

def before_configuration
  @leaves.clear
end

#collector_value(label) ⇒ Object



78
79
80
# File 'lib/middleman-core/core_extensions/collections.rb', line 78

def collector_value(label)
  @values_by_name[label]
end

#data_collectorObject



61
62
63
# File 'lib/middleman-core/core_extensions/collections.rb', line 61

def data_collector
  live_collector { |app, _| app.data }
end

#live_collector(&block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/middleman-core/core_extensions/collections.rb', line 66

def live_collector(&block)
  root = LazyCollectorRoot.new(self)

  @collector_roots << {
    root: root,
    block: block
  }

  root
end

#manipulate_resource_list(resources) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/middleman-core/core_extensions/collections.rb', line 83

def manipulate_resource_list(resources)
  @collector_roots.each do |pair|
    dataset = pair[:block].call(app, resources)
    pair[:root].realize!(dataset)
  end

  ctx = StepContext.new
  leaves = @leaves.dup

  @collectors_by_name.each do |k, v|
    @values_by_name[k] = v.value(ctx)
    leaves.delete v
  end

  # Execute code paths
  leaves.each do |v|
    v.value(ctx)
  end

  # Inject descriptors
  ctx.descriptors.reduce(resources) do |sum, d|
    d.execute_descriptor(app, sum)
  end
end

#register_collector(label, endpoint) ⇒ Object



51
52
53
# File 'lib/middleman-core/core_extensions/collections.rb', line 51

def register_collector(label, endpoint)
  @collectors_by_name[label] = endpoint
end

#sitemap_collectorObject



56
57
58
# File 'lib/middleman-core/core_extensions/collections.rb', line 56

def sitemap_collector
  live_collector { |_, resources| resources }
end