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 / 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 / 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
@backpackerhh
backpackerhh / _modal.css.scss
Last active August 29, 2015 14:00
Modals in Rails with jQuery, Haml, SASS & Twitter Bootstrap 2.3+
// Some basic styles
.modal {
width: 660px;
.modal-header {
.close {
margin: 0;
padding: 0;
&:hover { opacity: 1; }
@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)
@backpackerhh
backpackerhh / metaprogramming.rb
Last active August 29, 2015 13:56
Dynamic methods with dynamic instances variables and memoization
%w(a b c).each do |var|
define_method var do
ivar = "@#{var}"
if instance_variable_defined?(ivar)
instance_variable_get(ivar)
else
instance_variable_set(ivar, Expensive.request)
end
end
@backpackerhh
backpackerhh / have_constant.rb
Created December 25, 2013 11:56
Custom Rspec matcher for checking class or module for constant. Based on a David Chelimsky's answer on StackOverflow.
# spec/support/matchers/have_constant.rb
RSpec::Matchers.define :have_constant do |constant|
match do |owner|
([Class, Module].include?(owner.class) ? owner : owner.class).const_defined?(constant)
end
failure_message_for_should do |klass|
"expected #{klass} to have constant #{constant}"
end