Skip to content

Instantly share code, notes, and snippets.

View we4tech's full-sized avatar

Hossain Khan we4tech

View GitHub Profile
@we4tech
we4tech / lazy_attribute.rb
Created December 30, 2013 08:39
Define attribute which value is loaded and stored lazily. Ie. You want to get some data from a 3rd party API call but instead of setting them explicitly you want to get those on demand. Here are few lines of code to get it workable.
module Concerns
module LazyAttribute
extend ActiveSupport::Concern
included do
class_attribute :lazy_attributes
end
module ClassMethods
def attr_lazy(name, &logic_impl)
@we4tech
we4tech / rthread_importer.rb
Last active December 31, 2015 11:59
An illustration how we can use ruby thread for accelerating our normal RAKE tasks. (intended for posting in a local Ruby developers group https://www.facebook.com/groups/rubydevs/)
namespace :somestuffs do
desc 'Importing some big stuff that requies lotta network stuff'
task :import
file, tasks = ENV['FILE'], Queue.new
# Parse out CSV file and retrieve each row and queue them up
# for further processing.
#
# Keeping it a thread allows us to let this process continue while other
# threads are not in RUNNING state.
@we4tech
we4tech / as_active_merchant.rb
Created October 9, 2013 07:15
Simple written "Model Concern" for adding active merchant
# Integrate with ActiveMerchant and Expose active merchant related methods.
#
# This module also includes Validator for ActiveMerchant.
module Concerns::AsActiveMerchant
extend ActiveSupport::Concern
included do
validates_with Validators::ActiveMerchant, if: :cc_number_is_changed?
end
@we4tech
we4tech / with_default_mongoid_methods.rb
Last active December 24, 2015 06:59
What if, we've more pictorial relationship syntax than syntactical sugar ?
class Message
include Mongoid::Document
include Mongoid::Timestamps
field :subject, type: String
field :content, type: String
belongs_to :replied_to, class_name: 'Message', inverse_of: :replies, touch: true
belongs_to :sender, class_name: 'User', inverse_of: :sent_messages
has_many :replies, class_name: 'Message', inverse_of: :replied_to
@we4tech
we4tech / exp_factory_method.rb
Created September 21, 2013 05:48
Instead of relying on 3rd party service, we could wrap them in bridge or expose through factory or facade
def handle_action_created(action)
if action.from.kind_of?(Source)
$redis.lpush(
"#{self.class.name}:#{self.id}",
[action.from.node.id, action.from.node.neo_id].join(":")
)
end
end
# Perhaps we could do
@we4tech
we4tech / american_style.rb
Created September 21, 2013 05:37
Pasta in ruby mixin
module AmericanStyle
extend ActiveSupport::Concern
included do
has_many :american_ingredients
end
module ClassMethods
def macaroni_cheese; ... end
end
@we4tech
we4tech / after_refactor_order_service.rb
Last active December 21, 2015 20:39
Just an example how to refactor and separate code and their concerns.
class OrderService
class << self
def find_or_create_purchase_for_order(order, credit_card_id, shipping_fee = 0.0)
user, vendor = order.buyer, order.seller
purchase = Purchase.find_by_user_id_and_order_id(user.id, order.id)
return purchase if purchase.present?
create_purchase order, credit_card_id, shipping_fee
end
@we4tech
we4tech / twitter_client_patch.rb
Created May 31, 2013 06:43
Twitter Client with control loop, If any too many requests error raised. It'll sleep for a certain time period and will wake up and continue from last batch. This is an addition to Twitter::Client 'gem twitter'
# Twitter Client with control loop, If any too many requests error raised.
# It'll sleep for a certain time period and will wake up and continue from last batch.
#
# This is an addition to Twitter::Client 'gem twitter'
module BeatDeckMachine::Scrapers::OAuth::TwitterClientPatch
MAX_ATTEMPTS = 20
extend ActiveSupport::Concern
@we4tech
we4tech / thread_pool.rb
Last active December 16, 2015 19:49
Thread pool, and separate mongo client with each thread.
class ThreadPool
attr_accessor :size, :pool
def logger
BeatDeckMachine::logger
end
def initialize(size)
@size = size
@jobs = Queue.new
@we4tech
we4tech / db.bash
Created January 24, 2013 05:59
Simple bash script to quickly import an existing schema to a fixed database or get quick access to mysql console.
#!/bin/bash
echo "---------------------------------------"
echo " Welcome to Quick Schema Importer "
echo "---------------------------------------"
ROOT=$PWD
CONF=""