Merge pull request #4285 from jekyll/drop-refactor

Merge pull request 4285
This commit is contained in:
Parker Moore 2015-12-26 13:50:03 -05:00
commit f386752184
8 changed files with 89 additions and 62 deletions

View File

@ -1,11 +1,13 @@
# encoding: UTF-8 # encoding: UTF-8
require "jekyll/drops/immutable_drop" require "jekyll/drops/drop"
module Jekyll module Jekyll
module Drops module Drops
class CollectionDrop < ImmutableDrop class CollectionDrop < Drop
extend Forwardable extend Forwardable
mutable false
def_delegator :@obj, :write?, :output def_delegator :@obj, :write?, :output
def_delegators :@obj, :label, :docs, :files, :directory, def_delegators :@obj, :label, :docs, :files, :directory,
:relative_directory :relative_directory

View File

@ -2,9 +2,11 @@
module Jekyll module Jekyll
module Drops module Drops
class DocumentDrop < ImmutableDrop class DocumentDrop < Drop
extend Forwardable extend Forwardable
mutable false
def_delegator :@obj, :next_doc, :next def_delegator :@obj, :next_doc, :next
def_delegator :@obj, :previous_doc, :previous def_delegator :@obj, :previous_doc, :previous
def_delegator :@obj, :relative_path, :path def_delegator :@obj, :relative_path, :path

74
lib/jekyll/drops/drop.rb Normal file
View File

@ -0,0 +1,74 @@
# encoding: UTF-8
module Jekyll
module Drops
class Drop < Liquid::Drop
# Get or set whether the drop class is mutable.
# Mutability determines whether or not pre-defined fields may be
# overwritten.
#
# is_mutable - Boolean set mutability of the class (default: nil)
#
# Returns the mutability of the class
def self.mutable(is_mutable = nil)
if is_mutable
@is_mutable = is_mutable
end
@is_mutable || false
end
# Create a new Drop
#
# obj - the Jekyll Site, Collection, or Document required by the
# drop.
#
# Returns nothing
def initialize(obj)
@obj = obj
@mutations = {} # only if mutable: true
end
# Access a method in the Drop or a field in the underlying hash data.
# If mutable, checks the mutations first. Then checks the methods,
# and finally check the underlying hash (e.g. document front matter)
# if all the previous places didn't match.
#
# key - the string key whose value to fetch
#
# Returns the value for the given key, or nil if none exists
def [](key)
if self.class.mutable && @mutations.key?(key)
@mutations[key]
elsif respond_to? key
public_send key
else
fallback_data[key]
end
end
# Set a field in the Drop. If mutable, sets in the mutations and
# returns. If not mutable, checks first if it's trying to override a
# Drop method and raises a DropMutationException if so. If not
# mutable and the key is not a method on the Drop, then it sets the
# key to the value in the underlying hash (e.g. document front
# matter)
#
# key - the String key whose value to set
# val - the Object to set the key's value to
#
# Returns the value the key was set to unless the Drop is not mutable
# and the key matches a method in which case it raises a
# DropMutationException.
def []=(key, val)
if self.class.mutable
@mutations[key] = val
elsif respond_to? key
raise Errors::DropMutationException, "Key #{key} cannot be set in the drop."
else
fallback_data[key] = val
end
end
end
end
end

View File

@ -1,28 +0,0 @@
# encoding: UTF-8
module Jekyll
module Drops
class ImmutableDrop < Liquid::Drop
def initialize(obj)
@obj = obj
end
def [](key)
if respond_to? key
public_send key
else
fallback_data[key]
end
end
def []=(key, val)
if respond_to? key
raise DropMutationException, "Key #{key} cannot be set in the drop."
else
fallback_data[key] = val
end
end
end
end
end

View File

@ -1,28 +0,0 @@
# encoding: UTF-8
module Jekyll
module Drops
class MutableDrop < Liquid::Drop
def initialize(obj)
@obj = obj
@mutations = {}
end
def [](key)
if @mutations.key? key
@mutations[key]
elsif respond_to? key
public_send key
else
fallback_data[key]
end
end
def []=(key, val)
@mutations[key] = val
end
end
end
end

View File

@ -2,9 +2,11 @@
module Jekyll module Jekyll
module Drops module Drops
class SiteDrop < ImmutableDrop class SiteDrop < Drop
extend Forwardable extend Forwardable
mutable false
def_delegator :@obj, :site_data, :data def_delegator :@obj, :site_data, :data
def_delegators :@obj, :time, :pages, :static_files, :documents, def_delegators :@obj, :time, :pages, :static_files, :documents,
:tags, :categories :tags, :categories

View File

@ -2,7 +2,8 @@
module Jekyll module Jekyll
module Drops module Drops
class UnifiedPayloadDrop < ImmutableDrop class UnifiedPayloadDrop < Drop
mutable false
attr_accessor :page, :layout, :content, :paginator attr_accessor :page, :layout, :content, :paginator
attr_accessor :highlighter_prefix, :highlighter_suffix attr_accessor :highlighter_prefix, :highlighter_suffix

View File

@ -2,9 +2,11 @@
module Jekyll module Jekyll
module Drops module Drops
class UrlDrop < ImmutableDrop class UrlDrop < Drop
extend Forwardable extend Forwardable
mutable false
def_delegator :@obj, :cleaned_relative_path, :path def_delegator :@obj, :cleaned_relative_path, :path
def_delegator :@obj, :output_ext, :output_ext def_delegator :@obj, :output_ext, :output_ext