class Jekyll::Drops::Drop

Constants

NON_CONTENT_METHODS
NON_CONTENT_METHOD_NAMES
SETTER_KEYS_STASH

A private stash to avoid repeatedly generating the setter method name string for a call to ‘Drops::Drop#[]=`. The keys of the stash below have a very high probability of being called upon during the course of various `Jekyll::Renderer#run` calls.

Public Class Methods

data_delegator(key) click to toggle source

Generate public Drop instance_methods for given string ‘key`. The generated method access(es) `@obj`’s data hash.

Returns method symbol.

# File lib/jekyll/drops/drop.rb, line 90
def data_delegator(key)
  define_method(key.to_sym) { @obj.data[key] }
end
data_delegators(*strings) click to toggle source

Generate public Drop instance_methods for each string entry in the given list. The generated method(s) access(es) ‘@obj`’s data hash.

Returns nothing.

# File lib/jekyll/drops/drop.rb, line 79
def data_delegators(*strings)
  strings.each do |key|
    data_delegator(key) if key.is_a?(String)
  end
  nil
end
delegate_method(symbol) click to toggle source

Generate public Drop instance_method for given symbol that calls ‘@obj.<sym>`.

Returns delegated method symbol.

# File lib/jekyll/drops/drop.rb, line 64
def delegate_method(symbol)
  define_method(symbol) { @obj.send(symbol) }
end
delegate_method_as(original, delegate) click to toggle source

Generate public Drop instance_method named ‘delegate` that calls `@obj.<original>`.

Returns delegated method symbol.

# File lib/jekyll/drops/drop.rb, line 71
def delegate_method_as(original, delegate)
  define_method(delegate) { @obj.send(original) }
end
delegate_methods(*symbols) click to toggle source

Generate public Drop instance_methods for each symbol in the given list.

Returns nothing.

# File lib/jekyll/drops/drop.rb, line 56
def delegate_methods(*symbols)
  symbols.each { |symbol| delegate_method(symbol) }
  nil
end
getter_method_names() click to toggle source

Array of stringified instance methods that do not end with the assignment operator.

(<klass>.instance_methods always generates a new Array object so it can be mutated)

Returns array of strings.

# File lib/jekyll/drops/drop.rb, line 99
def getter_method_names
  @getter_method_names ||= instance_methods.map!(&:to_s).tap do |list|
    list.reject! { |item| item.end_with?("=") }
  end
end
mutable(is_mutable = nil) click to toggle source

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

# File lib/jekyll/drops/drop.rb, line 34
def mutable(is_mutable = nil)
  @is_mutable = is_mutable || false
end
mutable?() click to toggle source
# File lib/jekyll/drops/drop.rb, line 38
def mutable?
  @is_mutable
end
new(obj) click to toggle source

Create a new Drop

obj - the Jekyll Site, Collection, or Document required by the drop.

Returns nothing

# File lib/jekyll/drops/drop.rb, line 112
def initialize(obj)
  @obj = obj
end
private_delegate_methods(*symbols) click to toggle source

Generate private Drop instance_methods for each symbol in the given list.

Returns nothing.

# File lib/jekyll/drops/drop.rb, line 48
def private_delegate_methods(*symbols)
  symbols.each { |symbol| private delegate_method(symbol) }
  nil
end

Public Instance Methods

[](key) click to toggle source

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

# File lib/jekyll/drops/drop.rb, line 124
def [](key)
  if self.class.mutable? && mutations.key?(key)
    mutations[key]
  elsif self.class.invokable? key
    public_send key
  else
    fallback_data[key]
  end
end
Also aliased as: invoke_drop
[]=(key, val) click to toggle source

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.

# File lib/jekyll/drops/drop.rb, line 148
def []=(key, val)
  setter = SETTER_KEYS_STASH[key] || "#{key}="
  if respond_to?(setter)
    public_send(setter, val)
  elsif respond_to?(key.to_s)
    if self.class.mutable?
      mutations[key] = val
    else
      raise Errors::DropMutationException, "Key #{key} cannot be set in the drop."
    end
  else
    fallback_data[key] = val
  end
end
content_methods() click to toggle source

Generates a list of strings which correspond to content getter methods.

Returns an Array of strings which represent method-specific keys.

# File lib/jekyll/drops/drop.rb, line 167
def content_methods
  @content_methods ||= \
    self.class.getter_method_names \
      - Jekyll::Drops::Drop.getter_method_names \
      - NON_CONTENT_METHOD_NAMES
end
each() { |key, self| ... } click to toggle source
# File lib/jekyll/drops/drop.rb, line 244
def each
  each_key.each do |key|
    yield key, self[key]
  end
end
each_key(&block) click to toggle source

Collects all the keys and passes each to the block in turn.

block - a block which accepts one argument, the key

Returns nothing.

# File lib/jekyll/drops/drop.rb, line 240
def each_key(&block)
  keys.each(&block)
end
fetch(key, default = nil) { |key| ... } click to toggle source

Imitate Hash.fetch method in Drop

Returns value if key is present in Drop, otherwise returns default value KeyError is raised if key is not present and no default value given

# File lib/jekyll/drops/drop.rb, line 279
def fetch(key, default = nil, &block)
  return self[key] if key?(key)
  raise KeyError, %(key not found: "#{key}") if default.nil? && block.nil?
  return yield(key) unless block.nil?
  return default unless default.nil?
end
hash_for_json(*) click to toggle source

Generate a Hash for use in generating JSON. This is useful if fields need to be cleared before the JSON can generate.

Returns a Hash ready for JSON generation.

# File lib/jekyll/drops/drop.rb, line 222
def hash_for_json(*)
  to_h
end
inspect() click to toggle source

Inspect the drop’s keys and values through a JSON representation of its keys and values.

Returns a pretty generation of the hash representation of the Drop.

# File lib/jekyll/drops/drop.rb, line 214
def inspect
  JSON.pretty_generate to_h
end
invoke_drop(key)
Alias for: []
key?(key) click to toggle source

Check if key exists in Drop

key - the string key whose value to fetch

Returns true if the given key is present

# File lib/jekyll/drops/drop.rb, line 179
def key?(key)
  return false if key.nil?
  return true if self.class.mutable? && mutations.key?(key)

  respond_to?(key) || fallback_data.key?(key)
end
keys() click to toggle source

Generates a list of keys with user content as their values. This gathers up the Drop methods and keys of the mutations and underlying data hashes and performs a set union to ensure a list of unique keys for the Drop.

Returns an Array of unique keys for content for the Drop.

# File lib/jekyll/drops/drop.rb, line 192
def keys
  (content_methods |
    mutations.keys |
    fallback_data.keys).flatten
end
merge(other, &block) click to toggle source
# File lib/jekyll/drops/drop.rb, line 250
def merge(other, &block)
  dup.tap do |me|
    if block.nil?
      me.merge!(other)
    else
      me.merge!(other, block)
    end
  end
end
merge!(other) { |key, self, other| ... } click to toggle source
# File lib/jekyll/drops/drop.rb, line 260
def merge!(other)
  other.each_key do |key|
    if block_given?
      self[key] = yield key, self[key], other[key]
    else
      if Utils.mergable?(self[key]) && Utils.mergable?(other[key])
        self[key] = Utils.deep_merge_hashes(self[key], other[key])
        next
      end

      self[key] = other[key] unless other[key].nil?
    end
  end
end
to_h() click to toggle source

Generate a Hash representation of the Drop by resolving each key’s value. It includes Drop methods, mutations, and the underlying object’s data. See the documentation for Drop#keys for more.

Returns a Hash with all the keys and values resolved.

# File lib/jekyll/drops/drop.rb, line 203
def to_h
  keys.each_with_object({}) do |(key, _), result|
    result[key] = self[key]
  end
end
Also aliased as: to_hash
to_hash()
Alias for: to_h
to_json(state = nil) click to toggle source

Generate a JSON representation of the Drop.

state - the JSON::State object which determines the state of current processing.

Returns a JSON representation of the Drop in a String.

# File lib/jekyll/drops/drop.rb, line 231
def to_json(state = nil)
  JSON.generate(hash_for_json(state), state)
end

Private Instance Methods

mutations() click to toggle source
# File lib/jekyll/drops/drop.rb, line 288
def mutations
  @mutations ||= {}
end