Skip to content

Instantly share code, notes, and snippets.

View nhh's full-sized avatar
🏠
Working from home

Niklas nhh

🏠
Working from home
View GitHub Profile
module Markdown
module Fixer
# Since gsub returns a replaced string, we can chain the methods together
# without yielding the module itself
def self.fix_all(markdown)
markdown.gsub('title', '"title"')
.gsub('hr', 'hhr')
.gsub('\r\n', '\n')
module Markdown
module Fixer
def self.fix(markdown, pattern=@fix_all_pattern)
pattern.map do |key, value|
markdown.gsub!(key.to_s, value.to_s)
end
markdown
end
@nhh
nhh / integer.rb
Last active March 16, 2018 17:01
Best implementation of divisible by
class Integer
def divisible_by?(number)
self % number == 0
end
end
irb(main):054:0> 1.divisible_by? 1
=> true
irb(main):055:0> 1.divisible_by? 2
=> false
@nhh
nhh / AppRoutes.ts
Last active May 28, 2018 14:20
AppRoutes.ts
import { Routes, RouterModule } from "@angular/router";
import { NgModule } from '@angular/core';
import { AdminComponent } from './component/admin/admin.component';
import { IsAuthenticatedGuard } from '../shared/guard/is-authenticated.guard';
const routes: Routes = [
{
path: 'admin',
canActivate: [IsAuthenticatedGuard],
component: AdminComponent,
@nhh
nhh / IsAuthenticatedGuard.ts
Last active May 28, 2018 14:24
IsAuthenticatedGuard
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild} from '@angular/router';
import { Router} from "@angular/router";
import { Observable } from 'rxjs';
import { AuthService } from '../service/auth/auth.service';
@Injectable({
providedIn: 'root'
})
export class IsAuthenticatedGuard implements CanActivate {
@nhh
nhh / IsAuthenticatedGuard.ts
Last active May 29, 2018 09:37
Authenticated Guard for nested views. (child views)
import { Injectable } from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild} from '@angular/router';
import {Observable} from 'rxjs';
import {AuthService} from '../service/auth/auth.service';
@Injectable({
providedIn: 'root'
})
export class IsAuthenticatedGuard implements CanActivate, CanActivateChild {
@nhh
nhh / App.routing.ts
Created May 29, 2018 09:35
Routing with secured child views
import { Routes, RouterModule } from "@angular/router";
import { NgModule } from '@angular/core';
import { DashboardComponent } from './component/dashboard/dashboard.component';
import { SettingsComponent } from './component/settings/settings.component';
import { IsAuthenticatedGuard } from '../shared/guard/is-authenticated.guard';
const routes: Routes = [
{
path: 'admin',
canActivate: [IsAuthenticatedGuard],
@nhh
nhh / log.ts
Created July 26, 2018 06:58
Typescript method decorator
export default function log(targetObject: Object, targetMethodName: string | symbol, targetMethod: TypedPropertyDescriptor<Function>) {
return {
value: function( ... args: any[]) {
if(targetMethod.value) {
const result = targetMethod.value.apply(targetObject, args);
return result + 1;
}
}
}
}
public class Server implements Runnable {
private DatagramSocket socket;
private boolean running;
@Override
public void run() {
running = true;
try {
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="720" prefWidth="1280" fx:controller="dev.nhh.echoclient.controller.LayoutController" xmlns:fx="http://javafx.com/fxml" stylesheets="@layout.css">
<children>
<!-- Top Menu -->
<fx:include GridPane.rowIndex="0" source="/menu/menu.fxml"></fx:include>