Skip to content

Instantly share code, notes, and snippets.

View pjsvis's full-sized avatar

Peter John Smith pjsvis

  • Virtual Information Systems
  • Edinburgh
View GitHub Profile
@pjsvis
pjsvis / gist:6320268
Created August 23, 2013 14:55
An angular directive to wrap Dan Grossman's excellent date range picker ref https://github.com/dangrossman/bootstrap-daterangepicker
angular.module('daterangepicker', []);
angular.module('daterangepicker').directive('dgDaterangepicker', [function () {
'use strict';
return{
restrict: 'A',
link: function (scope, elem, attrs) {
var options = attrs.options;
var parseOptions = function (options) {
if (!options) return;
@pjsvis
pjsvis / gist:7453183
Created November 13, 2013 17:42
Modified gruntfile.js from generator-cg-angular
'use strict';
var path = require('path');
var folderMount = function folderMount(connect, point) {
return connect.static(path.resolve(point));
};
module.exports = function (grunt) {
// Project configuration.
grunt.initConfig({
angular.module('app', ['jmdobry.angular-cache']).config(function ($angularCacheProvider) {
// optionally set cache defaults
$angularCacheFactoryProvider.setCacheDefaults({ options... });
}).run(function ($angularCacheFactory) {
// Create a cache here, or anywhere else. Just inject $angularCacheFactory
var newCache = $angularCacheFactory('newCache', { options... });
IEnumerable<IEnumerable<int>> GenerateCage(int num, int target, params int[] excluded)
{
return GenerateCageInner(num, target, 1, 9, new HashSet<int>(excluded));
}
private IEnumerable<IEnumerable<int>> GenerateCageInner(int num, int target, int min, int max, HashSet<int> excluded)
{
// Base case
if (num == 0)
{
@pjsvis
pjsvis / gist:780d930b87ce8ea5cc48
Created December 11, 2014 13:23
Using LINQ to simplify business rules
// Ref: http://odetocode.com/Articles/739.aspx
// Using LINQ to simplify business rules
Employee employee =
new Employee { ID = 1, Name =
"Poonam", DepartmentID = 1 };
Func<Employee, bool>[] validEmployeeRules =
{
e => e.DepartmentID > 0,
@pjsvis
pjsvis / UrlBuilder
Last active August 29, 2015 14:15
Angular Service to Build a URL with Query Params
// NOTE: Build a url with query params
angular.module('app').factory('UrlBuilder', function () {
function sortedKeys(obj) {
var keys = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
keys.push(key);
}
}
// Ref: http://stackoverflow.com/questions/3738748/create-an-array-or-list-of-all-dates-between-two-dates
void Main()
{
var requirements = GetRequirements();
// TODO: Add a margin at the start and end
// Get a range of dates
var start = requirements.Select (x =>x.DtFm).Min();
var end =requirements.Select (x =>x.DtTo).Max();
var dateRange = Enumerable.Range(0, 1 + end.Subtract(start).Days)
// Format as per flot requirements
var flotData = new[]
{
new {label = "Quantity Required", data = partAvailability.Select(x => new[] {x.TimeStamp, x.QuantityRequired})},
new {label = "Quantity Available", data = partAvailability.Select(x => new[] {x.TimeStamp, x.QuantityAvailable})}
};
// Timestamp created as follows
private static long GetJavascriptTimestamp(DateTime date)
{
@pjsvis
pjsvis / gist:65e0e5efd16eb1d4e100
Last active August 29, 2015 14:27
Lazy instantiation of SqlConnection
// Ref: [mythz](https://groups.google.com/forum/#!topic/servicestack/bb95kGpDcEo)
using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
namespace Clear2Pay.OTS.UTP.Data.Tests
{
public class Database : IDisposable
{
using System;
using System.Data;
using Dapper;
using Dapper.Contrib.Extensions;
using NUnit.Framework;
using ServiceStack.Text;
//using Dommel;
namespace DataAccess