Skip to content

Instantly share code, notes, and snippets.

View gosukiwi's full-sized avatar

Federico Ramirez gosukiwi

View GitHub Profile
@gosukiwi
gosukiwi / demo.rb
Created May 29, 2019 18:41
Custom predicates for Ransack, allows you to search inside a YAML-serialized Array column
# some model
serialize :my_field, Array
ransacker :my_field_raw, type: :string do
Arel.sql('my_table.my_field')
end
# some view (using simple form)
= search_form_for @q, builder: SimpleForm::FormBuilder do |f|
# This makes sure UTF8 characters don't break the encoding/decoding. For
# more info: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
#
App.Lib.Base64 =
encode: (str) ->
window.btoa unescape(encodeURIComponent(str))
decode: (str) ->
decodeURIComponent escape(window.atob(str))
@gosukiwi
gosukiwi / demo.rb
Last active January 13, 2019 15:43
class Token
attr_reader :type, :value
def initialize(type:, value:)
@type = type
@value = value
raise InvalidTokenError if value.nil? || type.nil?
end
def self.null
NullToken.new
@gosukiwi
gosukiwi / sed-cheatsheet.md
Last active September 3, 2018 17:38
sed cheatsheet

Sed Cheatsheet

Sed is a non-interactive line-based text editor. It can perform an edition in either file or STDIN and print it in STDOUT, which can later be redirected to a file it needed.

CLI options

-e  Editing instructions follows
-f  File name of script follows
-n  Supress automatic output of lines
-i  Extension - Edit files in-place, saving backups with the specified extension.  

If a zero-length extension is given, no backup will be saved.

;; a plist associating keywords with functions
(defvar *commands*
'(:title title-command))
(defun title-command (params)
(format t "Title command function. Params: ~A" params))
;; I want to read the keyword from the CLI, but it's a char array
;; how can I cast it to keyword?
(apply (getf *commands* (read-line)) '(1 2 3))
class SentenceVisitor
SENTENCE_VISITORS = {
"BOLD" => BoldVisitor,
"EMPHASIS" => EmphasisVisitor,
"TEXT" => TextVisitor
}.freeze
def visit(node)
visitor_for(node).visit(node)
end
# 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
@gosukiwi
gosukiwi / demo.rb
Last active May 15, 2018 17:42
Helper Methods Module Pattern
# app/controllers/books_controller.rb
class BooksController < ApplicationController
include HelperMethods::BooksController
def index; end
def show; end
end
# app/controllers/helper_methods/books_controller.rb
module HelperMethods
@gosukiwi
gosukiwi / rspec_rails_cheetsheet.rb
Created January 17, 2018 18:53 — forked from mlr/rspec_rails_cheetsheet.rb
Rspec Rails cheatsheet with expect syntax (including capybara matchers)
# Model
expect(@user).to have(1).error_on(:username) # checks whether there is an error in username
expect(@user.errors[:username]).to include("can't be blank") # check for the error message
# Rendering
expect(response).to render_template(:index)
# Redirecting
@gosukiwi
gosukiwi / .vimrc
Created August 10, 2016 01:42
My vim configuration
" [ CORE ] ------------------------------------------------------------------
" Very much needed settings to provide a solid base for source code editting
" DEPENDENCIES:
" - vim-plug: For managing plugins
" - ag: The silver searcher
" - editorconfig: For projects where common configuration is important
" - SourceCodePro: Pretty font for coding
" ---------------------------------------------------------------------------
" don't make vim compatible with vi