Skip to content

Instantly share code, notes, and snippets.

View Electron-libre's full-sized avatar

Cedric Brancourt Electron-libre

View GitHub Profile
@Electron-libre
Electron-libre / conf_wrapper.ex
Last active January 23, 2018 10:15
elixir_config_wrapper.ex
defmodule Gifnoc.Config do
@spec get(atom, atom, term | nil) :: term
def get(app, key, default \\ nil) when is_atom(app) and is_atom(key) do
case read_config(Application.get_env(app, key)) do
nil -> default
val -> val
end
end
def read_config({:system, var_name}), do: System.get_env(var_name)
module Analysis.GobanState exposing (GobanState, Intersection(..), Stone(..), blank, addStone)
import Matrix exposing (Matrix, repeat, set)
type alias GobanState = Matrix Intersection
type Intersection = Free | Stone
type Stone = Black | White
blank : Int -> GobanState
@Electron-libre
Electron-libre / puma.rb
Created February 22, 2017 10:51
Puma with auto worker count
pidfile ENV.fetch('PUMA_PID_PATH','/tmp/pid')
# Should not be a solution
worker_timeout Integer(ENV.fetch('PUMA_WORKER_TIMEOUT', 60))
# Default worker ( OS processes ) is set to 4 ( most machines has 4 cores )
# But adjust depending on context ( how much CPU are given to my container ? )
current_cpu_count = begin
Integer(`nproc`) - 1
rescue
4
end
@Electron-libre
Electron-libre / extend_on_demand.rb
Created September 16, 2016 10:15
Extend on demand
# this wont work for immediate values as Fixnum Symbols but there may be workaround
#
module Doublable
def self.extended(base)
case base
when ::Array
base.extend Doublable::Array
when ::String
base.extend Doublable::String

Running long jobs on remote machine with docker-machine

Use the -d flag with run command.

EG:

eval $(docker-machine env feature)
docker-compose -f delivery.docker-compose.yml run -d rails rake db:seed
@Electron-libre
Electron-libre / any_spec.rb
Last active August 29, 2015 14:05
How to chain message expectation ?
require 'spec_helper'
describe Documents::PaymentSummaryPresenter do
describe 'deal_paid_deposits' do
let(:deal) {double()}
let(:deposit) {double()}
let(:presenter) { Documents::PaymentSummaryPresenter.new(deal) }
before(:each) do
deal.stub_chain(:deposits, :paid) {[deposit]}
end
@Electron-libre
Electron-libre / partner.rb
Created June 19, 2014 09:11
Bintje model example
class OpenObject::Partner
include OpenObjectModel
set_open_object_model 'res.partner'
## Where user context is the trinity : {uid: Int , dbname: String , pwd: String}
def my_custom_method(user_context)
OpenObject.rescue_xmlrpc_fault do
response = self.class.connection(user_context).execute(self.class.open_object_model, 'remoteOpenObjectMethod', self.id.to_i)
OpenObject::BackendResponse.new(success: true, content: response)
end
@Electron-libre
Electron-libre / funny_to_read_spec.rb
Created February 17, 2014 14:03
This Rspec Test is funny daer, hem .. read
describe "link_with" do
let (:mirror) {create(:relation_kind, label: 'mirror')}
let (:rorrim) {create(:relation_kind, label: 'rorrim')}
it 'link relation kind both ways' do
mirror.link_with(rorrim)
expect(mirror.mirror_relation_kind).to eql rorrim
expect(rorrim.mirror_relation_kind).to eql mirror
end
end
@Electron-libre
Electron-libre / pg_array_hsotre_fix.rb
Created November 7, 2013 07:59
Include this snippet into your model in order to use hstore in array with postgres and rails 4.0 ( fix is pending on the rails side )
module PgArrayHstoreFix
def self.included(base)
base.class_eval do
before_save :serialize_array_hash
end
def serialize_array_hash
self.class.attribute_names.each do |attribute|
column_definition = self.column_for_attribute(attribute)
if column_definition.array && column_definition.type == :hstore
@Electron-libre
Electron-libre / person_policies_spec.rb
Created October 16, 2013 14:28
Test Policies with pundit
require 'spec_helper'
describe PersonPolicy do
subject { PersonPolicy }
let(:person) { create(:person) }
let(:user) { create(:valid_user) }
context 'given user\'s role activities' do