Skip to content

Instantly share code, notes, and snippets.

View afcastano's full-sized avatar

Andres Castano afcastano

View GitHub Profile
<table>
<tr ng-repeat="item in items">
<td in-line-editable ng-model="item" model-att="name"></td>
<td in-line-editable ng-model="item" model-att="value" display-filter="currency" s-numbers-only="true"></td>
</tr>
</table>
var app = angular.module('demoApp', ['simple.ui.inLineEdit']);
@afcastano
afcastano / watch changes on URL
Created August 21, 2014 02:03
Route based authorization for Angular.js
$rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams) {
if (!authorizationService.isAuthorized($location.url()) {
// redirect to error page
$location.path('/error');
}
});
@afcastano
afcastano / Restrict Roles Directive
Created August 21, 2014 02:13
Directive layout for restricting HTML elements depending on the user roles
app.directive('restrict', function(authService){
return{
compile: function(element, attr, linker){
//attr.access is an attribute on the element with the roles that apply for that element.
var authorized = authService.isAuthorized(attr.access);
if(!authorized){
// Remove element from DOM.
}
@afcastano
afcastano / promise_test.js
Last active August 29, 2015 14:21
Promise testing
var nock = require('nock');
var assert = require('assert');
var sinon = require('sinon');
var weeklygames = require('../../app/server/weeklygames.js');
var gamesData = require('./weeklygames-data.json');
describe('weeklygames', function(){
var service;
var baseUrl = 'http://basketball.example.com';
@afcastano
afcastano / OptionalAdd1.java
Last active July 7, 2016 04:49
Add two optionals in Java
public Optional<Integer> optionalAdd(Optional<Integer> val1, Optional<Integer> val2) {
if(val1.isPresent() && val2.isPresent()) {
return Optional.of(val1.get() + val2.get());
}
return Optional.empty();
}
@afcastano
afcastano / OptionalAdd2.java
Last active July 7, 2016 04:58
Add two optionals using bind and unit functions.
public Optional<Integer> optionalAdd(Optional<Integer> val1, Optional<Integer> val2) {
return
val1.flatMap( first ->
val2.flatMap( second ->
Optional.of(first + second)
));
}
@afcastano
afcastano / ColourCount.java
Last active July 16, 2016 05:44
Operation with nested optionals
private abstract class Counter {
abstract Optional<Integer> colourCount();
}
private abstract Optional<Counter> fetchThisMonthCounter();
private abstract Optional<Counter> fetchPreviousMonthCounter();
// ************ blah blah blah
// ...
public Optional<Integer> totalColourCount() {
Optional<Counter> thisMonth = fetchThisMonthCounter();
@afcastano
afcastano / ColourCountMonadic.java
Last active July 14, 2016 13:42
Monadic way of dealing with colour count.
private abstract Optional<Counter> fetchThisMonthCounts();
private abstract Optional<Counter> fetchPreviousMonthCounts();
public Optional<Integer> totalColour() {
return fetchThisMonthCounts().flatMap(Counter::colour).flatMap(colour1 ->
fetchPreviousMonthCounts().flatMap(Counter::colour).flatMap(colour2 ->
Optional.of(colour1 + colour2)
));
}
@afcastano
afcastano / ResultFlatMap.java
Created July 7, 2016 13:00
flatMap method on the result monad.
public<U> Result<U> flatMap(Function<? super S, Result<U>> mapper) {
if (error.isPresent()) {
return fail(error.get());
}
return mapper.apply(value.get());
}