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.ignore! if ignored == true && !resource.proxy?
resource.ignore! if ignored == true && !resource.is_a?(::Middleman::Sitemap::ProxyResource)
# TODO: Save new template here somewhere?
end

View file

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

View file

@ -58,10 +58,12 @@ module Middleman
# @return [Boolean]
def ignored?
return true if @ignored
# Ignore based on the source path (without template extensions)
return true if @app.sitemap.ignored?(path)
# 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

View file

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