Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring back Registers#each #1663

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/liquid/registers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ def delete(key)

UNDEFINED = Object.new

def keys
(@static.keys + @changes.keys).uniq
end

def each(&block)
keys.each do |key|
yield(key, fetch(key))
end
end

def fetch(key, default = UNDEFINED, &block)
if @changes.key?(key)
@changes.fetch(key)
Expand Down
24 changes: 24 additions & 0 deletions test/unit/registers_unit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ def test_delete
assert_nil(static_register.delete(:d))
end

def test_keys
static_register = Registers.new(a: 1, b: 2)
assert_equal([:a, :b], static_register.keys.sort)

static_register[:b] = 3
assert_equal([:a, :b], static_register.keys.sort)

static_register[:c] = 4
assert_equal([:a, :b, :c], static_register.keys.sort)
end

def test_each
static_register = Registers.new(a: 1, b: 2)

keys = []
vals = []
static_register.each do |key, val|
keys << key
vals << val
end
assert_equal([:a, :b], keys.sort)
assert_equal([1, 2], vals.sort)
end

def test_fetch
static_register = Registers.new(a: 1, b: 2)
static_register[:b] = 22
Expand Down