Skip to content

Instantly share code, notes, and snippets.

View we4tech's full-sized avatar

Hossain Khan we4tech

View GitHub Profile
@we4tech
we4tech / export-mongo-collections.rb
Last active December 11, 2015 05:48
Export mongodb collections and their properties to HTML format
# Export collection name and their properties list in HTML format.
require 'mongo'
include Mongo
# Connect with mongodb server
client = MongoClient.new('localhost', 27017)
# Load database
@we4tech
we4tech / pre-push
Created November 9, 2015 12:42
GIT Pre push script to prevent from accidental force push to master branch
#!/usr/bin/env ruby
# Make sure you have 'colorize' gem installed already.
require 'colorize'
current_branch = `git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,'`
push_cmd = `ps -ocommand | grep "git push"`
if current_branch =~ /\/master/ && push_cmd =~ /(\-f|force|delete)/
puts '*********************************************'.colorize(:red)
@we4tech
we4tech / method.rb
Created July 24, 2012 10:47
Ruby closure method
def hola
puts 'hola'
end
def say_hola(block)
block.call
end
say_hola method(:hola)
@we4tech
we4tech / lambda-proc-diff.rb
Created July 24, 2012 10:45
ruby closure lambda proc difference
def whats_your_name?
l = Proc.new { return "Karim" }
l.call
return "Rahim"
end
#=> "Karim"
@we4tech
we4tech / lambda-example.rb
Created July 24, 2012 10:43
Ruby closure lambda example
l = lambda { |a, b| ... }
l.call('A')
#=> Error need to pass 2 arguments
@we4tech
we4tech / proc-example.rb
Created July 24, 2012 10:03
Ruby closure Proc example
def say_hi_to(block)
puts "Say hi #{block.call}"
end
say_hi_to Proc.new { "hasan" }
#=> Say hi hasan
@we4tech
we4tech / block-example.rb
Created July 24, 2012 09:58
Ruby closure - block code example
def say_hi_to(&block)
puts "Say hi #{block.call}"
end
say_hi_to { "hasan" }
#=> Say hi hasan
@we4tech
we4tech / page.rb
Created July 24, 2012 06:13
Sample active record model
class Page < ActiveRecord::Base
attr_accessible :body, :metadata, :title, :store_id
# Relationships
belongs_to :store
# Validations
validates_presence_of :title, :body, :store_id
# Scopes
@we4tech
we4tech / product.rb
Created June 20, 2012 03:40
Sample flexi-model code
class Product
include FlexiModel
flexi_field :name, :string
flexi_field :price, :decimal
set_flexi_partition_id USER.store.id
end
@we4tech
we4tech / spec_helper.rb
Created June 12, 2012 12:36
working spec_helper.rb with spork, factory_girl and devise
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'