Class: Bridgetown::Drops::Drop

Inherits:
Liquid::Drop
  • Object
show all
Includes:
Enumerable
Defined in:
lib/bridgetown-core/drops/drop.rb

Constant Summary collapse

NON_CONTENT_METHODS =
[:fallback_data, :collapse_document].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Drop

Create a new Drop

drop.

Parameters:

  • obj (Object)

    the Bridgetown Site, Collection, or Resource required by the



30
31
32
# File 'lib/bridgetown-core/drops/drop.rb', line 30

def initialize(obj) # rubocop:disable Lint/MissingSuper
  @obj = obj
end

Class Method Details

.mutable(is_mutable = nil) ⇒ Boolean

Get or set whether the drop class is mutable. Mutability determines whether or not pre-defined fields may be overwritten.

Parameters:

  • is_mutable (Boolean) (defaults to: nil)

    set mutability of the class

Returns:

  • (Boolean)

    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

Returns the mutability of the class.

Returns:

  • (Boolean)

    the mutability of the class



22
23
24
# File 'lib/bridgetown-core/drops/drop.rb', line 22

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.

Parameters:

  • key (String)

    key whose value to fetch

Returns:

  • (Object, nil)

    returns the value for the given key if present



41
42
43
44
45
46
47
48
49
# File 'lib/bridgetown-core/drops/drop.rb', line 41

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 an exception 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)

Parameters:

  • key (String)

    key whose value to set

  • val (Object)

    what to set the key's value to

Returns:

  • (Object)

    the value the key was set to unless the Drop is not mutable and the key matches a method in which case it raises an exception



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

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::FatalException, "Key #{key} cannot be set in the drop."
    end

    mutations[key] = val
  else
    fallback_data[key] = val
  end
end

#content_methodsArray<String>

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

Returns:

  • (Array<String>)

    method-specific keys



80
81
82
83
84
85
86
87
88
# File 'lib/bridgetown-core/drops/drop.rb', line 80

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

#eachObject



153
154
155
156
157
# File 'lib/bridgetown-core/drops/drop.rb', line 153

def each
  each_key.each do |key|
    yield key, self[key]
  end
end

#each_keyObject

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



149
150
151
# File 'lib/bridgetown-core/drops/drop.rb', line 149

def each_key(&)
  keys.each(&)
end

#fetch(key, default = nil, &block) ⇒ Object

Imitate Hash.fetch method in Drop

Returns:

  • (Object)

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

Raises:

  • (KeyError)


188
189
190
191
192
193
194
# File 'lib/bridgetown-core/drops/drop.rb', line 188

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_jsonHash<String, Object>

Generate a Hash for use in generating JSON. Essentially an alias for to_h

Returns:

  • (Hash<String, Object>)

    all the keys and values resolved



136
137
138
# File 'lib/bridgetown-core/drops/drop.rb', line 136

def hash_for_json(*)
  to_h
end

#inspectString

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

Returns:

  • (String)


129
130
131
# File 'lib/bridgetown-core/drops/drop.rb', line 129

def inspect
  JSON.pretty_generate to_h
end

#key?(key) ⇒ Boolean

Check if key exists in Drop

Parameters:

  • key (String)

    key whose value to set

Returns:

  • (Boolean)

    true if the given key is present



94
95
96
97
98
99
# File 'lib/bridgetown-core/drops/drop.rb', line 94

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

#keysArray<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.

Returns:

  • (Array<String>)


107
108
109
110
111
# File 'lib/bridgetown-core/drops/drop.rb', line 107

def keys
  (content_methods |
    mutations.keys |
    fallback_data.keys).flatten
end

#merge(other, &block) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/bridgetown-core/drops/drop.rb', line 159

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



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/bridgetown-core/drops/drop.rb', line 169

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_hHash<String, 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:

  • (Hash<String, Object>)

    all the keys and values resolved



118
119
120
121
122
# File 'lib/bridgetown-core/drops/drop.rb', line 118

def to_h
  keys.each_with_object({}) do |(key, _), result|
    result[key] = self[key]
  end
end

#to_json(state = nil) ⇒ String

Generate a JSON representation of the Drop

Parameters:

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

    object which determines the state of current processing

Returns:

  • (String)

    JSON representation of the Drop



144
145
146
# File 'lib/bridgetown-core/drops/drop.rb', line 144

def to_json(state = nil)
  JSON.generate(hash_for_json(state), state)
end