Class: Bridgetown::Drops::ResourceDrop
- Extended by:
- Forwardable
- Defined in:
- 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
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#collapse_document(doc) ⇒ Object
Generate a Hash which breaks the recursive chain.
- #collection ⇒ Object
-
#hash_for_json(state = nil) ⇒ Object
Generate a Hash for use in generating JSON.
-
#inspect ⇒ Object
Inspect the drop's keys and values through a JSON representation of its keys and values.
-
#keys ⇒ Array<String>
Generates a list of keys with user content as their values.
- #next_resource ⇒ Object (also: #next)
- #previous_resource ⇒ Object (also: #previous)
- #relative_path ⇒ Object
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 '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) ⇒ 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.
80 81 82 83 84 |
# File 'lib/bridgetown-core/drops/resource_drop.rb', line 80 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 |
#collection ⇒ Object
32 33 34 |
# File 'lib/bridgetown-core/drops/resource_drop.rb', line 32 def collection @collection ||= @obj.collection.to_liquid 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.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/bridgetown-core/drops/resource_drop.rb', line 64 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 |
#inspect ⇒ Object
Inspect the drop's keys and values through a JSON representation of its keys and values.
103 104 105 |
# File 'lib/bridgetown-core/drops/resource_drop.rb', line 103 def inspect JSON.pretty_generate hash_for_json end |
#keys ⇒ Array<String>
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.
92 93 94 95 96 97 98 99 |
# File 'lib/bridgetown-core/drops/resource_drop.rb', line 92 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_resource ⇒ Object Also known as: next
48 49 50 |
# File 'lib/bridgetown-core/drops/resource_drop.rb', line 48 def next_resource @next ||= @obj.next_resource.to_liquid end |
#previous_resource ⇒ Object Also known as: previous
53 54 55 |
# File 'lib/bridgetown-core/drops/resource_drop.rb', line 53 def previous_resource @previous ||= @obj.previous_resource.to_liquid end |
#relative_path ⇒ Object
36 37 38 |
# File 'lib/bridgetown-core/drops/resource_drop.rb', line 36 def relative_path @relative_path ||= @obj.relative_path.to_s end |