Skip to content

Instantly share code, notes, and snippets.

View maxim's full-sized avatar
🛠️

Max Chernyak maxim

🛠️
View GitHub Profile
@maxim
maxim / react.rb
Created June 28, 2019 06:12
Extending react-rails with styled components support
# 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
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 August 7, 2017 23:07
Getting started with docker
version: '3'
services:
postgres:
image: postgres:9.6
ports:
- "5432:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
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)
# 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 November 28, 2016 18:28
Strange ruby bug: can't put comment after linebreak in method args. Tested in 2.3.2.
def foo \
a: 'a',
# comment here is ok
b: 'b'
puts a, b
end
foo(a: 'a', b: 'b') # => a\nb
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
@maxim
maxim / foobar.rb
Created February 17, 2016 16:51 — forked from AJFaraday/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 September 9, 2022 18:15
Similar to Rails `find_each`, but for Elixir's Ecto, using Stream
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 # => […]
@maxim
maxim / 1-ru.rb
Last active November 9, 2015 13:47
If ruby was in Russian
затребовать 'shellwords'
затребовать 'posix/spawn'
затребовать 'skeptick/error'
модуль Skeptick
класс Command
атр_читатель :shellwords
опр инициализировать(shellwords)
@shellwords = shellwords