Skip to content
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
5 changes: 5 additions & 0 deletions lib/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Product
def initialize(type)
@type = type
end
end
17 changes: 17 additions & 0 deletions lib/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class ProductsController
# include Singleton - right way to do that

@instance = new

private_class_method :new

attr_accessor :product

def self.instance
@instance
end

def show
@product = PRODUCT
end
end
4 changes: 4 additions & 0 deletions spec/anything_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@

describe ProductsController do
it 'cannot be instantiated via :new' do
expect{ ProductsController.new }.to raise_error NoMethodError
end

it 'can be instantiated only once' do
expect(ProductsController.instance.object_id).to eq(ProductsController.instance.object_id)
end

it 'does some work' do
ProductsController.instance.show
expect(ProductsController.instance.product).to be(PRODUCT)
end
end