Skip to content

Instantly share code, notes, and snippets.

View alexpeachey's full-sized avatar

Alex Peachey alexpeachey

View GitHub Profile
@alexpeachey
alexpeachey / ping_pong.ex
Created October 23, 2017 20:12
Process communication with Elixir
defmodule PingPong do
def start do
ping_pid = spawn __MODULE__, :ping, []
pong_pid = spawn __MODULE__, :pong, []
send ping_pid, {pong_pid, :ping}
{ping_pid, pong_pid}
end
def ping do
@alexpeachey
alexpeachey / bowling.ex
Created October 23, 2017 20:10
Bowling Scoring with Elixir
# Stolen from TJ Stankus, ElixirConf 2017
defmodule Bowling do
defstruct rolls: [], score: 0
def new_game, do: %Bowling{}
def roll(game = %Bowling{}, pinfall) do
rolls = append_pinfall(game.rolls, pinfall)
%{ game | rolls: rolls, score: score(rolls) }
@alexpeachey
alexpeachey / transactions_post_create.rb
Last active October 11, 2017 19:13
Example Transaction 3
module Transactions
module Posts
CREATE_ATTRIBUTES = %i{topic title body}
Create = Transactions.build(:posts, :create) do
step :validate
step :hydrate, model: :topic
step :set_attributes, model: :post, attributes: CREATE_ATTRIBUTES
step :persist, model: :post
step :submit_draft
@alexpeachey
alexpeachey / transactions_post_create.rb
Last active October 11, 2017 19:21
Example Transaction 2
module Transactions
module Posts
Create = Dry.Transaction(container: {}) do
step :validate, with: Components::Common::Validate.call(schema: Schemas::Post::Create)
step :load_topic, with: Components::Common::LoadModel.call(id: :topic_id, class: Topic, model: :topic)
step :build_post, with: Components::Posts::Create::BuildPost
step :save_post, with: Components::Common::Save.call(model: :post)
step :extract_post, with: Components::Common::Extract::Extract(key: :post)
end
end
@alexpeachey
alexpeachey / transactions_post_create.rb
Last active October 11, 2017 19:20
Example Transaction 1
module Transactions
module Posts
Create = Dry.Transaction(container: {}) do
step :validate, with: Components::Posts::Create::Validate
step :load_topic, with: Components::Posts::Create::LoadTopic
step :build_post, with: Components::Posts::Create::BuildPost
step :save_post, with: Components::Posts::Create::SavePost
step :extract_post, with: Components::Posts::Create::ExtractPost
end
end
### Keybase proof
I hereby claim:
* I am alexpeachey on github.
* I am alexpeachey (https://keybase.io/alexpeachey) on keybase.
* I have a public key whose fingerprint is 8FE8 F2EA A0FA 2629 7D14 E47D 98D5 7A08 AE06 05A6
To claim this, I am signing this object:
@alexpeachey
alexpeachey / index.html
Created April 3, 2011 16:14
Click new and you get a square and it alerts the bbox for the square and it is correct. Click the square, you get some handles. Only the center move handle is active. Click and drag the square somewhere else. Now click new again and you get another square
<!DOCTYPE html>
<head>
<title>Raphael Bugs</title>
<link href='style.css' rel='stylesheet' type='text/css' />
</head>
<body>
<button id='newButton'>
New
</button>
<div id='paper'></div>
@alexpeachey
alexpeachey / gist:879995
Created March 21, 2011 19:03
weird warnings on 3.0.5 and 1.9.2p180
/home/alex/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239: warning: nested repeat operator + and ? was replaced with '*': /\bany(?:\s+)?day\b/
/home/alex/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239: warning: nested repeat operator + and ? was replaced with '*': /([0-9])(?:\s+)?a\b/
/home/alex/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239: warning: nested repeat operator + and ? was replaced with '*': /([0-9])(?:\s+)?p\b/
/home/alex/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239: warning: nested repeat operator + and ? was replaced with '*': /this(?:\s+)?morning/
/home/alex/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239: warning: nested repeat operator + and ? was replaced with '*': /^(?:\s+)?(?:right\s+)?now(?:\s+)?$/
/home/alex/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.5/lib/active_supp