Skip to content

Instantly share code, notes, and snippets.

Avatar

Douglas Teoh dteoh

View GitHub Profile
@dteoh
dteoh / macos_no_sleep.md
Created Feb 23, 2017
Preventing macOS from sleeping
View macos_no_sleep.md

There is a built-in command line utility caffeinate that can be used to prevent macOS from sleeping.

To prevent macOS from sleeping before a process with given PID exits:

$ caffeinate -w PID

To prevent macOS from sleeping for the next N seconds:

@dteoh
dteoh / rails_5_engine_tests.md
Last active Apr 28, 2018
Adding extra test paths to Rails 5 `rails test` command
View rails_5_engine_tests.md

In Rails 4.x, you could add extra test paths to rake test by overriding the Rake task like so:

Rake::Task['test:run'].clear

namespace :test do
  Rails::TestTask.new(:run) do |t|
    paths = ['test/**/*_test.rb']
    paths << 'engines/foo_engine/test/**/*_test.rb'
@dteoh
dteoh / put_post_json_rails_5_integration_test.md
Created Jul 12, 2016
PUT or POST JSON in a Rails 5 ActionDispatch::IntegrationTest
View put_post_json_rails_5_integration_test.md

In Rails 5, the preferred base class for testing controllers is ActionDispatch::IntegrationTest.

If you have an API that receives parameters as JSON request bodies, here are some helper methods to facilitate testing:

class ActionDispatch::IntegrationTest
  def put_json(path, obj)
    put path, params: obj.to_json, headers: { 'CONTENT_TYPE' => 'application/json' }
  end
@dteoh
dteoh / hash_to_numeric.rb
Last active Mar 22, 2016
Get numeric value from hash digest
View hash_to_numeric.rb
require 'digest'
key = 'the value to hash'
num = Digest::MD5.digest(key).unpack('L').first
@dteoh
dteoh / par_checksum.md
Last active Feb 1, 2017
Checksumming backups
View par_checksum.md

par2 is not just for checksumming and recovering Usenet binaries, you can use it for your backups as well.

In my case, I wanted to make sure my RAW and JPG files don't get corrupted by bitrot.

Generating the checksum:

$ cd /the/directory/where/files/are/kept
$ par2 c something.par2 *
$ shasum -a 256 * > shasum256.txt
@dteoh
dteoh / arel_select_star_from_table.rb
Last active Dec 2, 2019
Select all columns of an Arel table
View arel_select_star_from_table.rb
users = User.arel_table
query = users.project(users[Arel.star])
View keybase.md

Keybase proof

I hereby claim:

  • I am dteoh on github.
  • I am dteoh (https://keybase.io/dteoh) on keybase.
  • I have a public key whose fingerprint is 981E AFFF 4AE6 5ED3 AC3E 401E 8498 2F30 A58D F5CB

To claim this, I am signing this object:

You can’t perform that action at this time.