Class: Jekyll::Drops::DocumentDrop

Inherits:
Drop
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/jekyll/drops/document_drop.rb

Direct Known Subclasses

ExcerptDrop

Constant Summary collapse

NESTED_OBJECT_FIELD_BLACKLIST =
%w(
  content output excerpt next previous
).freeze

Constants inherited from Drop

Jekyll::Drops::Drop::NON_CONTENT_METHODS

Instance Method Summary collapse

Methods inherited from Drop

#[], #[]=, #content_methods, #each, #each_key, #fetch, #initialize, #inspect, #key?, #keys, #merge, #merge!, mutable, mutable?, #to_h, #to_json

Constructor Details

This class inherits a constructor from Jekyll::Drops::Drop

Instance Method Details

#<=>(other) ⇒ Object


27
28
29
30
31
32
33
# File 'lib/jekyll/drops/document_drop.rb', line 27

def <=>(other)
  return nil unless other.is_a? DocumentDrop

  cmp = self["date"] <=> other["date"]
  cmp = self["path"] <=> other["path"] if cmp.nil? || cmp.zero?
  cmp
end

#categoriesObject


72
73
74
# File 'lib/jekyll/drops/document_drop.rb', line 72

def categories
  @obj.data["categories"]
end

#collapse_document(doc) ⇒ Object

Generate a Hash which breaks the recursive chain. Certain fields which are normally available are omitted.

Returns a Hash with only non-recursive fields present.


62
63
64
65
66
# File 'lib/jekyll/drops/document_drop.rb', line 62

def collapse_document(doc)
  doc.keys.each_with_object({}) do |(key, _), result|
    result[key] = doc[key] unless NESTED_OBJECT_FIELD_BLACKLIST.include?(key)
  end
end

#collectionObject


19
20
21
# File 'lib/jekyll/drops/document_drop.rb', line 19

def collection
  @obj.collection.label
end

#excerptObject


23
24
25
# File 'lib/jekyll/drops/document_drop.rb', line 23

def excerpt
  fallback_data["excerpt"].to_s
end

#hash_for_json(state = nil) ⇒ Object

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

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

Returns a Hash ready for JSON generation.


49
50
51
52
53
54
55
56
# File 'lib/jekyll/drops/document_drop.rb', line 49

def hash_for_json(state = nil)
  to_h.tap do |hash|
    if state && state.depth >= 2
      hash["previous"] = collapse_document(hash["previous"]) if hash["previous"]
      hash["next"]     = collapse_document(hash["next"]) if hash["next"]
    end
  end
end

#nextObject


39
40
41
# File 'lib/jekyll/drops/document_drop.rb', line 39

def next
  @obj.next_doc.to_liquid
end

#previousObject


35
36
37
# File 'lib/jekyll/drops/document_drop.rb', line 35

def previous
  @obj.previous_doc.to_liquid
end

#tagsObject


76
77
78
# File 'lib/jekyll/drops/document_drop.rb', line 76

def tags
  @obj.data["tags"]
end

#titleObject


68
69
70
# File 'lib/jekyll/drops/document_drop.rb', line 68

def title
  @obj.data["title"]
end