Skip to content

Instantly share code, notes, and snippets.

View Ch4s3's full-sized avatar

Chase Gilliam Ch4s3

View GitHub Profile
defmodule Testing do
"""
You can pipe straight into a case
Kernel.<<>>, the special form for binary pattern
matching can be used in a case match to
split up incoming binaries, like this
imaginary |~ delimited protocol.
"fiexd_size_header|~some message"
@doc """
You can pipe queries into this build_where/3
fn and match on operators like :in, :==,
:ilike, etc and handle the particular
where semantics in each function case.
Pay attention to the positional binding
[...,e] which will capture the last
binding added to the piped in query.
See also: https://hexdocs.pm/ecto/Ecto.Query.html#module-positional-bindings
and. https://hexdocs.pm/ecto/Ecto.Query.API.html#field/2
@Ch4s3
Ch4s3 / select_2_explain.ex
Created September 21, 2020 23:19
select/2 explaination
from(c in City, select: c) # returns the schema as a struct
from(c in City, select: {c.name, c.population}) # returns Tuple of {name, population}
from(c in City, select: [c.name, c.county]) # returns List of [name, county]
from(c in City, select: %{n: c.name, answer: 42}) # returns Map with keys :n, and :answer
from(c in City, select: %{c | alternative_name: c.name}) # returns the schema as a Struct with :alternative_name added
from(c in City, select: %Data{name: c.name}) # returns the selected name in %Data{}
defmodule MyApi.Schema.Pipeline.MyCompilePhase do
@moduledoc """
based on Absinthe phases https://hexdocs.pm/absinthe/1.5.3/Absinthe.Phase.html
"""
alias Absinthe.{Blueprint, Pipeline, Phase}
alias Absinthe.Blueprint.Schema.{ObjectTypeDefinition, FieldDefinition}
def pipeline(pipeline) do
Pipeline.insert_after(pipeline, Phase.Schema.TypeImports, __MODULE__)
@Ch4s3
Ch4s3 / check_migrations.sh
Created December 4, 2019 17:23
check_for_migrations
file_changes="$(git diff --name-only $(git merge-base master HEAD))"
migration_files="$(echo $file_changes | grep -c /migrations/)"
application_files="$(echo $file_changes | grep -c -v /migrations/ --include \*.ex --include \*.exs)"
echo "checking for migrations and apllication changes"
if (($migration_files == 0)) && (($application_files > 0));
then
echo "Only Application Changes. OK"
exit 0
defmodule Boggle do
def full_match(dictionary, word) do
case Enum.member?(dictionary, word) do
true -> word
false -> nil
end
end
@Ch4s3
Ch4s3 / dump_db_to_json.rb
Last active May 14, 2021 15:17 — forked from brianburridge/gist:8d2755a73dd5b4f0332b
Export local db to JSON and load that dump back to the db later
namespace :json do
desc 'Export all data to JSON files'
task :export => :environment do
Rails.application.eager_load!
ApplicationRecord.descendants.each do |model|
next if model.table_name.nil? || model.table_name == ''
begin
data = model.all
next if data == []
@Ch4s3
Ch4s3 / double_stack.rb
Created April 12, 2018 02:40
a simple two stack based queue in ruby
class DoubleStack
attr_accessor :inbox, :outbox
def initialize
@inbox = []
@outbox = []
end
def enqueue(item)
@inbox << item
end
@Ch4s3
Ch4s3 / merge_sort.rb
Created April 9, 2018 03:08
Top Down Merge Sort in ruby
#################################
# Top-down recursive merge sort
#################################
#################################
# Worst-case perf O(n log n)
# Bestt-case perf O(n log n)
# Worst-case space complexity O(n)
#################################
@Ch4s3
Ch4s3 / sparse_array.rb
Created March 29, 2018 22:11
find and sum the count of occurances of a query_string in an array of strings
def find_suffix(collection, query_string)
# Inject 0 so that when evaluating the block, you have
# and object that responds to `+`
# if the array element is equal to the query_string
# add to the sum and return the sum to be used in
# the next step
collection.inject(0) { |sum, str| sum += 1 if str == query_string; sum }
end