Skip to content

Instantly share code, notes, and snippets.

View dudo's full-sized avatar
🤙

Brett Dudo dudo

🤙
View GitHub Profile
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def self.human_enum_name(enum_name, enum_value)
I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{enum_name.to_s.pluralize}.#{enum_value}")
end
def self.human_enum_collection(enum_name)
send(enum_name.to_s.pluralize).keys.map do |val|
[human_enum_name(enum_name, val), val]
@dudo
dudo / equivalent_binary_trees.go
Last active January 5, 2020 04:12
Go Exercises
package main
import (
"code.google.com/p/go-tour/tree"
"fmt"
)
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
class Order < ActiveRecord::Base
scope :paid, -> { where(id: Payment.paid.select(:order_id)) }
end
# SELECT "orders".* FROM "orders" WHERE "orders"."id" IN (
# SELECT "payments"."order_id"
# FROM "payments"
# WHERE "payments"."state" IN ('charged', 'authorized')
# )
class String
def encrypt(k)
lower_alphabet = [*'a'..'z']
lower_translation_map = lower_alphabet.zip(lower_alphabet.rotate(k%26)).to_h
upper_alphabet = [*'A'..'Z']
upper_translation_map = upper_alphabet.zip(upper_alphabet.rotate(k%26)).to_h
translation_map = lower_translation_map.merge(upper_translation_map)
chars.map { |char| translation_map[char] || char }.join
end
class String
def balanced?
pairs = { '{' => '}', '[' => ']', '(' => ')' }
brackets = chars.each_with_object([]) do |char, stack|
if bracket = pairs[char]
stack << bracket
elsif pairs.values.include?(char)
next if stack.pop == char
# Return a boolean indicating whether the number is a palindrome. Raises a
# TypeError if the argument is not an integer
#
# Caveat: Try to accomplish this without converting the number to a string.
# The goal is to understand your approach to a problem with constraints
# rather than seeing how quickly you can jump to an easy solution.
#
# Example
# palindrome?(1001) => true
# palindrome?(1234) => false
module FastJsonapi
module ErrorSerializer
extend ActiveSupport::Concern
included do
include FastJsonapi::ObjectSerializer
attr_accessor :with_root_key
set_id :title
# Pundit
# Friendly ID
# FastJSON
module ActsAsResource
extend ActiveSupport::Concern
included do
include Pundit
@dudo
dudo / kubernetes.local.md
Last active December 5, 2021 17:00
Tooling for Interacting with Kubernetes

Kubernetes local development

kubectl

Autocomplete

@dudo
dudo / app-constraints-api_constraint.rb
Last active September 15, 2019 23:59
rails api versioned
class ApiConstraint
def initialize(options)
@version = options[:version]
@default = options[:default]
end
def matches?(request)
@default || request.headers.fetch(:accept).include?("version=#{version}")
end
end