Skip to content

Instantly share code, notes, and snippets.

Avatar
🙂
🌴

Max Chernyak maxim

🙂
🌴
View GitHub Profile
View barewords.rb
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
@maxim
maxim / react.rb
Created Jun 28, 2019
Extending react-rails with styled components support
View react.rb
# 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
View stream_ext.ex
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.
@maxim
maxim / docker-compose.yml
Created Aug 7, 2017
Getting started with docker
View docker-compose.yml
version: '3'
services:
postgres:
image: postgres:9.6
ports:
- "5432:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
View select_at.ex
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)
View singleton_or_not.rb
# 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)
@maxim
maxim / 1-works_fine.rb
Last active Nov 28, 2016
Strange ruby bug: can't put comment after linebreak in method args. Tested in 2.3.2.
View 1-works_fine.rb
def foo \
a: 'a',
# comment here is ok
b: 'b'
puts a, b
end
foo(a: 'a', b: 'b') # => a\nb
View pipe.rb
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
View foobar.rb
module Widgets
def self.extended(base)
class << base
attr_accessor :widgets
end
base.widgets = {}
end
def add_widget(name, widget)
@maxim
maxim / ecto_batch_stream.ex
Last active Jul 10, 2020
Similar to Rails `find_each`, but for Elixir's Ecto, using Stream
View ecto_batch_stream.ex
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 # => […]
You can’t perform that action at this time.