Skip to content

Instantly share code, notes, and snippets.

View backpackerhh's full-sized avatar
🏠
Working from home

David Montesdeoca backpackerhh

🏠
Working from home
View GitHub Profile
@backpackerhh
backpackerhh / domain_events_providers.rb
Created March 25, 2024 18:26
Event bus implemented in Ruby
YourApp.register_provider :domain_events, namespace: true do # dry-system
prepare do
Dir[target.root.join("path/to/**/*_event_subscriber.rb")].each { |file| require file }
register "subscribers", EventSubscriber.subclasses
end
start do
register "bus", InMemoryEventBus.new
register "async_bus", SidekiqEventBus.new
@backpackerhh
backpackerhh / core-set.sql
Last active March 18, 2024 17:24
SQL - Social-Network Query Exercises
-- 1. Find the names of all students who are friends with someone named Gabriel.
SELECT H1.name
FROM Highschooler H1
INNER JOIN Friend ON H1.ID = Friend.ID1
INNER JOIN Highschooler H2 ON H2.ID = Friend.ID2
WHERE H2.name = "Gabriel";
-- 2. For every student who likes someone 2 or more grades younger than themselves, return that student's name and grade, and the name and grade of the student they like.
@backpackerhh
backpackerhh / core-set.sql
Last active March 17, 2024 05:03
SQL - Movie-Rating Query Exercises
-- 1. Find the titles of all movies directed by Steven Spielberg.
SELECT title
FROM Movie
WHERE director = 'Steven Spielberg';
-- 2. Find all years that have a movie that received a rating of 4 or 5, and sort them in increasing order.
SELECT DISTINCT year

Keybase proof

I hereby claim:

  • I am backpackerhh on github.
  • I am backpackerhh (https://keybase.io/backpackerhh) on keybase.
  • I have a public key ASAbGAEAN_vx-W97CRO0GG3xEQReFKUk6y65OpxeZCaPugo

To claim this, I am signing this object:

@backpackerhh
backpackerhh / remove-old-snaps.sh
Created October 2, 2023 08:43
Remove old snaps in Ubuntu
#!/bin/bash
# Removes old revisions of snaps
# CLOSE ALL SNAPS BEFORE RUNNING THIS
set -eu
snap list --all | awk '/disabled/{print $1, $3}' |
while read snapname revision; do
snap remove "$snapname" --revision="$revision"
done
@backpackerhh
backpackerhh / core-set.xsl
Created May 23, 2014 19:05
XML World-Countries XSLT Exercises
<!-- 1. Return all countries with population between 9 and 10 million. Retain the structure of country elements from the original data. -->
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="country[@population &gt; 9000000][@population &lt; 10000000]">
<xsl:copy-of select="." />
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
@backpackerhh
backpackerhh / core-set.xml
Created May 23, 2014 19:03
XML World-Countries XPath and XQuery Exercises
<!-- 1. Return the area of Mongolia. -->
<!-- Reminder: To return the value of an attribute attr, you must use data(@attr), although just @attr may be used in comparisons. You will need to remember this for some later questions as well. -->
//country[@name = "Mongolia"]/data(@area)
<!-- 2. Return the names of all cities that have the same name as the country in which they are located. -->
//city[name = parent::country/data(@name)]/name
@backpackerhh
backpackerhh / spanish_postal_code_validator.rb
Last active September 16, 2019 15:53
Spanish postal code validator for Rails
# Usage:
#
# class MyClass < ActiveRecord::Base
# ...
# validate :postal_code, spanish_postal_code: true # default message
# validate :postal_code, spanish_postal_code: { message: '<Your message>' } # custom message
# ...
# end
#
class SpanishPostalCodeValidator < ActiveModel::EachValidator
@backpackerhh
backpackerhh / countries.xml
Created May 4, 2014 11:16
XML - DTD Exercises
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE countries [
<!ELEMENT countries (country*)>
<!ELEMENT country ((language|city)*)>
<!ATTLIST country name CDATA #REQUIRED population CDATA #REQUIRED area CDATA #REQUIRED>
<!ELEMENT language (#PCDATA)>
<!ATTLIST language percentage CDATA #REQUIRED>
<!ELEMENT city (name, population)>
<!ELEMENT name (#PCDATA)>
@backpackerhh
backpackerhh / subdomain_validator.rb
Last active December 21, 2018 10:36
Subdomains validation, inspired by Matthew Hutchinson.
# Each subdivision can go down to 127 levels deep, and each DNS label can contain up to 63 characters,
# as long as the whole domain name does not exceed a total length of 255 characters.
class SubdomainValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
return unless value
reserved_names = %w[admin beta blog ftp imap mail pop pop3 sftp smtp ssl www]
reserved_names += options[:reserved] if options[:reserved]
object.errors[attribute] << 'cannot be a reserved name' if reserved_names.include?(value.downcase)