Make a ProxyResource which extends Resource, rather than injecting methods into Resource

This commit is contained in:
Thomas Reynolds 2014-07-04 10:38:25 -07:00
parent 6bb9673630
commit 1bd7dab1a3
5 changed files with 59 additions and 62 deletions

View file

@ -52,7 +52,7 @@ module Middleman::CoreExtensions
resource.add_metadata options: opts, page: fmdata resource.add_metadata options: opts, page: fmdata
resource.ignore! if ignored == true && !resource.proxy? resource.ignore! if ignored == true && !resource.is_a?(::Middleman::Sitemap::ProxyResource)
# TODO: Save new template here somewhere? # TODO: Save new template here somewhere?
end end

View file

@ -162,8 +162,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
path = path.sub(options[:templates_dir] + '/', '') path = path.sub(options[:templates_dir] + '/', '')
p = ::Middleman::Sitemap::Resource.new(app.sitemap, path) p = ::Middleman::Sitemap::ProxyResource.new(app.sitemap, path, source_path)
p.proxy_to(source_path)
p.add_metadata locals: { lang: lang, page_id: path }, options: { lang: lang } p.add_metadata locals: { lang: lang, page_id: path }, options: { lang: lang }
::I18n.locale = old_locale ::I18n.locale = old_locale

View file

@ -58,10 +58,12 @@ module Middleman
# @return [Boolean] # @return [Boolean]
def ignored? def ignored?
return true if @ignored return true if @ignored
# Ignore based on the source path (without template extensions) # Ignore based on the source path (without template extensions)
return true if @app.sitemap.ignored?(path) return true if @app.sitemap.ignored?(path)
# This allows files to be ignored by their source file name (with template extensions) # This allows files to be ignored by their source file name (with template extensions)
!proxy? && @app.sitemap.ignored?(source_file.sub("#{@app.source_dir}/", '')) @app.sitemap.ignored?(source_file.sub("#{@app.source_dir}/", ''))
end end
end end
end end

View file

@ -10,8 +10,6 @@ module Middleman
@app.define_singleton_method(:proxy, &method(:create_proxy)) @app.define_singleton_method(:proxy, &method(:create_proxy))
@proxy_configs = Set.new @proxy_configs = Set.new
::Middleman::Sitemap::Resource.send :include, ProxyResourceInstanceMethods
end end
# Setup a proxy from a path to a target # Setup a proxy from a path to a target
@ -43,11 +41,11 @@ module Middleman
# @return [void] # @return [void]
def manipulate_resource_list(resources) def manipulate_resource_list(resources)
resources + @proxy_configs.map do |config| resources + @proxy_configs.map do |config|
p = ::Middleman::Sitemap::Resource.new( p = ProxyResource.new(
@app.sitemap, @app.sitemap,
config.path config.path,
config.target
) )
p.proxy_to(config.target)
p.add_metadata(config.metadata) p.add_metadata(config.metadata)
p p
@ -89,63 +87,59 @@ module Middleman
path.hash path.hash
end end
end end
end
module ProxyResourceInstanceMethods class ProxyResource < ::Middleman::Sitemap::Resource
# Whether this page is a proxy # Initialize resource with parent store and URL
# @return [Boolean] # @param [Middleman::Sitemap::Store] store
def proxy? # @param [String] path
@proxied_to # @param [String] source_file
def initialize(store, path, target)
super(store, path)
target = ::Middleman::Util.normalize_path(target)
raise "You can't proxy #{path} to itself!" if target == path
@target = target
end
# The resource for the page this page is proxied to. Throws an exception
# if there is no resource.
# @return [Sitemap::Resource]
def target_resource
resource = @store.find_resource_by_path(@target)
unless resource
raise "Path #{path} proxies to unknown file #{@target}:#{@store.resources.map(&:path)}"
end end
# Set this page to proxy to a target path if resource.is_a? ProxyResource
# @param [String] target raise "You can't proxy #{path} to #{@target} which is itself a proxy."
# @return [void]
def proxy_to(target)
target = ::Middleman::Util.normalize_path(target)
raise "You can't proxy #{path} to itself!" if target == path
@proxied_to = target
end end
# The path of the page this page is proxied to, or nil if it's not proxied. resource
# @return [String] end
attr_reader :proxied_to
# The resource for the page this page is proxied to. Throws an exception # rubocop:disable AccessorMethodName
# if there is no resource. def get_source_file
# @return [Sitemap::Resource] target_resource.source_file
def proxied_to_resource end
proxy_resource = @store.find_resource_by_path(proxied_to)
unless proxy_resource def content_type
raise "Path #{path} proxies to unknown file #{proxied_to}:#{@store.resources.map(&:path)}" mime_type = super
end return mime_type if mime_type
if proxy_resource.proxy? target_resource.content_type
raise "You can't proxy #{path} to #{proxied_to} which is itself a proxy." end
end
proxy_resource # Whether the Resource is ignored
end # @return [Boolean]
def ignored?
return true if @ignored
# rubocop:disable AccessorMethodName # Ignore based on the source path (without template extensions)
def get_source_file return true if @app.sitemap.ignored?(path)
if proxy?
proxied_to_resource.source_file
else
super
end
end
def content_type false
mime_type = super
return mime_type if mime_type
if proxy?
proxied_to_resource.content_type
else
nil
end
end
end end
end end
end end

View file

@ -2,12 +2,14 @@ require 'middleman-core/sitemap/extensions/traversal'
require 'middleman-core/sitemap/extensions/content_type' require 'middleman-core/sitemap/extensions/content_type'
require 'middleman-core/file_renderer' require 'middleman-core/file_renderer'
require 'middleman-core/template_renderer' require 'middleman-core/template_renderer'
require 'hamster/immutable'
module Middleman module Middleman
# Sitemap namespace # Sitemap namespace
module Sitemap module Sitemap
# Sitemap Resource class # Sitemap Resource class
class Resource class Resource
# include ::Hamster::Immutable
include Middleman::Sitemap::Extensions::Traversal include Middleman::Sitemap::Extensions::Traversal
include Middleman::Sitemap::Extensions::ContentType include Middleman::Sitemap::Extensions::ContentType
@ -25,13 +27,6 @@ module Middleman
# @return [String] # @return [String]
alias_method :request_path, :destination_path alias_method :request_path, :destination_path
# Set the on-disk source file for this resource
# @return [String]
def source_file
# TODO: Make this work when get_source_file doesn't exist
@source_file || get_source_file
end
# Initialize resource with parent store and URL # Initialize resource with parent store and URL
# @param [Middleman::Sitemap::Store] store # @param [Middleman::Sitemap::Store] store
# @param [String] path # @param [String] path
@ -50,6 +45,13 @@ module Middleman
@metadata = { options: {}, locals: {}, page: {} } @metadata = { options: {}, locals: {}, page: {} }
end end
# Set the on-disk source file for this resource
# @return [String]
def source_file
# TODO: Make this work when get_source_file doesn't exist
@source_file || get_source_file
end
# Whether this resource has a template file # Whether this resource has a template file
# @return [Boolean] # @return [Boolean]
def template? def template?