Class: Bridgetown::Drops::ResourceDrop

Inherits:
Drop
  • Object
show all
Extended by:
Forwardable
Defined in:
bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb

Constant Summary collapse

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

Constants inherited from Drop

Drop::NON_CONTENT_METHODS

Instance Method Summary collapse

Methods inherited from Drop

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

Constructor Details

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

Instance Method Details

#<=>(other) ⇒ Object



40
41
42
43
44
45
46
# File 'bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb', line 40

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

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

#collapse_document(doc) ⇒ Hash<String, Object>

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

Returns:

  • (Hash<String, Object>)

    only non-recursive fields present



79
80
81
82
83
# File 'bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb', line 79

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



32
33
34
# File 'bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb', line 32

def collection
  @collection ||= @obj.collection.to_liquid
end

#hash_for_json(state = nil) ⇒ Hash<String, Object>

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

Parameters:

  • state (JSON::State) (defaults to: nil)

    object which determines the state of current processing

Returns:

  • (Hash<String, Object>)

    all the keys and values resolved



63
64
65
66
67
68
69
70
71
72
73
# File 'bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb', line 63

def hash_for_json(state = nil)
  to_h.tap do |hash|
    # use collection label in the hash
    hash["collection"] = hash["collection"]["label"] if hash["collection"]

    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

#inspectObject



94
95
96
# File 'bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb', line 94

def inspect
  JSON.pretty_generate hash_for_json
end

#keysObject



85
86
87
88
89
90
91
92
# File 'bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb', line 85

def keys
  keys_to_remove = %w[next_resource previous_resource]
  (content_methods |
    mutations.keys |
    fallback_data.keys).flatten.reject do |key|
    keys_to_remove.include?(key)
  end
end

#next_resourceObject Also known as: next



48
49
50
# File 'bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb', line 48

def next_resource
  @next ||= @obj.next_resource.to_liquid
end

#previous_resourceObject Also known as: previous



53
54
55
# File 'bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb', line 53

def previous_resource
  @previous ||= @obj.previous_resource.to_liquid
end

#relative_pathObject



36
37
38
# File 'bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb', line 36

def relative_path
  @relative_path ||= @obj.relative_path.to_s
end