Skip to content

Instantly share code, notes, and snippets.

Rails REST
CRUD(L) -- Create Read Update Delete (List)
List -- GET /widgets
Create -- POST /widgets
Read -- GET /widgets/:id
Update -- PUT /widgets/:id
Update -- PATCH /widgets/:id
-- according to the following type annotation the `add` function...
--
-- * takes an `Int`
-- * takes another `Int`
-- * and returns an `Int`
--
-- elm will automatically curry for fewer arguments though,
-- so that also means that this function will...
--
-- * take an `Int`

I'm defining a function on Question model called user, which gets the User record tied to it.

Do you think multiple calls to a DB in the view/template is too much?

So there's the "best practices" answer, and then what I imagine is the actual answer to your question here.

The Actual Answer

So let's assume we have a Question model:

new Git project

dev $ mkdir proj && cd proj

proj $ git init
Initialized empty Git repository in /home/bosco/dev/proj/.git/

create and commit a file

def split_on(split_string, string_to_split_on = nil)
splitter = Proc.new {|string_to_split_on|
string_to_split_on.split split_string
}
if string_to_split_on
splitter.call string_to_split_on
else
splitter
end
class User < AR::Base
has_many :questions
end
class Question < AR::Base
end
module Service::UserShow
UserShowExhibit = Struct.new :user, :user_questions
allowed_permissions = MapSet.new([:owner, :manager]) # => #MapSet<[:manager, :owner]>
user_permissions = MapSet.new([:associate, :member]) # => #MapSet<[:associate, :member]>
MapSet.disjoint? user_permissions, allowed_permissions
# => true
allowed_permissions = MapSet.new([:owner, :manager]) # => #MapSet<[:manager, :owner]>
user_permissions = MapSet.new([:manager, :member]) # => #MapSet<[:manager, :member]>
MapSet.disjoint? user_permissions, allowed_permissions
# => false
@bchase
bchase / rpi_gpio_relay_controller.exs
Last active November 2, 2016 19:18
Elixir + Raspberry Pi 2 GPIO to drive a 4 channel relay module (https://smile.amazon.com/gp/product/B00KTEN3TM)
defmodule RPi.GPIO.RelayController do
use GenServer
### CLIENT API ###
def start_link do
GenServer.start_link __MODULE__, :init_args, []
end
def on( pid, relay_num), do: GenServer.cast(pid, {:on, relay_num})
defmodule Web do
# $ elixir -v
# Erlang/OTP 19 [erts-8.1] [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false]
#
# Elixir 1.3.4
# MATCH CASES
# *** &Web.cond_and_regex_match/1 ***
# 1.6 sec 2M iterations 0.78 μs/op
#
_ = require 'underscore'
categorizeSides = (sides) ->
throw "not a triangle" unless sides.length == 3
switch _(sides).uniq().length
when 1 then "equilateral"
when 2 then "isosceles"
when 3 then "scalene"