Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View marcelorxaviers's full-sized avatar

Marcelo Ribeiro Xavier da Silva marcelorxaviers

View GitHub Profile
# frozen_string_literal: true
# This pattern is often useful in situations where a class cannot anticipate the type of objects it
# will need to create, or in situations where a class wants to delegate the responsibility of
# creating objects to one of its expert subclasses.
# Base class that defines the interface of the object to be created
class Car
def drive
raise NotImplementedError, 'This is an abstract class check one if its subclasses'
# frozen_string_literal: true
# The command pattern is useful in situations where you want to decouple the requester of an action
# from the object that performs the action, need to support undo/redo functionality, require
# flexibility to dynamically change or extend commands, or aim to encapsulate requests as objects
# for easier management and organization in a software system.
# This pattern can be split in three main components: The receiver, the invoker and the command
# frozen_string_literal: true
require 'ostruct'
# Command "Interface"
class Command
attr_accessor :errors, :input, :output
def initialize(**input)
@errors = []
# frozen_string_literal: true
require 'singleton'
class Log
include Singleton
attr_reader :list
def append(text)
# frozen_string_literal: true
# The singleton pattern is used when you need to ensure that only one instance of a class exists
# and provide a global point of access to it.
#
# The most common reason to use this design pattern is to control access to some shared resource,
# for example a database or a file.
# The second most common reason is to create a global variable whose change is more controlled.
class TicketDispenserMachine
private_class_method :new
#!/bin/bash
chruby 2.7.0
export RUBYOPT=-W:no-deprecated
# Install all gem dependencies
bundle install
# Just to prevent showing many "error" messages
dirty_bit=0
class BatchCreate
class << self
def perform(active_records:, conflict_fields:)
return if active_records.empty?
ActiveRecord::Base.connection.execute(
<<-SQL.squish
#{insert_statement(active_records.first)}
#{values(active_records)}
#{on_conflict_statement(conflict_fields)}
class BatchCreate
class << self
def perform(active_records:, conflict_fields:)
return if active_records.empty?
ActiveRecord::Base.connection.execute(
<<-SQL.squish
#{insert_statement(active_records.first)}
#{values(active_records)}
#{on_conflict_statement(active_records.first, conflict_fields)}
if Rails.env.development?
class MarcelosController < ApplicationController
def method
require 'pry-byebug' ; binding.pry
end
end
get '/marcelos_method/:id', to: 'marcelos#method'
end
@marcelorxaviers
marcelorxaviers / flatten.rb
Last active June 26, 2017 19:57
Flatten method for array in Ruby
# flatten.rb
module Flatten
extend self
def perform!(array, type = nil)
return [] if type == Array
recursive(array.to_a, type)
rescue
raise "The parameter has to be a #{type || "mixed types"} collection."