Skip to content

Instantly share code, notes, and snippets.

@abinoam
abinoam / hanami_snippet.rb
Last active July 27, 2018 15:05
A Hanami Snippet to show a callable controller
# Example from: http://hanamirb.org/guides/1.2/getting-started/#implementing-create-action
module Web::Controllers::Books
class Create
include Web::Action
def call(params)
BookRepository.new.create(params[:book])
redirect_to '/books'
@abinoam
abinoam / frontend_api.rb
Created July 9, 2018 16:58 — forked from ErvalhouS/frontend_api.rb
A read-only frontend API abstraction applicable to any application.
# frozen_string_literal: true
module Api
module V1
# Controller to consume read-only data to be used on client's frontend
class FrontEndController < ActionController::API
include Orderable
# GET /:resource
# GET /:resource.json
@abinoam
abinoam / after_commit_error.rb
Created August 29, 2017 22:14
Bug at paranoia 2.3.1 / rails 5.1.x
# frozen_string_literal: true
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
module FactoryGirl::Syntax::Methods
def find_or_create(name, attributes = {}, &block)
factory = FactoryGirl.factory_by_name(name)
clazz = factory.build_class
factory_attributes = FactoryGirl.attributes_for(name)
attributes = factory_attributes.merge(attributes)
clazz.find_or_create_by(attributes, &block)
end
@abinoam
abinoam / codeship_heroku_with_maintenance_mode.sh
Created March 20, 2015 15:30
Codeship custom script for Heroku deploying with maintenance mode
#!/bin/sh
# This codeship custom script depends on:
# - API_KEY env var from Heroku
# - STAGING_APP_NAME
# - STAGING_APP_URL
set -e
export HEROKU_API_KEY="${API_KEY}"
class MegaLotto
NUMBERS = 5
def draw
Array.new(NUMBERS){ single_draw }
end
private
def single_draw
#!/usr/bin/env ruby
def the_main_script
# Your main script
# Do whatever here
x = rand(3)
puts "x = #{x}"
puts "and 10/#{x} = #{10/x}" # When x==0 ZeroDivisionError
end
require 'psych'
class ScalarHandler < Psych::Handler
def parser=(parser)
@parser=parser
end
def mark
@parser.mark
@abinoam
abinoam / append_data_to_daily_files.rb
Last active December 17, 2015 00:39
append_data_to_daily_files - Ruby Talk
def append_data_to_daily_files
Dir.entries('A').each do |file|
next unless file.match(/today/i)
File.open(File.join("A",file), 'a') do |file|
file.puts("hello")
end
end
end
@abinoam
abinoam / regexp_bug_investigation.rb
Created January 21, 2012 02:50
Testing for a possible regexp bug on 1.9.2-290
#!/usr/bin/env ruby
regexp = /^([A-Z]{1,4})([0-9]{1,4})$/
text_phrases =
[ "ABCD1234",
"ABCDE1234",
"ABCD12345",
"ABCDE12345"]