More docs. Use ActiveSupport delegate to simplify code in places

This commit is contained in:
Thomas Reynolds 2011-12-21 21:52:38 -08:00
parent dba2cbd0ee
commit 4d2e0d8a7d
6 changed files with 76 additions and 48 deletions

View file

@ -304,14 +304,7 @@ class Middleman::Base
def self.cache def self.cache
@_cache ||= ::Middleman::Cache.new @_cache ||= ::Middleman::Cache.new
end end
delegate :cache, :to => :"self.class"
# Cache accessor for instance, simply forwards to class
#
# @private
# @return [Middleman::Cache] The cache
def cache
self.class.cache
end
# Rack env # Rack env
attr :env attr :env
@ -465,22 +458,7 @@ protected
@res.finish @res.finish
end end
# Set helpers at the class level delegate :helpers, :use, :map, :to => :"self.class"
def helpers(*extensions, &block)
self.class.helpers(*extensions, &block)
end
# Set middleware at the class level
# @return [void]
def use(middleware, *args, &block)
self.class.use(middleware, *args, &block)
end
# Set mapped rack app at the class level
# @return [void]
def map(map, &block)
self.class.map(map, &block)
end
# Immediately send static file # Immediately send static file
# #

View file

@ -8,6 +8,7 @@ module Middleman::CoreExtensions::Builder
app.define_hook :after_build app.define_hook :after_build
app.extend ClassMethods app.extend ClassMethods
app.send :include, InstanceMethods app.send :include, InstanceMethods
app.delegate :build_reroute, :to => :"self.class"
end end
alias :included :registered alias :included :registered
end end
@ -26,13 +27,6 @@ module Middleman::CoreExtensions::Builder
# Build Instance Methods # Build Instance Methods
module InstanceMethods module InstanceMethods
# Forward to class method
#
# @return [Array<Proc>]
def build_reroute(&block)
self.class.build_reroute(&block)
end
# Run through callbacks and get the new values # Run through callbacks and get the new values
# #
# @param [String] destination The current destination of the built file # @param [String] destination The current destination of the built file

View file

@ -9,8 +9,12 @@ require 'active_support/inflector' # humanize
FileSet.glob_require('../vendor/padrino-helpers-0.10.5/lib/padrino-helpers/**/*.rb', __FILE__) FileSet.glob_require('../vendor/padrino-helpers-0.10.5/lib/padrino-helpers/**/*.rb', __FILE__)
# Built-in helpers
module Middleman::CoreExtensions::DefaultHelpers module Middleman::CoreExtensions::DefaultHelpers
# Extension registered
class << self class << self
# @private
def registered(app) def registered(app)
app.helpers ::Padrino::Helpers::OutputHelpers app.helpers ::Padrino::Helpers::OutputHelpers
app.helpers ::Padrino::Helpers::TagHelpers app.helpers ::Padrino::Helpers::TagHelpers
@ -30,19 +34,34 @@ module Middleman::CoreExtensions::DefaultHelpers
alias :included :registered alias :included :registered
end end
# The helpers
module Helpers module Helpers
# Output a stylesheet link tag based on the current path
#
# @param [String] separator How to break up path in parts
# @return [String]
def auto_stylesheet_link_tag(separator="/") def auto_stylesheet_link_tag(separator="/")
auto_tag(:css, separator) do |path| auto_tag(:css, separator) do |path|
stylesheet_link_tag path stylesheet_link_tag path
end end
end end
# Output a javascript tag based on the current path
#
# @param [String] separator How to break up path in parts
# @return [String]
def auto_javascript_include_tag(separator="/") def auto_javascript_include_tag(separator="/")
auto_tag(:js, separator) do |path| auto_tag(:js, separator) do |path|
javascript_include_tag path javascript_include_tag path
end end
end end
# Output a stylesheet link tag based on the current path
#
# @param [Symbol] asset_ext The type of asset
# @param [String] separator How to break up path in parts
# @param [String] asset_dir Where to look for assets
# @return [void]
def auto_tag(asset_ext, separator="/", asset_dir=nil) def auto_tag(asset_ext, separator="/", asset_dir=nil)
if asset_dir.nil? if asset_dir.nil?
asset_dir = case asset_ext asset_dir = case asset_ext
@ -61,6 +80,9 @@ module Middleman::CoreExtensions::DefaultHelpers
yield path if sitemap.exists?(File.join(asset_dir, path)) yield path if sitemap.exists?(File.join(asset_dir, path))
end end
# Generate body css classes based on the current path
#
# @return [String]
def page_classes def page_classes
path = current_path.dup path = current_path.dup
path << index_file if path.match(%r{/$}) path << index_file if path.match(%r{/$})
@ -73,7 +95,11 @@ module Middleman::CoreExtensions::DefaultHelpers
classes.join(' ') classes.join(' ')
end end
# Padrino's asset handling needs to pass through ours # Get the path of a file of a given type
#
# @param [Symbol] kind The type of file
# @param [String] source The path to the file
# @return [String]
def asset_path(kind, source) def asset_path(kind, source)
return source if source =~ /^http/ return source if source =~ /^http/
asset_folder = case kind asset_folder = case kind

View file

@ -31,9 +31,12 @@
# Using for version parsing # Using for version parsing
require "rubygems" require "rubygems"
# Namespace extensions module
module Middleman::CoreExtensions::Extensions module Middleman::CoreExtensions::Extensions
# Register extension
class << self class << self
# @private
def included(app) def included(app)
# app.set :default_extensions, [] # app.set :default_extensions, []
app.define_hook :after_configuration app.define_hook :after_configuration
@ -43,18 +46,31 @@ module Middleman::CoreExtensions::Extensions
app.extend ClassMethods app.extend ClassMethods
app.send :include, InstanceMethods app.send :include, InstanceMethods
app.delegate :configure, :to => :"self.class"
end end
end end
# Class methods
module ClassMethods module ClassMethods
# Add a callback to run in a specific environment
#
# @param [String, Symbol] env The environment to run in
# @return [void]
def configure(env, &block) def configure(env, &block)
send("#{env}_config", &block) send("#{env}_config", &block)
end end
# Alias `extensions` to access registered extensions
#
# @return [Array<Module]
def extensions def extensions
@extensions ||= [] @extensions ||= []
end end
# Register a new extension
#
# @param [Array<Module>] new_extensions Extension modules to register
# @return [Array<Module]
def register(*new_extensions) def register(*new_extensions)
@extensions ||= [] @extensions ||= []
@extensions += new_extensions @extensions += new_extensions
@ -65,12 +81,16 @@ module Middleman::CoreExtensions::Extensions
end end
end end
# Instance methods
module InstanceMethods module InstanceMethods
# This method is available in the project's `config.rb`. # This method is available in the project's `config.rb`.
# It takes a underscore-separated symbol, finds the appropriate # It takes a underscore-separated symbol, finds the appropriate
# feature module and includes it. # feature module and includes it.
# #
# activate :lorem # activate :lorem
#
# @param [Symbol, Module] feature Which extension to activate
# @return [void]
def activate(feature) def activate(feature)
ext = ::Middleman::Extensions.load(feature.to_sym) ext = ::Middleman::Extensions.load(feature.to_sym)
@ -83,10 +103,6 @@ module Middleman::CoreExtensions::Extensions
self.class.register(ext) self.class.register(ext)
end end
end end
def configure(env, &block)
self.class.configure(env, &block)
end
# Load features before starting server # Load features before starting server
def initialize def initialize

View file

@ -1,11 +1,17 @@
require "find" require "find"
# API for watching file change events
module Middleman::CoreExtensions::FileWatcher module Middleman::CoreExtensions::FileWatcher
# Setup extension
class << self class << self
# @private
def registered(app) def registered(app)
app.extend ClassMethods app.extend ClassMethods
app.send :include, InstanceMethods app.send :include, InstanceMethods
app.delegate :file_changed, :file_deleted, :to => :"self.class"
# Before parsing config, load the data/ directory
app.before_configuration do app.before_configuration do
data_path = File.join(root, data_dir) data_path = File.join(root, data_dir)
Find.find(data_path) do |path| Find.find(data_path) do |path|
@ -14,6 +20,7 @@ module Middleman::CoreExtensions::FileWatcher
end if File.exists?(data_path) end if File.exists?(data_path)
end end
# After config, load everything else
app.ready do app.ready do
Find.find(root) do |path| Find.find(root) do |path|
next if File.directory?(path) next if File.directory?(path)
@ -24,13 +31,22 @@ module Middleman::CoreExtensions::FileWatcher
alias :included :registered alias :included :registered
end end
# Class methods
module ClassMethods module ClassMethods
# Add callback to be run on file change
#
# @param [nil,Regexp] matcher A Regexp to match the change path against
# @return [Array<Proc>]
def file_changed(matcher=nil, &block) def file_changed(matcher=nil, &block)
@_file_changed ||= [] @_file_changed ||= []
@_file_changed << [block, matcher] if block_given? @_file_changed << [block, matcher] if block_given?
@_file_changed @_file_changed
end end
# Add callback to be run on file deletion
#
# @param [nil,Regexp] matcher A Regexp to match the deleted path against
# @return [Array<Proc>]
def file_deleted(matcher=nil, &block) def file_deleted(matcher=nil, &block)
@_file_deleted ||= [] @_file_deleted ||= []
@_file_deleted << [block, matcher] if block_given? @_file_deleted << [block, matcher] if block_given?
@ -38,11 +54,12 @@ module Middleman::CoreExtensions::FileWatcher
end end
end end
# Instance methods
module InstanceMethods module InstanceMethods
def file_changed(*args, &block) # Notify callbacks that a file changed
self.class.file_changed(*args, &block) #
end # @param [String] path The file that changed
# @return [void]
def file_did_change(path) def file_did_change(path)
file_changed.each do |callback, matcher| file_changed.each do |callback, matcher|
next if path.match(%r{^#{build_dir}/}) next if path.match(%r{^#{build_dir}/})
@ -50,11 +67,11 @@ module Middleman::CoreExtensions::FileWatcher
instance_exec(path, &callback) instance_exec(path, &callback)
end end
end end
def file_deleted(*args)
self.class.file_deleted(*args)
end
# Notify callbacks that a file was deleted
#
# @param [String] path The file that was deleted
# @return [void]
def file_did_delete(path) def file_did_delete(path)
file_deleted.each do |callback, matcher| file_deleted.each do |callback, matcher|
next if path.match(%r{^#{build_dir}/}) next if path.match(%r{^#{build_dir}/})

View file

@ -7,6 +7,7 @@ module Middleman::CoreExtensions::FrontMatter
app.set :frontmatter_extensions, %w(.htm .html .php) app.set :frontmatter_extensions, %w(.htm .html .php)
app.extend ClassMethods app.extend ClassMethods
app.send :include, InstanceMethods app.send :include, InstanceMethods
app.delegate :frontmatter_changed, :to => :"self.class"
end end
alias :included :registered alias :included :registered
end end
@ -55,10 +56,6 @@ module Middleman::CoreExtensions::FrontMatter
{ :options => data } { :options => data }
end end
end end
def frontmatter_changed(*args, &block)
self.class.frontmatter_changed(*args, &block)
end
def frontmatter_did_change(path) def frontmatter_did_change(path)
frontmatter_changed.each do |callback, matcher| frontmatter_changed.each do |callback, matcher|