Skip to content

Instantly share code, notes, and snippets.

@glaucocustodio
glaucocustodio / cnpj_validator.swift
Last active February 4, 2016 17:19
Swift (1.2) function to validate CNPJ
// Returns true if the given cnpj is valid
func checkIfCNPJIsValid(cnpj: String) -> Bool {
if(cnpj.isEmpty) {
return true
}
// validates format, allowed ones: 38.041.242/0001-04, 38041242000104
var formatRegex = NSRegularExpression(pattern: "^[0-9\\.\\-\\/]*[0-9]$", options: NSRegularExpressionOptions.CaseInsensitive, error: nil)
@glaucocustodio
glaucocustodio / application_helper.rb
Created November 23, 2015 17:21
Rails helper that returns if current time is contained in a time range
def current_time_between start_time, end_time
now = Time.now
start_time = start_time.split(':')
end_time = end_time.split(':')
start_time_hour = start_time.first.to_i
start_time_min = start_time.last.to_i
end_time_hour = end_time.first.to_i
end_time_min = end_time.last.to_i
@glaucocustodio
glaucocustodio / excel.pmt.formula.swift
Created May 28, 2015 18:16
Excel PMT Formula in Swift
class ExcelFormulas {
class func pmt(rate : Double, nper : Double, pv : Double, fv : Double = 0, type : Double = 0) -> Double {
return ((-pv * pvif(rate, nper: nper) - fv) / ((1.0 + rate * type) * fvifa(rate, nper: nper)))
}
class func pow1pm1(x : Double, y : Double) -> Double {
return (x <= -1) ? pow((1 + x), y) - 1 : exp(y * log(1.0 + x)) - 1
}
class func pow1p(x : Double, y : Double) -> Double {
@glaucocustodio
glaucocustodio / rails-4-2-unnest-query-in-array-columns.rb
Last active August 29, 2015 14:13
Scenario from issue with the use of postgres's unnest query in array columns on rails 4.2
# Activate the gem you are reporting the issue against.
# gem 'activerecord', '4.1.9' # test passing
gem 'activerecord', '4.2.0' # test failing
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
@glaucocustodio
glaucocustodio / Default.sublime-theme
Created January 5, 2015 16:30
Increase font size and row padding from Sublime Text 3's sidebar
// for the ST3 users who don't have the Default.sublime-theme file (which is actually the default configuration),
// the simplest procedure is:
// 1- Navigate to Sublime Text -> Preferences -> Browse Packages
// 2- Open the User directory
// 3- Create a file named Default.sublime-theme with the following content (modify font.size as required):
[
{
"class": "sidebar_label",
"color": [0, 0, 0],
"font.size": 16
# my_page.models.py
from mezzanine.blog.models import BlogCategory
class MyBlogCategory(BlogCategory):
color = models.CharField('Hexadecimal color', max_length=6, blank=True, null=True)
class Meta:
verbose_name = "My Category"
verbose_name_plural = "My Categories"
# my_page.admin.py
@glaucocustodio
glaucocustodio / gist:6039161
Last active December 20, 2015 00:09
Rails 4 and Ruby 2 date format validation
# Model
# Working in Rails 3 with Ruby 1.9.3 - always returning false in Rails 4 with Ruby 2
with_options :if => :condition? do |co|
co.validates :date_birth, :format => {:with => /[0-3]{1}[0-9]{1}\/[0-1]{1}[0-9]{1}\/[1-2]{1}[0-9]{3}/, :message => "invalid"}
# I also tried below and many others regex
#co.validates :date_birth, :format => {:with => /[1-2]{1}[0-9]{3}\-[0-1]{1}[0-9]{1}\-[0-3]{1}[0-9]{1}/, :message => "invalid"}
end
# Workaround to work in Rails 4 with Ruby 2
@glaucocustodio
glaucocustodio / gist:5985229
Last active March 20, 2016 08:50
Rails 4 , Rails Admin and Rich
# Following https://github.com/sferik/rails_admin/wiki/Theming-and-customization I have created a custom js to load rich base, in this way I get the assets included on top of page
# /assets/javascripts/rails_admin/custom/ui.js
//= require rich/base
//= require_self
# File upload at Rails 4
# How rich still uses 'attr_accessible' and I didnt want use attr_protected gem I had to overrode RichFile class to comment attr_accessible, I just created /models/rich/rich_file.rb with the same content from rich gem and commented out attr_acessible line.
@glaucocustodio
glaucocustodio / gist:5929646
Created July 4, 2013 18:59
Doorkepper and CanCan
# routes
use_doorkeeper do
controllers :applications => 'custom_applications',
:authorized_applications => 'custom_authorized_applications',
:authorizations => 'custom_authorizations'
end
# application
config.to_prepare do
# Only Applications list
@glaucocustodio
glaucocustodio / doorkeeper.pt-BR.yml
Created July 4, 2013 14:51
pt-BR translations for Doorkeeper gem
# encoding: UTF-8
pt-BR:
activerecord:
errors:
models:
application:
attributes:
redirect_uri:
fragment_present: 'não pode conter um fragemento.'
has_query_parameter: 'não pode conter uma parâmetro de query.'