From 5db2274e9bea4ddef7eca5a87050db5ddcb42f1c Mon Sep 17 00:00:00 2001 From: mesmerize Date: Tue, 21 Sep 2021 17:13:50 +0200 Subject: [PATCH] decorator? --- lib/product.rb | 12 ++++++++++++ spec/anything_spec.rb | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/product.rb b/lib/product.rb index d4d81c3..9dd0b34 100644 --- a/lib/product.rb +++ b/lib/product.rb @@ -1,3 +1,5 @@ +require 'forwardable' + class Product attr_reader :name, :category, :tags @@ -6,6 +8,16 @@ def initialize(name, category, tags = []) @category = category @tags = tags end +end + +class ProductDecorator + extend Forwardable + + def_delegators :@context, :name, :category, :tags + + def initialize(context) + @context = context + end def as_html_row <<~EOF diff --git a/spec/anything_spec.rb b/spec/anything_spec.rb index 0e4a072..0b66070 100644 --- a/spec/anything_spec.rb +++ b/spec/anything_spec.rb @@ -27,6 +27,6 @@ end it 'can be printed as HTML table row' do - expect(tomato.as_html_row).to eq(expected_result) + expect(ProductDecorator.new(tomato).as_html_row).to eq(expected_result) end end