Class: Middleman::ConfigExtension

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

Constant Summary

Constants included from Contracts

Contracts::PATH_MATCHER

Instance Attribute Summary

Attributes inherited from Extension

#app, #options

Instance Method Summary collapse

Methods inherited from Extension

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

Methods included from Contracts

#Contract

Constructor Details

#initialize(app, config = {}, &block) ⇒ ConfigExtension

Returns a new instance of ConfigExtension.



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/middleman-core/extension.rb', line 494

def initialize(app, config={}, &block)
  @descriptors = {}
  @ready = false

  self.class.exposed_to_config.each do |k, v|
    @descriptors[k] = []

    define_singleton_method(:"__original_#{v}", &method(v))
    define_singleton_method(v) do |*args, &b|
      proxy_method_call(k, v, args, &b)
    end
  end

  super
end

Instance Method Details

#after_configurationObject



510
511
512
513
514
515
516
517
518
519
# File 'lib/middleman-core/extension.rb', line 510

def after_configuration
  context = self

  self.class.exposed_to_config.each do |k, v|
    ::Middleman::CoreExtensions::Collections::StepContext.add_to_context(k) do |*args, &b|
      r = context.method(:"__original_#{v}").call(*args, &b)
      descriptors << r if r.respond_to?(:execute_descriptor)
    end
  end
end

#manipulate_resource_list(resources) ⇒ Object



532
533
534
535
536
# File 'lib/middleman-core/extension.rb', line 532

def manipulate_resource_list(resources)
  @descriptors.values.flatten.reduce(resources) do |sum, c|
    c.execute_descriptor(app, sum)
  end
end

#proxy_method_call(k, v, args, &b) ⇒ Object



539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/middleman-core/extension.rb', line 539

def proxy_method_call(k, v, args, &b)
  if @ready
    ctx = ::Middleman::CoreExtensions::Collections::StepContext.current
    r = method(:"__original_#{v}").call(*args, &b)

    if r.respond_to?(:execute_descriptor)
      if ctx
        ctx.descriptors << r
      else
        @descriptors[k] << r
        @app.sitemap.rebuild_resource_list!(:"first_run_change_#{v}")
      end
    end
  else
    @descriptors[k] << method(:"__original_#{v}").call(*args, &b)
    @app.sitemap.rebuild_resource_list!(:"first_run_change_#{v}")
  end
end

#readyObject



521
522
523
524
525
526
527
# File 'lib/middleman-core/extension.rb', line 521

def ready
  @ready = true

  # @descriptors.each do |k, v|
  #   @descriptors[k] = []
  # end
end