Skip to content

Instantly share code, notes, and snippets.

View jarrettmeyer's full-sized avatar

Jarrett Meyer jarrettmeyer

View GitHub Profile
@jarrettmeyer
jarrettmeyer / IPaginate.cs
Created May 29, 2012 17:20
Simple Pagination with MVC3 + Twitter Bootstrap
/// <summary>
/// Indicates that a model provides support for pagination.
/// See http://twitter.github.com/bootstrap/components.html#pagination.
/// </summary>
public interface IPaginate
{
/// <summary>
/// Gets the current page in the record set.
/// </summary>
int Page { get; }
@jarrettmeyer
jarrettmeyer / HasHttpContext.cs
Created May 25, 2012 15:26
Determine if there is a current HTTP context without referencing System.Web
public static bool HasHttpContext
{
get
{
try
{
var assembly = Assembly.Load("System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
Type httpContextType = assembly.GetType("System.Web.HttpContext");
PropertyInfo currentProperty = httpContextType.GetProperty("Current");
object value = currentProperty.GetValue(null, null);
@jarrettmeyer
jarrettmeyer / Rakefile
Created May 24, 2012 21:28
Octopress publish drafts
require "rubygems"
require "date"
# Configuration
base_dir = Dir.pwd
blog_dir = "#{base_dir}/blog"
source_dir = "#{blog_dir}/source"
posts_dir = "#{source_dir}/_posts"
git_remote = "origin"
git_branch = "master"
@jarrettmeyer
jarrettmeyer / Gemfile.lock
Created May 24, 2012 13:40
working Gemfile.lock for Octopress issue 578
GEM
remote: http://rubygems.org/
specs:
RedCloth (4.2.9)
albino (1.3.3)
posix-spawn (>= 0.3.6)
blankslate (2.1.2.4)
chunky_png (1.2.5)
classifier (1.3.3)
fast-stemmer (>= 1.0.0)
@jarrettmeyer
jarrettmeyer / email_validation.cs
Created May 16, 2012 12:05
Email validation - the point is to show that the extension method doesn't actually do anything. The real logic is in the validator class.
public class EmailValidator
{
private const string PATTERN = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$";
public static bool IsValid(string s)
{
if (string.IsNullOrEmpty(s))
return false;
// Regex from http://www.regular-expressions.info/email.html
@jarrettmeyer
jarrettmeyer / Pluralize.cs
Created August 17, 2011 17:50
Pluralization
public static class Pluralization
{
private static readonly ConcurrentDictionary<string, string> dictionary;
private static Func<string, string> defaultPluralizationRule = (s) => s.EndsWith("s") ? "es" : "s";
static Pluralization()
{
dictionary = new ConcurrentDictionary<string, string>();
}
@jarrettmeyer
jarrettmeyer / ObjectToDictionaryHelper.cs
Created January 27, 2011 15:53
C# convert an object to a dictionary of its properties
public static class ObjectToDictionaryHelper
{
public static IDictionary<string, object> ToDictionary(this object source)
{
return source.ToDictionary<object>();
}
public static IDictionary<string, T> ToDictionary<T>(this object source)
{
if (source == null)
@jarrettmeyer
jarrettmeyer / jquery.buildSelectList.js
Created December 6, 2010 15:01
jQuery buildSelectList plugin
(function ($) {
$.fn.buildSelectList = function (options) {
defaults = {
url: null, // URL for the select list options.
data: null, // Data to be passed to the URL.
value: null, // Currently selected option value.
valueProperty: "value", // JSON property for option value.
textProperty: "text", // JSON property for option text.
type: "GET", // JSON HTTP method.
useCache: false, // Allow outputs to be cached.
@jarrettmeyer
jarrettmeyer / jquery.deleteLink.js
Created December 3, 2010 15:09
jQuery deleteLink plugin
// Because I am an idiot and keep on using 'append', instead of 'push'.
Array.prototype.append = function (item) {
this.push(item);
return this;
};
// Returns true if the given item is contained in the array.
Array.prototype.contains = function (item) {
for (var i = 0; i < this.length; i++) {
if (this[i] == item) return true;