Skip to content

Instantly share code, notes, and snippets.

View italopaiva's full-sized avatar

Italo Paiva italopaiva

View GitHub Profile
@italopaiva
italopaiva / open_closed_principle_example.rb
Last active October 4, 2017 11:43
An example of the 'O' principle in SOLID - Open-Closed Principle
# Consider this example to illustrate the Open-Closed Principle (OCP):
#
# Let's say you a have a client SomeClient that uses
# a class DatabaseConnection to connect to the database and do something.
#
class SomeClient
def do_something
params = [:mysql, 'host', 1234, 'dbname', 'user', 'pass']
connection = DatabaseConnection.new(*params).connect
end
@italopaiva
italopaiva / single_responsability_principle_rails_example.rb
Created October 4, 2017 10:25
A example of the 'S' principle in SOLID - Single Responsability Principle
# Consider this Rails example to illustrate the Single Responsability Principle(SRP):
#
# Domain information:
# - A document has a number and needs one vinculated process and may have
# many involveds (people involved in the document's matters) on it.
# - A document can only be created if a process is given.
# - The process can only be created if the document was created too.
# - There are some other rules to create a document and them may change in the future.
# For example, the informed document number needs to be checked agains an external system before saving.
#
"""
This code exemplify the use of metaclasses, decorators and signals in python3
"""
def logged(function):
"""
Decorator to log the creation of an object of class with given arguments
"""
def decorator(cls, *args, **kwargs):
print('Object of class %s created!' % cls)