Skip to content

Instantly share code, notes, and snippets.

View mathifonseca's full-sized avatar

Mathi Fonseca mathifonseca

View GitHub Profile
@mathifonseca
mathifonseca / AlfredDemo.md
Last active July 2, 2019 18:35
AlfredDemo

Pay with Cash / Credit Card

  • Shop API-KEY -> EDGDgFU21pZD6VBpJFMX635y5Gh8vyLYiNAmh2Y5qn7Be3wPp2dNFQ9R
  • Platform API-KEY -> ww7Tpkha3jxE6eJgmi74xG89zTWCSh2Pw7wVELC6t7wb6MXE8Bbsjkuj

STEP 1 - Create Purchase

POST /v2/alfred/purchase

@mathifonseca
mathifonseca / intro_to_rails
Last active October 17, 2017 17:04
Intro to Rails
#crear app
rails new rottenpotatoes
cd rottenpotatoes
tree
#agregar haml a Gemfile
https://gist.github.com/e858c6b1ba260e9b0ecccdebd64cb2d7
bundle install
#mirar routes vacias
@mathifonseca
mathifonseca / BrazilianMobilePhoneValidator.groovy
Last active January 2, 2024 18:19
Brazilian mobile phone validator
/*
Anatel Documentation -> http://www.anatel.gov.br/Portal/exibirPortalPaginaEspecial.do?org.apache.struts.taglib.html.TOKEN=9594e1d11fbc996d52bda44e608bb744&codItemCanal=1794&pastaSelecionada=2984
TL;DR:
Before 2012-07-29, Brazilian mobile phones where 10 digits long, 2 of them being an area code. e.g.: 11 2222-3333
From then on, they are slowly adding a ninth digit between the area code and the user's number, like this: 11 9 2222-3333
This process will end on 2016-12-31, so if you are reading this after that date, ignore the first method and use the second one.
*/
boolean isValidBrazilianMobilePhoneBefore20161231(String phone) {
@mathifonseca
mathifonseca / GRAILS - ToString of a null param.md
Created July 7, 2015 12:28
GRAILS - ToString of a null param

I hate it. I simply hate it.

If a Grails parameter is null and you call toString() on it, it doesn't fail with a good old NullPointerException like I expected. Instead, what you get is a String with value 'null'.

The only way to get a null value when the param is null, is to use the Groovy Safe Navigation operator.

Therefore:

given:
@mathifonseca
mathifonseca / GRAILS - Assigned id for domains.md
Last active July 15, 2019 00:26
GRAILS - Assigned id for domains

If you have a Domain class with an assigned id (not the default autoincremented long), the id assignment must not be in the constructor, but in a different line.

That means, having this domain:

class State implements Serializable {
    String id
    static mapping = {
    	id column: 'id', generator: 'assigned'
 }
@mathifonseca
mathifonseca / GRAILS - Don't use this packages!.md
Created October 27, 2014 14:34
GRAILS - Don't use this packages!

This is not Grails fault, but Spring-Loaded's.

They make the assumption that you won't put any of your own code under a package called in certain ways, and caches classes under that packages, which are meant to be used only by the framework. The most significant ones for us are grails.* and groovy.*. As a result, if you do use that packages, you will get something like this when trying to reload those classes:

2014-09-26 04:21:20,945 [FileSystemWatcher: files=#100 cl=java.net.URLClassLoader@
19d86dfc] ERROR plugins.AbstractGrailsPluginManager  - Plugin [services:2.4.2] could not reload changes to file [/<path to ggts workspace>/ggts-workspace/grails-slow3/grails-app/services/slow/SomeClassService.groovy]: Cannot get property 'cacheOperationSource' on null object
Message: Cannot get property 'cacheOperationSource' on null object
   Line | Method
->> 184 | doCall in CacheGrailsPlugin$_closure4
@mathifonseca
mathifonseca / GRAILS - ConfigObject curiosity.md
Created October 8, 2014 15:00
GRAILS - ConfigObject curiosity

What happens if you look for a config key in a groovy.util.ConfigObject and it does not exist?

It should return null or an empty map. The latter is true.

But the curious thing is that the key is created if it does not exist! It seems pretty odd and useless, but they must have a reason...

Here is the code that does that in this class:

public Object getProperty(String name) {
@mathifonseca
mathifonseca / GRAILS - String and variable contatenation.md
Created October 1, 2014 01:53
GRAILS - String and variable contatenation

This seems pretty stupid, but it can happen to anyone.

This, of course, works:

boolean something = true
prinltn "The value of something is: " + something

But, this, strangely (at first) DOES NOT work:

I know it's a dangerous thing, it can generate confusion and unwanted magical happenings, but...

The Groovy Truth is what makes collections evaluate as false when they are null or empty, or Numbers evaluate as false when its value is null or zero and true otherwise.

As with many other operators and defult Groovy methods, you can add this magic to an object of your own. Just by overriding its asBoolean method.

For example, here is my Order class.

class Order {
@mathifonseca
mathifonseca / grails.md
Last active August 29, 2015 14:06
Grails

#Grails

##Objetivo Simplifica el desarrollo de aplicaciones Java. Lleva el desarrollo de aplicaciones web al siguiente nivel de abstracción. Construido sobre muchas herramientas que son usadas actualmente y sigue teniendo la flexibilidad para cambiar lo que se desee. Las configuraciones por defecto son usualmente suficientes.

Hibernate El ORM por default del mundo Java
Spring Inversion of Control (IoC) container y framework wrapper
SiteMesh Layout-rendering framework