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

Inherits:
Extension
  • Object
show all
Defined in:
middleman-core/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, #manipulate_resource_list, option, #ready, resources

Methods included from Contracts

#Contract

Constructor Details

#initialize(app, options_hash = ::Middleman::EMPTY_HASH, &block) ⇒ CollectionsExtension

Returns a new instance of CollectionsExtension.


39
40
41
42
43
44
45
46
47
48
49
# File 'middleman-core/lib/middleman-core/core_extensions/collections.rb', line 39

def initialize(app, options_hash = ::Middleman::EMPTY_HASH, &block)
  super

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

  @collector_roots = []

  @lock = Monitor.new
end

Instance Attribute Details

#leavesObject

Returns the value of attribute leaves


22
23
24
# File 'middleman-core/lib/middleman-core/core_extensions/collections.rb', line 22

def leaves
  @leaves
end

Instance Method Details

#before_configurationObject


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

def before_configuration
  @leaves.clear
end

#collector_value(label) ⇒ Object


83
84
85
# File 'middleman-core/lib/middleman-core/core_extensions/collections.rb', line 83

def collector_value(label)
  @values_by_name[label]
end

#data_collectorObject


66
67
68
# File 'middleman-core/lib/middleman-core/core_extensions/collections.rb', line 66

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

#live_collector(&block) ⇒ Object


71
72
73
74
75
76
77
78
79
80
# File 'middleman-core/lib/middleman-core/core_extensions/collections.rb', line 71

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

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

  root
end

#manipulate_resource_list_container!(resource_list) ⇒ Object


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'middleman-core/lib/middleman-core/core_extensions/collections.rb', line 88

def manipulate_resource_list_container!(resource_list)
  @lock.synchronize do
    @collector_roots.each do |pair|
      dataset = pair[:block].call(app, resource_list)
      pair[:root].realize!(dataset)
    end

    ctx = StepContext.new(app)
    StepContext.current = ctx

    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.each do |d|
      d.execute_descriptor(app, resource_list)
    end

    StepContext.current = nil
  end
end

#register_collector(label, endpoint) ⇒ Object


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

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

#sitemap_collectorObject


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

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