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.



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

def initialize(app, options_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.



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

def leaves
  @leaves
end

Instance Method Details

#before_configurationObject



49
50
51
# File 'lib/middleman-core/core_extensions/collections.rb', line 49

def before_configuration
  @leaves.clear
end

#collector_value(label) ⇒ Object



81
82
83
# File 'lib/middleman-core/core_extensions/collections.rb', line 81

def collector_value(label)
  @values_by_name[label]
end

#data_collectorObject



64
65
66
# File 'lib/middleman-core/core_extensions/collections.rb', line 64

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

#live_collector(&block) ⇒ Object



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

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

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

  root
end

#manipulate_resource_list(resources) ⇒ Object



86
87
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 'lib/middleman-core/core_extensions/collections.rb', line 86

def manipulate_resource_list(resources)
  @lock.synchronize do
    @collector_roots.each do |pair|
      dataset = pair[:block].call(app, resources)
      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
    results = ctx.descriptors.reduce(resources) do |sum, d|
      d.execute_descriptor(app, sum)
    end

    StepContext.current = nil

    results
  end
end

#register_collector(label, endpoint) ⇒ Object



54
55
56
# File 'lib/middleman-core/core_extensions/collections.rb', line 54

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

#sitemap_collectorObject



59
60
61
# File 'lib/middleman-core/core_extensions/collections.rb', line 59

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