Skip to content

Instantly share code, notes, and snippets.

Avatar

Eran Kampf ekampf

View GitHub Profile
View fix_time_with_zone.rb
module ActiveSupport
class TimeWithZone
alias_method :to_int, :to_i # For some reason this is requires after upgrading to ruby 1.9.3-p429 - https://github.com/rails/rails/pull/10686
end
end
@ekampf
ekampf / nested_dict.py
Last active Aug 29, 2015
Python Nested Dictionary
View nested_dict.py
class Nil(object):
def __getitem__(self, key):
return Nil()
def __nonzero__(self):
return False
class NestedDict(dict):
def __init__(self, *args, **kwargs):
self.update(*args, **kwargs)
@ekampf
ekampf / gist:13916578123fedb60977
Last active Aug 29, 2015
Python NotificationsManager and observer
View gist:13916578123fedb60977
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class observes(object):
def __init__(self, name):
@ekampf
ekampf / RssResult.cs
Created Jan 16, 2011
ASp.NET MVC action result that renders an RSS
View RssResult.cs
public class RssResult : FileResult
{
private readonly SyndicationFeed _feed;
/// <summary>
/// Creates a new instance of RssResult
/// </summary>
/// <param name="feed">The feed to return the user.</param>
public RssResult(SyndicationFeed feed)
: base("application/rss+xml")
@ekampf
ekampf / gist:847741
Created Feb 28, 2011
A ruby snippet to sanitize Html (and specifically Microsoft Word's messy HTML) (based on https://gist.github.com/139987)
View gist:847741
# This function cleans up messy HTML that was pasted by a user to a WYSIWYG editor.
# Specifically it also handles messy Word\Outlook generated HTML while keeping its original formattings.
require 'rubygems'
require 'sanitize'
def clean_up_document(html)
elements = %w[p b h1 h2 h3 h4 h5 h6 strong li ul ol i br div pre p]
attributes = {
'a' => ['href', 'title'],
@ekampf
ekampf / gist:2000771
Created Mar 8, 2012
Basically, the has_many association controls how the "join model" (Subscription) is created and the has_many_through association controls how the :through model is created. So instead of having one has_many :subscriptions I split it to two...
View gist:2000771
has_many :subscriptions_members,
:class_name => "Subscription",
:conditions => {:kind => Subscription::SubscriptionKinds::MEMBER},
:dependent => :destroy
has_many :subscriptions_followers,
:class_name => "Subscription",
:conditions => {:kind => Subscription::SubscriptionKinds::FOLLOWER},
@ekampf
ekampf / initializer.rb
Created Mar 21, 2012
How to limit access to a certain mountable engine
View initializer.rb
class CanAccessSomeGem
def self.matches?(request)
current_user = request.env['warden'].user
return false if current_user.blank?
return < some condition on current_user >
end
end
@ekampf
ekampf / created_at_scopes.rb
Last active Dec 12, 2015
Add scopes to query by created_at\updated_at to models
View created_at_scopes.rb
module Concerns
module CreatedAtScopes
extend ActiveSupport::Concern
included do
%w(created_at updated_at).each do |prop|
verb = prop.gsub('_at', '')
scope "#{verb}_last_h", lambda { where("#{prop} >= ?", (Time.now-1.hour).to_s(:db)) }
scope "#{verb}_last_6h", lambda { where("#{prop} >= ?", (Time.now-6.hour).to_s(:db)) }
@ekampf
ekampf / resource_route.py
Last active Dec 17, 2015
webapp2.py (Google AppEgnine) Resource Route to describe RESTful resources
View resource_route.py
from webapp2 import Route
from webapp2_extras.routes import MultiRoute
from inflection import singularize, pluralize
__author__ = 'ekampf'
# pylint:disable=C0326,R0902
class ResourceRoute(MultiRoute):
@ekampf
ekampf / gist:7721226
Created Nov 30, 2013
Code to convert temperature from Arduino's temperature sensor into heatmap RGB
View gist:7721226
const int rLED = 11;
const int gLED = 9;
const int bLED = 10;
const int sensor = A0;
const float minTemp = 10.0;
const float maxTemp = 50.0;
int redV = 0;
int greenV = 0;
You can’t perform that action at this time.