Skip to content

Instantly share code, notes, and snippets.

@ProGM
ProGM / hash_to_dotted_path.rb
Created July 1, 2016 15:38
Hash to Dotted Path
class HashToDottedPath
def initialize(hash)
@hash = hash
end
def to_dotted_path
result = []
@hash.each do |k, v|
explore_keys(v, [k], result)
end

Convert a ruby hash to dotted path

def hash_to_dotted_path(hash, path = "")
  hash.each_with_object({}) do |(k, v), ret|
    key = path + k.to_s

    if v.is_a? Hash
      ret.merge! hash_to_dotted_path(v, key.to_s + ".")
 else
@ProGM
ProGM / video2gif.sh
Last active June 7, 2016 08:41
A script to create a gif from a video!
#!/usr/bin/env bash
#
# Usage:
# $ video2gif.sh in.avi out.gif [width]
#
# Based on http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
size=$3
if [ -z $size ]; then
size=320
@ProGM
ProGM / dig_for_objects.rb
Created April 14, 2016 11:41
An implementation for `dig` method for generic objects.
class Object
def dig(*methods)
output = self
methods.each { |method| output = output&.public_send(method) }
output
end
end
class MyClass
attr_accessor :some_attribute
@ProGM
ProGM / diff-words.sh
Created March 29, 2016 10:01
A script to run git-diff per character/words.
#!/usr/bin/env bash
if [[ $1 == 'words' ]]; then
git diff --color-words="[^[:space:]]|([[:alnum:]]|UTF_8_GUARD)+"
elif [[ $1 == 'nospace' ]]; then
git diff --color-words=.
else
git diff --color-words
fi
@ProGM
ProGM / i18n-tasks-snippets.sh
Last active April 12, 2016 20:16
A set of i18n-tasks snippets.
# Rename games.filters.* to games_sections.*
i18n-tasks data -f yaml | i18n-tasks tree-filter games.filters.* | i18n-tasks tree-rename-key *.games games_sections | i18n-tasks data-merge
# Remove games.filters.*
i18n-tasks data -f yaml | i18n-tasks tree-filter games.filters.* | i18n-tasks data-remove
# Adds a new tree
echo 'en.my.new.tree' | i18n-tasks tree-convert -f keys -t yaml | i18n-tasks tree-set-value Hello | i18n-tasks data-merge
@ProGM
ProGM / invitation.rb
Last active March 29, 2016 10:02
An example "orm" for redis.
class Invitation < RedisDataMapper
key_property :mail
key_property :type
key_format -> { |attrs| "invitation:#{attrs[:type]}#{attrs[:mail]}" }
property :token
property :mail_sent
property :accepted
@ProGM
ProGM / example.rb
Last active March 1, 2016 15:44
A patch to allow `neo4j` gem to retry when there's a `id_property` conflict.
class Article
include Neo4j::ActiveNode
include IdPropertyWithRetry
id_property :uuid, on: :id_builder, retry: 3
def id_builder
SecureRandom.urlsafe_base64(8)
end
end
@ProGM
ProGM / github_issue.js
Created February 17, 2016 10:31
Create Github issue that refers to trello card
// Add this to favourites, prefixing this with "javascript:""
window.open(encodeURI("https://github.com/:NAME/:REPO/issues/new?body=\n\n----\nRefers to " + window.location.href))