Skip to content

Instantly share code, notes, and snippets.

View joeletizia's full-sized avatar

Joe Letizia joeletizia

View GitHub Profile
defmodule Success do
@type t :: %Success{value: any}
defstruct value: nil
end
defmodule Failure do
@type t :: %Failure{value: any, reason: String.t}
defstruct value: nil, reason: nil
end
defmodule EmailValidation do
def validate(email) do
email_validation_result = {:ok, email}
check_for_validity(email_validation_result)
|> check_if_email_taken
end
defp check_for_validity({:ok, email}) do
case check_against_regex(email) do
Dir.glob("**/*.rb").each do |file|
`vim -c "set ts=4 sts=4 noet|retab!|set ts=2 sts=2 et|retab|wq" #{file}`
end
@joeletizia
joeletizia / example.rb
Created June 26, 2013 01:54
Example of mocking and stubing with dependency injection
module Example
class Dog
attr_reader :stomach
def initialize(stomach)
@stomach = stomach
end
def hungry?
stomach.empty?
@joeletizia
joeletizia / parameterize-demo.rb
Created April 21, 2013 23:23
A demo of parameterize function in ActiveSupport
"Joe Letizia's awesome blog 2.0".parameterize
# => "joe-letizia-s-awesome-blog-2-0"
require 'active_record'
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.colorize_logging = false
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:dbfile => ":memory:"
)
@joeletizia
joeletizia / products.rb
Created December 15, 2015 22:21
Controller resources that are agnostic of content type to be rendered.
module Products
# What if we want to differ the content between iOS and Web?
class IndexResource
def initialize(product_presenters, content_type)
@product_presenters = product_presenters
@content_type = content_type
end
def render(rendering_engine = RenderingEngine)
rendering_engine.render(partial_path, product_presenters: product_presenters)
@joeletizia
joeletizia / products.rb
Created December 15, 2015 22:21
Controller resources that are agnostic of content type to be rendered.
module Products
# What if we want to differ the content between iOS and Web?
class IndexResource
def initialize(product_presenters, content_type)
@product_presenters = product_presenters
@content_type = content_type
end
def render(rendering_engine = RenderingEngine)
rendering_engine.render(partial_path, product_presenters: product_presenters)
@joeletizia
joeletizia / Logger.cs
Created February 21, 2013 16:56
Logging class
public class SubordinateLoggingContext
{
private static string CONNECTIONSTRING = "mongodb://connotate:connotate@linus.mongohq.com:10002/ConnotateLogs";
MongoServer mongo;
MongoDatabase db;
MongoCollection collection;
public SubordinateLog log;
public SubordinateLoggingContext()
@joeletizia
joeletizia / Winter-func.rb
Created February 6, 2013 02:10
A solution (at least I think it's right...) to http://challenge.signpost.com/leveltwo The Ransom Glad you've made it this far, intrepid friend of Max. To tell you a little about ourselves, our goal is to end winter in Chicago. We've read our Ries and plan to start small, with an MVP. We currently have the resources to build a structure covering …
require 'open-uri'
def adj_sum(multi,x,y)
sum = multi[x][y].to_i
if x > 0
sum += multi[x-1][y].to_i
end
if x < 999