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
krisleech / define_method versus def.md
Created Jul 18, 2019
Which is easier to read / maintain?
View define_method versus def.md

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
    send(attr).present?
  end
end
@krisleech
krisleech / event_bus.rb
Last active Jul 16, 2019
Simple Event Bus based on Wisper
View event_bus.rb
class EventBus
def initialize
@publisher = Class.new do
include Wisper::Publisher
public :broadcast
end.new
end
def subscribe(listener, options = {})
@publisher.subscribe(listener, options)
@krisleech
krisleech / HOWTO.md
Last active Jul 10, 2019
in-memory belongs_to model
View HOWTO.md

model

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

  def site=(new_site)
    @site = new_site
View HOWTO.md

Size of directory:

du -h --summarize /archive/production/files/document_store/2019.07.02.11.58.28

92GB

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 {
content:{
text: "[" + request.content.message + "](" + request.content.url + ")",
attachments: [
{
collapsed: false,
@krisleech
krisleech / defaults.md
Last active Jun 27, 2019
dry-struct defaults
View defaults.md

As a default in the model

class MyModel < Sry::Struct
  attribute :created_at, ::Types::Strict::Time.default(-> _ { Time.now }.freeze)
  # attribute :created_at, ::Types::Strict::Time.default(Proc.new { Time.now }.freeze)
  # attribute :created_at, ::Types::Strict::Time.default(lambda { |_| Time.now }.freeze)
end
@krisleech
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
View rsync_ssh.md

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
krisleech / how_to_copy_files_to_another_server.md
Last active Jun 19, 2019
[Ansible] Copying file to another server
View how_to_copy_files_to_another_server.md

Create some files (via templating) on the server

- name: Create backup configuration files
  template:
    src: "{{ item.type }}.yml.j2"
    dest: "/tmp/{{ item.project }}_{{ item.env }}_{{ item.type }}.yml"
  with_items:
    - "{{ backup }}"
@krisleech
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.