Skip to content

Instantly share code, notes, and snippets.

View mabster's full-sized avatar

Matt Hamilton mabster

View GitHub Profile
@mabster
mabster / gist:3212270
Created July 31, 2012 00:28
Create farrowing for gestating sow
[Test]
public void Create_farrowing_for_gestating_sow()
{
byte parity = 1;
var farrowing = new Farrowing
{
Id = Guid.NewGuid(),
Date = new DateTime(2011, 3, 22),
Farm = new PigX.Farms.Farm(),
@mabster
mabster / gist:3203077
Created July 30, 2012 01:03
Pig Gives Birth
// the business logic for the creation of a new litter
Find the pig with the given id to make sure it's on the user's farm, female and pregnant. If not, return error.
Find the pig's most recent mating, which should be in a gestating state. If not, return error.
If it's been less than 100 days or more than 130 days since the mating, return error.
Update the mating to a successful state.
@mabster
mabster / gist:3174380
Created July 25, 2012 04:23
Entity Framework and Interfaces
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
namespace ConsoleApplication5
{
public interface IFoo
{
@mabster
mabster / gist:3030502
Created July 2, 2012 02:05
SpamProbability Calculation
// if this is not a mention or I follow you, this won't be spam
if (IsMention && !_host.Friends.Any(u => u.ScreenName == User.ScreenName))
{
SpamProbability += 0.1;
// if they haven't tweeted much, good chance
if (item.User.StatusCount < 100) SpamProbability += 0.2;
// if they're a recent user, also a sign
if (item.User.CreatedAt > DateTime.Today.Subtract(TimeSpan.FromDays(7))) SpamProbability += 0.2;
@mabster
mabster / gist:2976454
Created June 23, 2012 02:47
Unfocused, Selected ListBoxItem Style
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox x:Name="foo">
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Orange" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
// in MainWindow.xaml:
<Grid>
<ListBox ItemsSource="{Binding Items}" />
</Grid>
// in MainWindow.xaml.cs
public MainWindow()
{
@mabster
mabster / gist:2851282
Created June 1, 2012 11:11
IComparable with Fody?
// before
public class Foo : IComparable<Foo>
{
int Id { get; set; }
public int CompareTo(Foo other)
{
if (other == null) return -1;
return this.Id.CompareTo(other.Id);
@mabster
mabster / gist:2850198
Created June 1, 2012 08:13
Awaiting a call in an anonymous type initializer
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
async void MainWindow_Loaded(object sender, RoutedEventArgs e)
@mabster
mabster / gist:2435527
Created April 21, 2012 08:11
Grammar bot v0.1
static void Main(string[] args)
{
var pattern = new Regex(@"(?:((c|sh|w)ould|might)\s+)of", RegexOptions.IgnoreCase);
var client = new TwitterAnonymousClient();
long? last = null;
while (true)
{
client.SearchAsync("\"should of\" OR \"would of\" OR \"could of\" OR \"might of\" -RT", since: last).ContinueWith(t =>
{
@mabster
mabster / twitterauthenticator.cs
Created April 10, 2012 08:10
TwitterAuthenticator.AuthenticateUser backport attempt
public static async Task<WebAuthenticationResult> AuthenticateUser(string twitterClientID, string twitterCallbackUrl
{
if (string.IsNullOrWhiteSpace(twitterClientID))
throw new ArgumentException("TwitterClientID must be specified", twitterClientID);
if (string.IsNullOrWhiteSpace(twitterCallbackUrl))
throw new ArgumentException("TwitterCallbackUrl must be specified", twitterCallbackUrl);
if (string.IsNullOrWhiteSpace(twitterClientSecret))
throw new ArgumentException("TwitterClientSecret must be specified", twitterClientSecret);
var sinceEpoch = (DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());