Class: Bridgetown::Drops::Drop
- Inherits:
-
Liquid::Drop
- Object
- Liquid::Drop
- Bridgetown::Drops::Drop
- Includes:
- Enumerable
- Defined in:
- lib/bridgetown-core/drops/drop.rb
Direct Known Subclasses
CollectionDrop, GeneratedPageDrop, RelationsDrop, ResourceDrop, SiteDrop, StaticFileDrop, UnifiedPayloadDrop
Constant Summary collapse
- NON_CONTENT_METHODS =
[:fallback_data, :collapse_document].freeze
Class Method Summary collapse
-
.mutable(is_mutable = nil) ⇒ Object
Get or set whether the drop class is mutable.
- .mutable? ⇒ Boolean
Instance Method Summary collapse
-
#[](key) ⇒ Object
(also: #invoke_drop)
Access a method in the Drop or a field in the underlying hash data.
-
#[]=(key, val) ⇒ Object
Set a field in the Drop.
-
#content_methods ⇒ Object
Generates a list of strings which correspond to content getter methods.
- #each ⇒ Object
-
#each_key ⇒ Object
Collects all the keys and passes each to the block in turn.
-
#fetch(key, default = nil, &block) ⇒ Object
Imitate Hash.fetch method in Drop.
-
#hash_for_json ⇒ Object
Generate a Hash for use in generating JSON.
-
#initialize(obj) ⇒ Drop
constructor
Create a new Drop.
-
#inspect ⇒ Object
Inspect the drop's keys and values through a JSON representation of its keys and values.
-
#key?(key) ⇒ Boolean
Check if key exists in Drop.
-
#keys ⇒ Array<String>
Generates a list of keys with user content as their values.
- #merge(other, &block) ⇒ Object
- #merge!(other) ⇒ Object
-
#to_h ⇒ Object
(also: #to_hash)
Generate a Hash representation of the Drop by resolving each key's value.
-
#to_json(state = nil) ⇒ Object
Generate a JSON representation of the Drop.
Constructor Details
#initialize(obj) ⇒ Drop
Create a new Drop
obj - the Bridgetown Site, Collection, or Resource required by the drop.
Returns nothing
31 32 33 |
# File 'lib/bridgetown-core/drops/drop.rb', line 31 def initialize(obj) # rubocop:disable Lint/MissingSuper @obj = obj end |
Class Method Details
.mutable(is_mutable = nil) ⇒ Object
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
17 18 19 |
# File 'lib/bridgetown-core/drops/drop.rb', line 17 def self.mutable(is_mutable = nil) @is_mutable = is_mutable || false end |
.mutable? ⇒ Boolean
21 22 23 |
# File 'lib/bridgetown-core/drops/drop.rb', line 21 def self.mutable? @is_mutable end |
Instance Method Details
#[](key) ⇒ Object Also known as: invoke_drop
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
43 44 45 46 47 48 49 50 51 |
# File 'lib/bridgetown-core/drops/drop.rb', line 43 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 |
#[]=(key, val) ⇒ Object
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.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/bridgetown-core/drops/drop.rb', line 67 def []=(key, val) setter = "#{key}=" if respond_to?(setter) public_send(setter, val) elsif respond_to?(key.to_s) unless self.class.mutable? raise Errors::DropMutationException, "Key #{key} cannot be set in the drop." end mutations[key] = val else fallback_data[key] = val end end |
#content_methods ⇒ Object
Generates a list of strings which correspond to content getter methods.
Returns an Array of strings which represent method-specific keys.
86 87 88 89 90 91 92 93 94 |
# File 'lib/bridgetown-core/drops/drop.rb', line 86 def content_methods @content_methods ||= ( self.class.instance_methods \ - Bridgetown::Drops::Drop.instance_methods \ - NON_CONTENT_METHODS ).map(&:to_s).reject do |method| method.end_with?("=") end end |
#each ⇒ Object
166 167 168 169 170 |
# File 'lib/bridgetown-core/drops/drop.rb', line 166 def each each_key.each do |key| yield key, self[key] end end |
#each_key ⇒ Object
Collects all the keys and passes each to the block in turn.
block - a block which accepts one argument, the key
Returns nothing.
162 163 164 |
# File 'lib/bridgetown-core/drops/drop.rb', line 162 def each_key(&) keys.each(&) end |
#fetch(key, default = nil, &block) ⇒ Object
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
201 202 203 204 205 206 207 |
# File 'lib/bridgetown-core/drops/drop.rb', line 201 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? default unless default.nil? end |
#hash_for_json ⇒ Object
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.
144 145 146 |
# File 'lib/bridgetown-core/drops/drop.rb', line 144 def hash_for_json(*) to_h end |
#inspect ⇒ Object
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.
136 137 138 |
# File 'lib/bridgetown-core/drops/drop.rb', line 136 def inspect JSON.pretty_generate to_h end |
#key?(key) ⇒ Boolean
Check if key exists in Drop
key - the string key whose value to fetch
Returns true if the given key is present
101 102 103 104 105 106 |
# File 'lib/bridgetown-core/drops/drop.rb', line 101 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 ⇒ 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.
114 115 116 117 118 |
# File 'lib/bridgetown-core/drops/drop.rb', line 114 def keys (content_methods | mutations.keys | fallback_data.keys).flatten end |
#merge(other, &block) ⇒ Object
172 173 174 175 176 177 178 179 180 |
# File 'lib/bridgetown-core/drops/drop.rb', line 172 def merge(other, &block) dup.tap do |me| if block.nil? me.merge!(other) else me.merge!(other, block) end end end |
#merge!(other) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/bridgetown-core/drops/drop.rb', line 182 def merge!(other) other.each_key do |key| if block_given? self[key] = yield key, self[key], other[key] else if Utils.mergeable?(self[key]) && Utils.mergeable?(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 ⇒ Object Also known as: to_hash
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.
125 126 127 128 129 |
# File 'lib/bridgetown-core/drops/drop.rb', line 125 def to_h keys.each_with_object({}) do |(key, _), result| result[key] = self[key] end end |
#to_json(state = nil) ⇒ Object
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.
153 154 155 |
# File 'lib/bridgetown-core/drops/drop.rb', line 153 def to_json(state = nil) JSON.generate(hash_for_json(state), state) end |