Skip to content

Instantly share code, notes, and snippets.

View ahmadshah's full-sized avatar
:octocat:
Yo!

Ahmad Shah Hafizan Hamidin ahmadshah

:octocat:
Yo!
View GitHub Profile
@ahmadshah
ahmadshah / node.ex
Created September 21, 2016 07:14
Fetch node from map using dot notation in elixir
defmodule Node do
defmacro fetch_node(map, node) do
quote do
String.split(unquote(node), ".")
|> Enum.reduce(unquote(map), fn(n, m) -> Map.get(m, n) end)
end
end
end
<?php
namespace App\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Laravel\Lumen\Routing\Controller as BaseController;
class Controller extends BaseController {
public function __construct()
{
@ahmadshah
ahmadshah / randomizer.ex
Created September 1, 2016 10:28
Random string in elixir
defmodule Randomizer do
@moduledoc """
Random string generator module.
"""
@doc """
Generate random string based on the given legth. It is also possible to generate certain type of randomise string using the options below:
* :all - generate alphanumeric random string
* :alpha - generate nom-numeric random string
@ahmadshah
ahmadshah / README.md
Last active January 6, 2021 15:21
Ecto Soft Delete

Soft Delete Ecto Repo

The goal is to support soft delete functionality in Ecto.Repo. With the suggestion by @imranismail, another repo is created and the remaining functionalities are delegate to the original MyApp.Repo.

The new repo get/2 and all/1 functions will exclude the soft deleted record by default. delete/1 and delete_all/1 will update the delete_at column by default instead of deleting.

Example

MyApp.Repo.get(MyApp.User, 1) //will return nil if record is in soft delete state
@ahmadshah
ahmadshah / authorize.rb
Last active July 29, 2016 10:43
Access Control List
module Guard
class Authorize
attr_reader :user, :roles
def initialize(user)
build_user_roles(user)
end
def authorities(role)
raise StandardError, "User is not associated to #{role} role" unless self.is? role
@ahmadshah
ahmadshah / metable.rb
Created July 19, 2016 17:58
Metable Concern for Rails ActiveRecord
module Metable
extend ActiveSupport::Concern
included do
serialize :meta, JSON
end
def is_metable?
has_attribute?(:meta)
end
@ahmadshah
ahmadshah / componentA.vue
Created June 24, 2016 07:49
Vuejs Event Emitter
<template>
<button @click="emitEvent">EVENT</button>
</template>
<script>
import { EV } from './events'
export default {
methods: {
emitEvent() {
$source = ['1:8', '2:8', '1:9', '2:9', '3:1'];
$cleanArray = [];
function split_source($str) {
$exp = explode(':', $str);
return $exp;
}
foreach ($source as $s) {
@ahmadshah
ahmadshah / db-backup.sh
Last active August 29, 2015 14:10
MySQL full backup
#!/bin/bash
#Database Connection
DBUSER=""
DBPASSWORD=""
DBNAME=""
#Location to save
BACKUPDIR="/opt/backup/database"
BACKUPPATH=$(date +"%m-%Y")
@ahmadshah
ahmadshah / AbstractableNotifier.php
Last active August 29, 2015 14:07
Email notifier
<?php namespace Support\Notifier;
use Model\User;
use Illuminate\Support\Arr;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
abstract class AbstractableNotifier {
/**
* Expression language component