🏋️♂️
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ClientsController < ApplicationController | |
def create | |
@client = Client.new(params[:client]) | |
if @client.save | |
redirect_to @client | |
else | |
flash.now[:error] = "Could not save client" | |
render action: "new" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'default_file_writer' | |
require_relative 'no_overwrite_file_writer' | |
require_relative 'no_null_file_writer' | |
file_writer = NoOverwriteFileWriter.new( | |
NoNullFileWriter.new( | |
DefaultFileWriter.new | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NoNullFileWriter | |
def initialize(default_file_writer) | |
@default_file_writer = default_file_writer | |
end | |
def write(path, contents) | |
fail_if_empty_contents(contents) | |
default_file_writer.write(path, contents) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NoOverwriteFileWriter | |
def initialize(default_file_writer) | |
@default_file_writer = default_file_writer | |
end | |
def write(path, contents) | |
fail_if_file_exist(path) | |
default_file_writer.write(path, contents) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DefaultFileWriter | |
def write(path, contents) | |
IO.write(path, contents) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DefensiveFileWriter | |
def write(path, contents) | |
fail_if_empty_contents(contents) | |
fail_if_file_exist(path) | |
write_file(path, contents) | |
end | |
private | |
def fail_if_empty_contents(contents) | |
raise 'Nothing to write!' if empty_contents?(contents) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildscript { | |
repositories { | |
maven { url 'https://dl.bintray.com/drummer-aidan/maven' } | |
mavenCentral() | |
} | |
dependencies { | |
classpath "com.android.tools.build:gradle:1.3.1" | |
classpath "jp.leafytree.gradle:gradle-android-scala-plugin:1.4" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Type do | |
defmacro __using__(opts) do | |
quote location: :keep, bind_quoted: [ | |
type: Macro.escape(opts[:type], unquote: true), | |
values: Macro.escape(opts[:values], unquote: true), | |
] do | |
@behaviour Ecto.Type | |
def type, do: type | |
def values, do: values |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Yatzy.Score do | |
def upper(n, ds), do: n * count(n, ds) | |
defp count(n, ls), do: select_elements(n, ls) |> Enum.count | |
defp select_elements(n, ls), do: ls |> Enum.filter &(&1 == n) | |
def chance(ds), do: ds |> Enum.reduce &(&1 + &2) | |
def yatzy(ds) do | |
case count_unique_elements(ds) do | |
1 -> 50 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Contract Num, Func[Num => Any], Proc => Any | |
def eight_divide_by(number, success_action, failure_action) | |
case number | |
# ... | |
when 4 then success_action[2] | |
when 2 then success_action[4] | |
when 1 then success_action[8] | |
when 0 then failure_action.call | |
end | |
end |