Skip to content

Instantly share code, notes, and snippets.

View mcelaney's full-sized avatar

Brian E. McElaney mcelaney

View GitHub Profile
defmodule VehicleMake do
schema "vehicle_makes" do
field :name, :string
has_many :vehicle_models, VehicleModel
has_many :vehicles, through: [:vehicle_models, :vehicles]
end
end
defmodule VehicleModel do
defmodule ProductVehicle do
def vehicles_that_fit_product(query \\ ProductVehicle, product_id) do
query
|> join(:inner,
[products_vehicles],
product in assoc(products_vehicles, :product))
|> join(:inner,
[products_vehicles],
vehicle in assoc(products_vehicles, :vehicle))
end
defmodule ProductVehicle do
def vehicles_that_fit_product(query \\ ProductVehicle, product_id) do
query
|> join(:inner,
[products_vehicles],
product in assoc(products_vehicles, :product))
|> join(:inner,
[products_vehicles, _product],
vehicle in assoc(products_vehicles, :vehicle))
end
defmodule ProductVehicle do
def vehicles_that_fit_product(query \\ ProductVehicle, product_id) do
query
|> join(:inner,
[products_vehicles],
product in assoc(products_vehicles, :product))
|> join(:inner,
[products_vehicles],
vehicle in assoc(products_vehicles, :vehicle))
|> join(:inner,
defmodule ProductVehicle do
def vehicles_that_fit_product(query \\ ProductVehicle, product_id) do
query
|> join(:inner,
[products_vehicles],
product in assoc(products_vehicles, :product))
|> join(:inner,
[products_vehicles],
vehicle in assoc(products_vehicles, :vehicle))
|> join(:inner,
@mcelaney
mcelaney / ugly_but_fast.rb
Created August 31, 2016 11:34
Ugly but fast
class FizzBuzz
def self.find_answers_for(max)
self.new.find_answers_for(max)
end
def initialize
@result = [
Proc.new do |x| x; end,
Proc.new do |x| x; end,
Proc.new do |x| "Fizz"; end,
@mcelaney
mcelaney / normalish.rb
Last active August 31, 2016 13:32
Normalish FizzBuzz
class FizzBuzz
def self.find_answers_for(max)
1.upto(max).each do |n|
result = ''
result << 'Fizz' if n % 3 == 0
result << 'Buzz' if n % 5 == 0
result = n if result.empty?
result
end
end
@mcelaney
mcelaney / slow-but_beautiful_in_a_way.rb
Created August 31, 2016 11:42
Slow - but beautiful in a way
class FizzBuzz
def self.find_answers_for(max)
self.new.find_answers_for(max)
end
def find_answers_for(max)
1.upto(max).map do |current|
[find_result_hash(current), current]
.find { |result| result != '' }
end
@mcelaney
mcelaney / when_brevity_is_not_a_thing.rb
Last active August 31, 2016 13:30
When brevity doesn't matter...
# Run if necessary:
# RubyVM::InstructionSequence.compile_option = {
# tailcall_optimization: true,
# trace_instruction: false
# }
class FizzBuzz
def self.find_answers_for(max)
self.new(max).find_answers_for([], 1)
end
@mcelaney
mcelaney / magic_square.ex
Created March 2, 2017 03:44
PhillyDev Slack #daily_programmer for March 1, 2017
defmodule MagicSquare do
@moduledoc """
Today's daily programmer challenge is to test a magic square. This challenge
is stolen verbatim from r/dailyprogrammer. A 3x3 magic square is a 3x3 grid
of the numbers 1-9 such that each row, column, and major diagonal adds up to
15. Here's an example:
```
8 1 6
3 5 7
4 9 2