Skip to content

Instantly share code, notes, and snippets.

@EdwardDiehl
EdwardDiehl / xml_parser.rb
Created Mar 12, 2016 — forked from kmile/xml_parser.rb
A small nokogiri xml reader DSL.
View xml_parser.rb
# A small DSL for helping parsing documents using Nokogiri::XML::Reader. The
# XML Reader is a good way to move a cursor through a (large) XML document fast,
# but is not as cumbersome as writing a full SAX document handler. Read about
# it here: http://nokogiri.org/Nokogiri/XML/Reader.html
#
# Just pass the reader in this parser and specificy the nodes that you are interested
# in in a block. You can just parse every node or only look inside certain nodes.
#
# A small example:
#
@EdwardDiehl
EdwardDiehl / download-url-to-file.rb
Created Apr 4, 2016 — forked from johnjohndoe/download-url-to-file.rb
Ruby script to download a number of files from individual URLs via HTTP/HTTPS/FTP specified in an external file.
View download-url-to-file.rb
#!/usr/bin/env ruby
#
# Ruby script to download a number of files
# from individual URLs via HTTP/HTTPS/FTP
# specified in an external file.
#
# Author: Tobias Preuss
# Revision: 2013-04-18 16:26 +0100 UTC
# License: Creative Commons Attribution-ShareAlike 3.0 Unported
@EdwardDiehl
EdwardDiehl / colors.rb
Created Apr 15, 2016 — forked from firedev/colors.rb
Colors.rb – Functional programming in Ruby example
View colors.rb
#!/usr/bin/env ruby
require 'paleta'
to_paleta = ->(color) { Paleta::Color.new(:hex, color) rescue nil }
to_div = ->(str) {
"<div style='font-family: sans-serif; width: 5em; height: 3em;
line-height: 3em; display: inline-block; text-align: center;
margin: 0.25em; border-radius: 0.25em; padding: 1em;
background: ##{str}'>
@EdwardDiehl
EdwardDiehl / functional-utils.js
Created Apr 17, 2016 — forked from bendc/functional-utils.js
A set of pure and immutable ES2015 functions aimed to make functional JavaScript more idiomatic.
View functional-utils.js
// array utils
// =================================================================================================
const combine = (...arrays) => [].concat(...arrays);
const compact = arr => arr.filter(Boolean);
const contains = (() => Array.prototype.includes
? (arr, value) => arr.includes(value)
: (arr, value) => arr.some(el => el === value)
@EdwardDiehl
EdwardDiehl / README.md
Created Jun 11, 2016 — forked from iamatypeofwalrus/README.md
A Rails 4 pluck in batches implementation
View README.md

pluck_in_batches

Sometimes you need to iterate over a ton of items and you don't want the overhead of creating AR objects out of all of them. Hell, you only need a few things! Well, #pluck has your back.

But what if you want to iterate over many tonnes of items?

Pluck in batches to the rescue!

This isn't the exact code that I use in my code base, but it is damn close.

@EdwardDiehl
EdwardDiehl / description.markdown
Created Jan 6, 2017 — forked from runemadsen/description.markdown
Reverse polymorphic associations in Rails
View description.markdown

Polymorphic Associations reversed

It's pretty easy to do polymorphic associations in Rails: A Picture can belong to either a BlogPost or an Article. But what if you need the relationship the other way around? A Picture, a Text and a Video can belong to an Article, and that article can find all media by calling @article.media

This example shows how to create an ArticleElement join model that handles the polymorphic relationship. To add fields that are common to all polymorphic models, add fields to the join model.

@EdwardDiehl
EdwardDiehl / reseed.rake
Created Jan 7, 2017 — forked from nithinbekal/reseed.rake
Rake task to reset and seed Rails database
View reseed.rake
# Originally written by Justin French (2008):
# http://justinfrench.com/notebook/a-custom-rake-task-to-reset-and-seed-your-database
#
# Modified to work with Rails 4.
desc 'Raise an error unless development environment'
task :safety_check do
raise "You can only use this in dev!" unless Rails.env.development?
end
@EdwardDiehl
EdwardDiehl / 000_postgresql_fancy_datatypes
Created Jan 15, 2017 — forked from pcreux/000_postgresql_fancy_datatypes
Postgresql fancy datatypes with Rails / ActiveRecord. Run it with `rake`!
View 000_postgresql_fancy_datatypes
# Postgresql fancy datatypes!
* array
* hstore (=~ hash)
* json
* jsonb
Philippe Creux - [@pcreux](http://twitter.com/pcreux)
View mocha_vs_jasmine.md

Почему Mocha, а не Jasmine?

Ниже будут приведены аргументы в пользу выбора Mocha

  • Высокая популярность:
    — 4m против 400k загузок в месяц

  • Высокая активность:
    — 1 890 против 1400 коммитов (всего)
    — 171 против 101 коммитов (последний год)

@EdwardDiehl
EdwardDiehl / 1-sleep-es7.js
Created Dec 21, 2017 — forked from danharper/1-sleep-es7.js
ES7's async/await syntax.
View 1-sleep-es7.js
// ES7, async/await
function sleep(ms = 0) {
return new Promise(r => setTimeout(r, ms));
}
(async () => {
console.log('a');
await sleep(1000);
console.log('b');
})()