Skip to content

Instantly share code, notes, and snippets.

View aesmail's full-sized avatar
🇯🇵
日本語を勉強しています

Abdullah Esmail aesmail

🇯🇵
日本語を勉強しています
View GitHub Profile
@aesmail
aesmail / email_format_validation.ex
Last active June 25, 2020 03:04
Email format validation helper text
defmodule MyApp.Blog.Author do
use Ecto.Schema
import Ecto.Changeset
schema "authors" do
field :name, :string
field :email, :string
# ...
end
@aesmail
aesmail / control_flow.ex
Created May 29, 2020 16:12
Elixir deciding which control flow to use
# boolean values, use if
if expression, do: this(), else: that()
# non-boolean multi-value single expressions, use case
case expression do
value1 -> one()
value2 -> two()
value3 -> three()
_ -> anything_else()
end
defmodule MyModule do
# this is valid sytax. However, you can't access x and my_function inside `def`
x = 2
my_function = fn (x) -> x * 2 end
# this is valid syntax. It will replace @my_variable with 2 anywehre inside the module at compile time.
@my_variable 2
# the following line will not compile
# because only the following values are supported here:
@aesmail
aesmail / phoenix_channels_nativescript.md
Last active November 8, 2018 12:11
NativeScript + Phoenix Channels

I have been struggling (unnecessarily) to make my NativeScript app work seamlessly with Phoenix Channels.

I'm sure this is not the perfect solution, but after trying a lot of other solutions and none of them worked, this one worked for me like a charm.

I'm using:

  • macOS 10.12.6
  • phoenix 1.3.0
  • NativeScript 3.1.3
@aesmail
aesmail / Python's Decimal behaviour
Created April 27, 2016 10:17
Python: Difference when passing a number or a string to decimal.Decimal
# Scenario ONE
price = Decimal(33.000) # <-- passed in a number
factor = Decimal(0.3020)
price = price / factor
price = price * factor
price.quantize(Decimal("0.01"), rounding=ROUND_UP) # --> 33
# Scenario TWO
price = Decimal('33.000') # <-- passed in a string
factor = Decimal('0.3020')
Abdullahs-iMac-2:GM abdullah$ bundle update
Fetching gem metadata from https://rubygems.org/............
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies...
Using rake 10.4.2
Using i18n 0.7.0
Using json 1.8.3
Using minitest 5.7.0
Using thread_safe 0.3.5
def application(app, didReceiveLocalNotification: notification)
if app.applicationState == UIApplicationStateInactive
NSLog("didReceiveLocalNotification...")
url = NSURL.URLWithString("sms:12345678")
NSLog("finished creating URL...")
app.openURL(url)
NSLog("opened URL... Done.")
elsif app.applicationState == UIApplicationStateActive
App.alert(notification.alertBody)
end
# controller
def create
user = User.create(params[:user])
respond_with(user, include: :extras)
end
# nginx conf
server {
listen 80;
@aesmail
aesmail / gist:4365006
Created December 23, 2012 18:16
BackgroundLayer is not positioned in the center of the screen.
// BackgroundLayer
-(id) init {
self = [super init];
if(self != nil) {
CCSprite *backgroundImage;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
backgroundImage = [CCSprite spriteWithFile:@"background.png"];
} else {
backgroundImage = [CCSprite spriteWithFile:@"backgroundiPhone.png"];
}
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.references :blog, :null => false
t.datetime :post_date, :null => false
t.string :url, :null => false
t.string :author
t.string :title, :null => false
t.attachment :image, :null => false
t.text :body