Skip to content

Instantly share code, notes, and snippets.

@jwoertink
jwoertink / custom_id.cr
Created July 22, 2021 15:01
Custom primary key ID for Avram
alias CustomID = String
module Avram::Migrator::Columns::PrimaryKeys
class CustomIDPrimaryKey < Avram::Migrator::Columns::PrimaryKeys::Base
def initialize(@name)
end
def column_type : String
"character varying(18)"
end
abstract class StoragePageLayout
include Lucky::HTMLBuilder
getter view = IO::Memory.new
abstract def content
def render
html_doctype
html do
# in src/app_server.cr
def middleware : Array(HTTP::Handler)
[
Lucky::ForceSSLHandler.new,
Lucky::HttpMethodOverrideHandler.new,
Lucky::LogHandler.new,
Lucky::ErrorHandler.new(action: Errors::Show),
CORSHandler.new,
Lucky::RemoteIpHandler.new,
Cable::Handler.new(ApplicationCable::Connection),
require 'httparty'
require 'nokogiri'
class Scraper
attr_accessor :parse_page
def initialize
doc = HTTParty.get("http://store.nike.com/us/en_us/pw/mens-nikeid-lifestyle-shoes/1k9Z7puZoneZoi3")
@parse_page ||= Nokogiri::HTML(doc)
end
def get_names
names = item_container.css(".product-name").css("p").children.map { |name| name.text }.compact

Crystal on WASM

Running

  • crystal wasm.cr
  • php -S localhost:3000
  • open your browser
  • open dev tool js console
  • crystal.add(1, 2)
<!DOCTYPE html>
<html>
<head><title>Paper Rock Scissor</title></head>
<body>
<h2>Play This Game!</h2>
<span id="playerScore">You: <strong>0</strong></span>
<span id="computerScore">It: <strong>0</strong></span>
<div>
<button value="paper">🧻</button>
<button value="rock">🤘</button>
class Basket
attr_accessor :items
def initialize
self.items = []
end
def total
self.items.sum(0.0) { |item| item[:price] }
end
@jwoertink
jwoertink / 00_notes.txt
Last active February 15, 2019 05:33
Our production deployment setup for deploying Lucky on to Elastic Beanstalk in production.
We develop locally on MacOS. Due to some issues with cross compilation, we build the crystal binary on docker locally,
then zip that up and ship that along with the docker stuff to elasticbeanstalk.
// can't use SomeLib here unless I require it in this file
// even if I run my program from main.js
class A extends SomeLib {
}
module.exports = A
@jwoertink
jwoertink / character.cr
Created July 15, 2018 16:06
Trying to get character movement similar to FinalFantasy V or VI. The character should move smoothly but always stop moving directly over a tile. Tiles are 64x64.
def initialize
@tile_x = 9
@tile_y = 7
@fine_x = 0
@fine_y = 0
end
def draw(renderer)
renderer.copy(sprite, dstrect: SDL::Rect[
@tile_x * 64 + @fine_x,