Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/andriy-baran/toritori


https://github.com/andriy-baran/toritori

Last synced: 20 days ago
JSON representation

Awesome Lists containing this project

README

        

# Toritori

[![Maintainability](https://api.codeclimate.com/v1/badges/4e5138d5018b81671692/maintainability)](https://codeclimate.com/github/andriy-baran/toritori/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/4e5138d5018b81671692/test_coverage)](https://codeclimate.com/github/andriy-baran/toritori/test_coverage)
[![Gem Version](https://badge.fury.io/rb/toritori.svg)](https://badge.fury.io/rb/toritori)

Simple tool to work with Abstract Factories.
It provides the DSL for defining a set factories and produce objects.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'toritori'
```

And then execute:

$ bundle install

Or install it yourself as:

$ gem install toritori

## Basic usage
First, add module to target class and define factory method
```ruby
require 'toritori'

class MyAbstractFactory
include Toritori

factory :chair
end
```
You'll get a few methods:
```ruby
# top level method similar to FactoryBot
MyAbstractFactory.create(:chair)

# a way for inspecting definitions of factory methods
MyAbstractFactory.factories # => { chair: # }
MyAbstractFactory.factories[:chair] # => chair: #
# an alias methods that reads from factories hash
factory = MyAbstractFactory.chair_factory # => chair: #

# Specific factory actually creates new objects
# top level method just calls it
MyAbstractFactory.factories[:chair].create

factory.base_class #=> #
factory.creation_method #=> :new
```
This example above shows a rare case when we just create instances of anonymous class.
## Setup options
In most cases we want to specify a class of objects we are aiming to produce. Or define a few methods ourself.
Assume we have some class
```ruby
class Table < Struct(:width, :height, :depth)
# ...omitted...
end
```
To specify that we want to produce instances of `Table` class
```ruby
class MyAbstractFactory
factory :table, produces: Table
end

factory = MyAbstractFactory.table_factory
factory.base_class #=> #
factory.creation_method #=> :new

MyAbstractFactory.create(:table, 80, 80, 120) #=> #
```
By default, we just call `new` method to instantiate an object. Some times it's not possible
```ruby
class MyAbstractFactory
factory :file, produces: File, creation_method: :open
end

factory = MyAbstractFactory.file_factory
factory.base_class #=> #
factory.creation_method #=> :open

MyAbstractFactory.file_factory.create('/dev/null') # => #
```
Or you need to use another `factory method`
```ruby
class MyAbstractFactory
factory :user, produces: User do |**kw|
FactoryBot.create(:user, **kw)
end
end

factory = MyAbstractFactory.user_factory
factory.base_class #=> #
factory.creation_method #=> #
```
## Sub-classes
But the main feature of the library is a possibility to change or extend the produced object. It is achieved by defining a sub-class of the target class. That is why `produces` option is required in most cases
```ruby
class MyAbstractFactory
table_factory.subclass do
attr_reader :shape

def initialize(width, height, depth, shape)
super(width, height, depth)
@shape = shape
end
end
end

MyAbstractFactory.create(:table, 80, 80, 80, :round)
#=> #
```
However this operation alters a definition of factory
```ruby
MyAbstractFactory.table_factory.base_class #=> #
MyAbstractFactory.table_factory.base_class.superclass #=> #
MyAbstractFactory.table_factory.creation_method #=> :new
```
Sometimes when sub-class definition is big it is better to put it into a separate file.
```ruby
class ModernTable < Table
# ... omitted ...
end

# Alternatively more generic code
class ModernTable < MyAbstractFactory.table_factory.base_class; end

class MyAbstractFactory
table_factory.subclass produces: ModernTable
end

MyAbstractFactory.table_factory.base_class #=> #
MyAbstractFactory.table_factory.base_class.superclass #=> #
MyAbstractFactory.table_factory.creation_method #=> :new
```
Note, that you should provide a child class otherwise you'll get exception `Toritori::SubclassError`
It is possible to change creation method of a sub-class
```ruby
class MyAbstractFactory
table_factory.subclass creation_method: :create do
def self.create(...)
new(...)
end
end
end

MyAbstractFactory.table_factory.base_class #=> #
MyAbstractFactory.table_factory.base_class.superclass #=> #
MyAbstractFactory.table_factory.creation_method #=> :create
```
Following calls of `subclass` method will create new sub-class based on sub-class generated by previous invocation.
## Inheritance
Let's imagine you need to the following setup
```ruby
class MyAbstractFactory
factory :chair
factory :table
end

class ModernFactory < MyAbstractFactory
chair.subclass do
include ModernStyle
end
end
class VictorianFactory < MyAbstractFactory
chair.subclass do
include VictorianStyle
end
end
```
During inheritance child classes should get the same factories as parent class. Definition of factories is copied from parent to child class
```ruby
copy = MyAbstractFactory.chair_factory.copy
copy.base_class == MyAbstractFactory.chair_factory.base_class
copy.creation_method #=> :new
```
It means that after the inheritance `factories` of child and parent are disconnected. Changes in factory definition in parent factory won't affect child classes and vice versa.

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/andriy-baran/toritori. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/andriy-baran/toritori/blob/master/CODE_OF_CONDUCT.md).

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

## Code of Conduct

Everyone interacting in the Toritori project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/andriy-baran/toritori/blob/master/CODE_OF_CONDUCT.md).