Skip to content

Instantly share code, notes, and snippets.

Nathan Appere nathan-appere

Block or report user

Report or block nathan-appere

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View contract-example.rb
module Test
module RGB
include Kit::Contract
before ->(r:, g:, b:) { (0..255) === r && (0..255) === g && (0..255) === b }
after [
->(result:) { result.is_a?(String) },
->(result:) { result.start_with?('#') },
->(result:) { result.length.in?([4, 7]) },
]
@nathan-appere
nathan-appere / kit-contracts.rb
Last active Oct 11, 2019
Example of ruby contracts
View kit-contracts.rb
module RGB
# First step -----------------------------------------------------------------
before ->(r:, g:, b:) { (0..255) === r && (0..255) === g && (0..255) === b }
after [
->(result:) { result.is_a?(String) },
->(result:) { result.start_with?('#') },
->(result:) { result.length.in?([4, 7]) },
]
@nathan-appere
nathan-appere / sig.rb
Created Oct 1, 2019 — forked from havenwood/sig.rb
An example for Nathanael7 on #ruby IRC
View sig.rb
def sig(method_name)
meth = method(method_name)
parameters = meth.parameters.map do |type, name|
case type
when :req
name
when :rest
"*#{name}"
when :opt
# Unknown default argument.
View dry-types.rb
require 'dry-types'
require 'pry'
module Types
include Dry.Types
end
ProtocolType = Types::Symbol.enum(:http, :async, :websockets)
HttpVerb = Types::Symbol.enum(:get, :post)
View pagination.txt
| D1 | D2 | D3
R1 | A | W | 0
R2 | A | W | 1
R3 | A | X | 2
R4 | A | X | 3
R5 | A | Y | 4
R6 | B | Y | 5
R7 | B | Z | 6
R8 | C | Z | 1
@nathan-appere
nathan-appere / organizer.rb
Last active Jul 21, 2019
Bare logic for a functional organizer
View organizer.rb
require 'contracts'
module Organizer
include Contracts
Contract KeywordArgs[list: ArrayOf[RespondTo[:call]], ctx: Optional[Hash]] => [Symbol, Hash]
def self.call(list:, ctx: {})
result = :ok
begin
@nathan-appere
nathan-appere / event-broker.coffee
Last active Apr 6, 2018
Quick implementation of an event-broker with channels + topics
View event-broker.coffee
`import Ember from 'ember'`
# Where we save channels.
# The format is:
# channels = { "components.auth" { "submit": { /* hash of callbacks */ } }}
channels = {}
# Given hash, search for keys that match a pattern and call "callback" on them
# Ex: forEachMatch({ "a.b.c": 1, "a.b": 2, "x.y": 3 }, 'a.b.c', fn) will call fn twice with: fn(1) and fn(2)
forEachMatch = (list, key, callback) ->
@nathan-appere
nathan-appere / pubsub.coffee
Last active Apr 5, 2018
Fake implementation of inter component PubSub mechanism
View pubsub.coffee
EventeableMixin = Ember.Mixin.create
# Handle eventBroker change, tricky one!
init: ->
if !@get('eventBroker')
@set('eventBroker', create_local_event_broker)
register_events_listeners()
register_events_listeners: ->
View interactor_danger.rb
class Interactor1
def call
context.fail! error: 'Failure. This is lost.'
end
end
class Interactor2
def call
foreign_context = Interactor1.call!(b: 2)
puts "THIS WILL NEVER BE CALLED."
View organizer.rb
organize [
Interactors::Upgrade::EnsureAccountOwner,
->(ctx) { ctx.payment_source = ctx.current_subscription.payment_source },
Interactors::Upgrade::EnsureValidUpgrade,
->(ctx) { Interactors::Subscribe::CreateSubscription.call(ctx, ended_at: ctx.upgraded_at) }
]
You can’t perform that action at this time.