Sha256: ee850c561a3d963ae0ff936f41256e5822a37ce3373b7575c9011c87efccdb3d

Contents?: true

Size: 965 Bytes

Versions: 4

Compression:

Stored size: 965 Bytes

Contents

# frozen_string_literal: true

module Liquid
  class Registers
    attr_reader :static

    def initialize(registers = {})
      @static = registers.is_a?(Registers) ? registers.static : registers
      @changes = {}
    end

    def []=(key, value)
      @changes[key] = value
    end

    def [](key)
      if @changes.key?(key)
        @changes[key]
      else
        @static[key]
      end
    end

    def delete(key)
      @changes.delete(key)
    end

    UNDEFINED = Object.new

    def fetch(key, default = UNDEFINED, &block)
      if @changes.key?(key)
        @changes.fetch(key)
      elsif default != UNDEFINED
        if block_given?
          @static.fetch(key, &block)
        else
          @static.fetch(key, default)
        end
      else
        @static.fetch(key, &block)
      end
    end

    def key?(key)
      @changes.key?(key) || @static.key?(key)
    end
  end

  # Alias for backwards compatibility
  StaticRegisters = Registers
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
liquid-5.6.0.rc1 lib/liquid/registers.rb
liquid-5.5.1 lib/liquid/registers.rb
liquid-5.5.0 lib/liquid/registers.rb
liquid-5.4.0 lib/liquid/registers.rb