Skip to content

Instantly share code, notes, and snippets.

View draveness's full-sized avatar
🌟
𝚊𝚋𝚜𝚝𝚛𝚊𝚌𝚝𝚒𝚘𝚗 & 𝚌𝚘𝚖𝚙𝚕𝚎𝚡𝚒𝚝𝚢

Draven draveness

🌟
𝚊𝚋𝚜𝚝𝚛𝚊𝚌𝚝𝚒𝚘𝚗 & 𝚌𝚘𝚖𝚙𝚕𝚎𝚡𝚒𝚝𝚢
View GitHub Profile
@draveness
draveness / 0_reuse_code.js
Created January 14, 2016 04:58
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@draveness
draveness / fishhook-test-viewcontroller
Last active September 1, 2016 02:58
Using fishhook to change socket layer function and failed because of recursive call
#import "ViewController.h"
#import <fishhook.h>
@interface ViewController () <NSURLSessionDataDelegate>
@end
@implementation ViewController
int (*origianl_connect)(int, const struct sockaddr *, socklen_t);
require 'csv'
require 'uri'
require 'pry'
csv_text = File.read('1.csv')
csv = CSV.parse(csv_text, :headers => true)
hashes = []
csv.each do |row|
hash = row.to_hash
old = hash["old"]
//
// Token.swift
// XProject
//
// Created by Draveness on 05/04/2017.
// Copyright © 2017 Draveness. All rights reserved.
//
import Foundation
import RbSwift

Keybase proof

I hereby claim:

  • I am Draveness on github.
  • I am draven (https://keybase.io/draven) on keybase.
  • I have a public key whose fingerprint is 62CA 63CD FC29 5526 5B5E 9141 30B1 E305 0480 F92E

To claim this, I am signing this object:

def embeds_many_to_has_many(parent, child)
child_key_name = child.to_s.underscore.pluralize
parent.collection.find({}).each do |parent_document|
next unless parent_document[child_key_name]
parent_document[child_key_name].each do |child_document|
new_child = child_document.merge(
"#{parent.to_s.underscore}_id": parent_document['_id']
)
child.collection.insert_one new_child
end
@draveness
draveness / monitor_database_record_creation.rb
Last active September 29, 2017 08:52
Monitor FactoryGirl record creation with ActiveSupport notifications
# source: https://robots.thoughtbot.com/debugging-why-your-specs-have-slowed-down
# spec/spec_helper.rb
config.before(:each, :monitor_database_record_creation) do |example|
ActiveSupport::Notifications.subscribe("factory_girl.run_factory") do |name, start, finish, id, payload|
$stderr.puts "FactoryGirl: #{payload[:strategy]}(:#{payload[:name]})"
end
end
@draveness
draveness / database_transformer.rb
Last active September 29, 2017 08:57
Export MongoDB to csv files and import them into MySQL with mysqldump
module DatabaseTransformer
def self.import(collection, fields, columns)
mongo_export = <<-EOF
mongoexport --host #{hostname} --username #{username} \
--password #{password} \
--authenticationDatabase #{auth_source} \
--db #{database} --collection #{collection} \
--type=csv \
--fields #{fields.join(',')} \
--out #{collection}.csv
@draveness
draveness / database_transformer.rb
Last active October 7, 2017 15:08
Export MongoDB collection to ActiveRecord Model directly. Used for MongoId to ActiveRecord migration.
# frozen_string_literal: true
require 'colorize'
module DatabaseTransformer
attr_reader :database
def initialize(database)
@database = database
end
@draveness
draveness / relation_builder.rb
Last active October 7, 2017 15:55
After export all the record from MongoDB, use this to build relation in all records. You need to handle many to many relations by yourself
# frozen_string_literal: true
require 'colorize'
module RelationBuilder
attr_reader :constants
def initialize(constants = [])
if constants.present?
@constants = constants