Class: Liquid::Registers

Inherits:
Object
  • Object
show all
Defined in:
lib/liquid/registers.rb

Constant Summary collapse

UNDEFINED =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registers = {}) ⇒ Registers

Returns a new instance of Registers.



7
8
9
10
# File 'lib/liquid/registers.rb', line 7

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

Instance Attribute Details

#staticObject (readonly)

Returns the value of attribute static.



5
6
7
# File 'lib/liquid/registers.rb', line 5

def static
  @static
end

Instance Method Details

#[](key) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/liquid/registers.rb', line 16

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

#[]=(key, value) ⇒ Object



12
13
14
# File 'lib/liquid/registers.rb', line 12

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

#delete(key) ⇒ Object



24
25
26
# File 'lib/liquid/registers.rb', line 24

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

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



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/liquid/registers.rb', line 30

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/liquid/registers.rb', line 44

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