This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def plain_text_receipt # => Runtime: 5 sec, Network Timeout | |
<<-TEXT | |
Thank you for your order! | |
Product: #{name} - #{price} # => 2 DB queries | |
Tax: #{tax_amount} # => 1 DB query, 1 API call | |
Total: #{total_amount} # => 2 DB queries, 1 API call | |
TEXT | |
end | |
private |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/react.rb | |
Rails.configuration.react.server_renderer = | |
Class.new(React::ServerRendering::BundleRenderer) do | |
private | |
def render_from_parts(before, main, after) | |
js_code = compose_js(before, main, after) | |
@context.eval(js_code) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule StreamExt do | |
@doc """ | |
Stream records from Ecto in batches. | |
Supported options are | |
* batch_size - how many rows to fetch at once | |
* strategy - either :offset or :id | |
* :offset - uses SQL offset to fetch pages of results | |
This is slower, but works for any query. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3' | |
services: | |
postgres: | |
image: postgres:9.6 | |
ports: | |
- "5432:5432" | |
volumes: | |
- postgres-data:/var/lib/postgresql/data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def select_at(list, indexes), | |
do: select_at(list, indexes, 0, []) |> Enum.reverse | |
def select_at([], _, _, results), | |
do: results | |
def select_at(_, [], _, results), | |
do: results | |
def select_at([item | list], [i | indexes], i, results), | |
do: select_at(list, indexes, i + 1, [item | results]) | |
def select_at([_ | list], indexes = [index | _], i, results), | |
do: select_at(list, indexes, i + 1, results) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You could have an object for the whole app to use (e.g. in rails initializer) | |
ChanceGenerator = Chance.new | |
# But also easily testable (this would be in a test setup): | |
test_chance = Chance.new(seed: 1) | |
# The constructor can have a little documentation: | |
class Chance | |
# If seed is omitted, a new seed will be generated for each random call | |
def initialize(seed: nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def foo \ | |
a: 'a', | |
# comment here is ok | |
b: 'b' | |
puts a, b | |
end | |
foo(a: 'a', b: 'b') # => a\nb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Object | |
def pipe(meth, obj = self, args = []) | |
obj.public_send(meth, self, *args) | |
end | |
end | |
class Doubler | |
def double(c); c.map{|v| v*2} end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Widgets | |
def self.extended(base) | |
class << base | |
attr_accessor :widgets | |
end | |
base.widgets = {} | |
end | |
def add_widget(name, widget) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule EctoBatchStream do | |
import Ecto.Query, only: [from: 1, from: 2] | |
@batch_size 1000 | |
# Example: | |
# | |
# query = from u in MyApp.User, select: u.email | |
# stream = EctoBatchStream.stream(MyApp.Repo, query) | |
# stream |> Stream.take(3) |> Enum.to_list # => […] |