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 / 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 / _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 / core-set.xml
Last active August 29, 2015 14:01
XML Course-Catalog XPath and XQuery Exercises
<!-- 1. Return all Title elements (of both departments and courses). -->
//Title
<!-- 2. Return last names of all department chairs. -->
//Chair//Last_Name
//Chair/*/Last_Name
@backpackerhh
backpackerhh / core-set.xsl
Created May 21, 2014 16:39
XML Course-Catalog XSLT Exercises
<!-- 1. Return a list of department titles. -->
<?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="Department">
<Title><xsl:value-of select="Title" /></Title>
</xsl:template>
</xsl:stylesheet>
@backpackerhh
backpackerhh / benchmark.rb
Last active December 10, 2015 19:36
Performance of different block invocations using Ruby 2.1.2
# Adapted from http://blog.sidu.in/2008/01/ruby-blocks-redux-ruby-190-ruby-186-and.html
require 'benchmark'
def implicit(*args)
yield args.join(' ')
end
def explicit(*args, &block)
block.call args.join(' ')
@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
@backpackerhh
backpackerhh / feature_helpers.rb
Last active March 3, 2017 09:10
Custom helpers for integration/acceptance tests
module FeatureHelpers
# Finds a file field on the page and attach a file given its path
def enhanced_attach_file(path)
field = find('input[type=file]', visible: false)
page.execute_script %($("##{field[:id]}").show())
attach_file field[:id], path.to_s
end
# Finds a file field on the page and attach a file given its path
def enhanced_attach_file(path)
@backpackerhh
backpackerhh / inflections.rb
Last active September 18, 2017 10:24
Inflections
# Be sure to restart your server when you modify this file.
# Add new inflection rules using the following format
# (all these examples are active by default):
ActiveSupport::Inflector.inflections do |inflect|
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people'
# inflect.uncountable %w( fish sheep )
inflect.plural /(or)$/i, '\1es'
@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 / 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)>