Nowadays all the functionalities in Rails are
built as Ruby gems. For example we can use devise gem for the authentication part of the
application. It’s like a package or library that is written in Ruby
programming language. Moreover it can be imported and used by others in
their programs.

Step 1 Bundler is like a dependency management
tool for Ruby, which is also used by Rails. We
will use Bundler for the basic gem structure. It helps us to install the
correct versions of the gem and forces us to
use the same in the application. So the command for that is, gem bundler install After bundling, we should
specify the gem “name” that we are going
to create i.e. Bundle gem “testgem” will
create a repository shown below

So in this we can see the basic gem
structure. Under the lib folder, version file will be used to mention the
version of the Gem. We can edit the version as per
our convenience and release it that will be the version in Rubygems.

Step 2 We will consider testgem.gemspec, with testgem as the name of the gem that we will create for sample. It will be
used to set up the gem on rubgems, for e.g.,
name of the gem, summary, description, files that are required in this
project, test files that are used to
testing the files in the project etc.

Rake file: – This makes releasing the new
versions of the gem, and also helps other developers to check the test cases if they are going to modify the
particular gem. After the rake, we should
create a test folder and test cases for each segments will be included
here in the app directory.

Step 3 Planning to make a rubygem, then we need to analyse the requirements
what to build up and what all functionalities should be included in that.
While generating, we should create a sample.rb file inside lib folder and
create own class with namespace because the other plugin has also the same
classes then it will get conflict in the names. And require the sample.rb file in the
testgem.rb file like reqiure “testgem/sample”.

Step 4 We have require
“bundler/gem_tasks” in rake file so when we run rake release, it will
release the gem to ruby gems and make it
available. Push to git repository

Decorators allow us to add behavior to objects in runtime and don’t
affect other objects of the class. Decorators can be applied when you need
to dynamically add and remove responsibility to a class. The decorator
pattern is a helpful alternative to creating sub-classes. They give
additional functionality to a class while still keeping the public API consistent. Let’s look at an example
to understand the importance of Ruby Decorators.

Now we will add an extra color as a feature, and the price would be
increased by 150

The simplest way is to create a TattooWithColour subclass that returns 450
in the price method.

class TattooWithColour<Tattoodef price450endend

Next, we need to represent a big tattoo that adds 200 to the price of our
tattoos. We can represent this using a BigTattoo subclass of Tattoo.

class BigTattoo<Tattoodef price500endend

We could also have bigger sized tattoos and they may add further price to
our BigTattoo. If we were to consider that these tattoos types could be
used with colours, we would need to add BigTattooWithColour and
BiggerTattooWithColour subclasses.

With this method, we end up with total of 6 classes. Even Double that the
number if you want to represent these combinations with extra designs on
tattoo. Inheriting dynamically with modules

To simplify our code, we may use modules to dynamically add behavior to our
Tattoo class. Let’s write ColourTattoo and BigTattoo modules for this.

This is good improvement over our inheritance based implementation. Instead
of having sub classes, we just have one
class and 3 modules. If we needed to add extra design to the equation, we
need just four modules instead of 12 classes. Applying the decorator
pattern

This module based solution has simplified our code greatly, but we can
still improve it by using the decorator. We will consider a BiggerTatto as
being formed by twice adding 150 to the cost of a Tattoo.

We can’t do this by our module based approach. It would be tempting to
call tattoo.extend(BigTattoo) twice to get BiggerTattoo. Extending module
second time has no effect when we have already used extend ones.

If we were to continue using the same implementation, we would need to have
a BiggerTattoo module that returns super + 300 as the cost. Instead, we can
use decorator that can be composed to build complex objects. We start with
a decorator called BigTattoo that is a wrapper around a Tattoo object.