Skip to content

Instantly share code, notes, and snippets.

Kris Leech krisleech

Block or report user

Report or block krisleech

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
krisleech / define_method versus
Created Jul 18, 2019
Which is easier to read / maintain?
View define_method versus

I need some methods to return true/false if a attribute had a value. So we have a date attribute #invited_on and I want an #invited? method to return true if the date is present.

There are a few dates for which I need this. My first attempt:

%w(invited_on selected_on confirmed_on ready).each do |attr|
  define_method attr.gsub('_on', '') + '?' do
krisleech / event_bus.rb
Last active Jul 16, 2019
Simple Event Bus based on Wisper
View event_bus.rb
class EventBus
def initialize
@publisher = do
include Wisper::Publisher
public :broadcast
def subscribe(listener, options = {})
@publisher.subscribe(listener, options)
krisleech /
Last active Jul 10, 2019
in-memory belongs_to model


class Study < ActiveRecord::Base
  def site
    @site ||= PrefixedSites.find(site_identifier)

  def site=(new_site)
    @site = new_site

Size of directory:

du -h --summarize /archive/production/files/document_store/2019.


Archive and compress:

View rocketchat_webhook_script.js
class Script {
process_incoming_request({ request }) {
if(request.content.event.request == undefined) { url = 'n/a' } else { url = request.content.event.request.url }
return {
text: "[" + request.content.message + "](" + request.content.url + ")",
attachments: [
collapsed: false,
krisleech /
Last active Jun 27, 2019
dry-struct defaults

As a default in the model

class MyModel < Sry::Struct
  attribute :created_at, ::Types::Strict::Time.default(-> _ { }.freeze)
  # attribute :created_at, ::Types::Strict::Time.default( { }.freeze)
  # attribute :created_at, ::Types::Strict::Time.default(lambda { |_| }.freeze)
krisleech / events.rb
Created Jun 25, 2019
Repo / Model with Sequel, SQLite and Dry-Struct #ruby
View events.rb
require 'app'
require 'dry-struct'
require 'types'
class Events
def self.migrate!
return if db.table_exists?(table_name)
db.create_table table_name do
String :project, null: false

Using rsync from cron on server A to pull files from server B via ssh.

Server A has a passwordless private key, the public key is in authorized_keys on Server B.

I can ssh from Server A to Server B.

To prevent use of ForwardAgent specific no configuration file:

ssh -F /dev/null -v admin@servera
krisleech /
Last active Jun 19, 2019
[Ansible] Copying file to another server

Create some files (via templating) on the server

- name: Create backup configuration files
    src: "{{ item.type }}.yml.j2"
    dest: "/tmp/{{ item.project }}_{{ item.env }}_{{ item.type }}.yml"
    - "{{ backup }}"
krisleech / sort_and_drop.rb
Created Jun 18, 2019
sort, drop, take #ruby
View sort_and_drop.rb
[1,2,3,4,5,6,7].drop(2) # => [3,4,5,6,7]
[1,2,3,4,5,6,7].take(2) # => [1,2]
[1,2,3,4,5,6,7].reverse.take(2) # => [5,7]
[1,2,3,4,5,6,7].reverse.drop(2) # => [5, 4, 3, 2, 1]
# sorting asc and desc
You can’t perform that action at this time.