Skip to content

Commit

Permalink
Change to memoize empty decorator chain
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Jun 28, 2020
1 parent 60e7012 commit 79b50ce
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/pastel/decorator_chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class DecoratorChain
#
# @api public
def self.empty
new
@empty ||= self.new
end

# Create a decorator chain
#
# @api public
def initialize(decorators = [])
def initialize(decorators = [].freeze)
@decorators = decorators
end

Expand Down
8 changes: 7 additions & 1 deletion spec/unit/decorator_chain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
expect(described_class.new).to be_a(Enumerable)
end

describe ".each" do
describe ".empty" do
it "memoizes an empty chain" do
expect(described_class.empty.object_id).to eq(described_class.empty.object_id)
end
end

describe "#each" do
it "yields each decorator" do
first = double("first")
second = double("second")
Expand Down

0 comments on commit 79b50ce

Please sign in to comment.