Skip to content

Instantly share code, notes, and snippets.

View sumitasok's full-sized avatar

Sumit. M. Asok sumitasok

View GitHub Profile
@sumitasok
sumitasok / user.rb
Created April 24, 2012 09:35
accepts_nested_attributes_for - Rails association management using single form submit.
# model file
class User < ActiveRecord::Base
has_many :books
accepts_nested_attributes_for :books, :reject_if => lambda {|book| book[:name].blank? }, :allow_destroy => true
end
# :reject_if => it passes the whole hash passed from browser with book details into this Proc(lambda) and iterates over each book hash. The book record is not created if the condition given in the lambda block is satisfied.
# :allow_destroy => if set to true, then the book record that was passed from browser, while submitting User form, with a key-value pair :_destroy => 1 (or '1' or true or "true") will be destroyed
@sumitasok
sumitasok / interfaces__migrator.go
Last active September 9, 2021 03:22
sample of how to do database table creation and deletion for running tests and then cleaning up.
package migration
type Migrator interface {
Migrate() error
}
@sumitasok
sumitasok / mov_to_mp4_convertor.sh
Last active October 19, 2020 08:06
Convert mov files in a folder to mp4 using ffmpeg
# cd to the folder where you have your .mov video files.
find . -name \*.mov -print0 | xargs -0 -I{} ffmpeg -i {} -f mp4 -vcodec libx264 -preset fast -profile:v main -acodec aac -vf scale=1024:576 {}.mp4
@sumitasok
sumitasok / band-info.json
Last active May 20, 2020 04:57
Sample GeoJson
{
'type': 'RGB'
'bandInfo': {
0 : { 'type': '...', 'colorinterpreter': 'R' }
1 : { 'type': '...', 'colorinterpreter': 'G' }
}
'cogInfo': {}
'mapTile': {}
}
@sumitasok
sumitasok / Dockerfile
Created June 16, 2019 07:08 — forked from perrygeo/Dockerfile
Minimal debian image with Python 3.6 and geo python tools
FROM python:3.6-slim-stretch
ADD requirements.txt /tmp/requirements.txt
RUN apt-get update && \
apt-get install -y \
build-essential \
make \
gcc \
locales \
@sumitasok
sumitasok / test.go
Created November 28, 2018 01:39
Golang Test snippet
package models_test
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMeta_ToJson(t *testing.T) {
assert := assert.New(t)
@sumitasok
sumitasok / class_var3.rb
Last active June 13, 2018 18:39
Understanding the Ruby scopes
# Class variables with @ are accessible from class methods!
class MyClass
@var = 2
def self.square
@var *= @var
end
end
MyClass.square
@sumitasok
sumitasok / build.sh
Last active June 13, 2018 08:22
Docker shell commands
docker build -t pandas_app:latest .
# * 100,000,000-line file generated by seq 100000000 > test.in
# * Reading lines 50,000,000-50,000,010
# * Tests in no particular order
# * real time as reported by bash's builtin time
# https://unix.stackexchange.com/questions/47407/cat-line-x-to-line-y-on-a-huge-file
awk 'NR >= 57890000 && NR <= 57890010' /path/to/file
awk 'NR < 57890000 { next } { print } NR == 57890010 { exit }' /path/to/file
tail -n+50000000 test.in | head -n10
@sumitasok
sumitasok / ruby-fake-a-web-response
Last active May 17, 2018 12:54
Snippet to fake a web response for testing the response.
req = RestClient::Request.new(url: "www.google.com", method: 'get')
net_http_resp = Net::HTTPResponse.new(1.0, 200, "OK")
net_http_resp.add_field 'Set-Cookie', 'Monster'
resp = RestClient::Response.create(response_body.to_json, net_http_resp, req)