Skip to content

Instantly share code, notes, and snippets.

template<typename C, C first, C last>
class EnumIterator {
private:
typedef typename std::underlying_type<C>::type val_t;
public:
EnumIterator(C const &f) : val(f) {}
EnumIterator() : val(first) {}
@craigweston
craigweston / db_migate_touch.rake
Last active March 8, 2022 14:01
Rake db migration touch command. Updates an existing migration version to latest version.
# called with: rake db:migrate:touch['123456_migration_file.rb']
namespace :db do
namespace :migrate do
desc "Touch migration - Renames file with new version and runs db:migrate:down with old version and db:migrate:up with new version"
task :touch, [:filename] => [:environment] do |t, args|
migrate_path = ActiveRecord::Migrator.migrations_path
filename = args[:filename]
next_version = ActiveRecord::Migrator.current_version.to_i + 1
@craigweston
craigweston / gist:3cc98cf0e752bc62896e
Last active April 7, 2016 14:00
$.ensure - Extending jQuery to guarantee passed in variable (of selector string, plain DOM element or jQuery instance) will be a jQuery instance without double wrapping it
// double wrapping performance test: http://jsperf.com/check-jquery-wrap
$.extend({
ensure: function(el) {
return !(el instanceof jQuery) ? $(el) : el;
}
});
// example use
function doSomethingWithEl(el) {
@craigweston
craigweston / or_scopes.rb
Last active March 26, 2020 23:22 — forked from j-mcnally/or_scopes.rb
OR'ing scopes
module ActiveRecord
module Querying
delegate :or, :to => :all
end
end
module ActiveRecord
module QueryMethods
# OrChain objects act as placeholder for queries in which #or does not have any parameter.
# In this case, #or must be chained with any other relation method to return a new relation.
@craigweston
craigweston / name_parse.rb
Created August 19, 2015 17:21
One liner that converts array of names from "Smith, Joe" format to "Joe Smith"
names = ['Mike Smith', 'McDonald, John', 'Jill Doe']
names.map { |e| e.split(/,\s*/).reverse!.join(' ') }
# => ["Mike Smith", "John McDonald", "Jill Doe"]
@craigweston
craigweston / gist:c770d3454647b4d605ee
Created May 13, 2015 15:58
Groovy - alphabet link generation
<label>All starting with:</label>
<g:each in="${'a'..'z'}" var="ch">
<g:link action="list" params="[searchLetter: ch]">${ch}</g:link> |
</g:each>