Skip to content

Instantly share code, notes, and snippets.

View remigijusj's full-sized avatar

Remigijus Jodelis remigijusj

View GitHub Profile
class Array
def deep_compact
term = []
hash = each_with_object({}) do |obj,res|
if obj.is_a?(Hash)
obj.each do |key,val|
res[key] ||= []
res[key] += val.respond_to?(:deep_compact) ? val.deep_compact : val
end
else
# a monkey-patch fix for OR-query building, needed for Rails 4.2
module Databound
class Manager
def or_query(root, *scopes)
relations = scopes.map do |scope|
root.where(scope.to_h)
end
nodes = relations.map do |rel|
rel.where_values.reduce(:and)
module Databound
class Manager
def find_scoped_records(only_extra_scopes: false)
if only_extra_scopes
rel = model.where(@scope.to_h)
else
rel = model.where(params.to_h)
check_permit!(:read, rel) # <<< this is out of place, because SRP!
end
@remigijusj
remigijusj / gist:af81ab6fccbcd33e94b9
Created June 1, 2015 08:53
KonservativeDK daina apie Lietuva
STOP den lille kænguru
Tre fra Litauen på indbrud
Gemte sig bag ved en hæk
Tævede gamle fru Jensen
Tog pengene med og var væk!
Men så råbte vi:
Stop de østkriminelle
Før de røver igen
@remigijusj
remigijusj / nested_benchmark.rb
Last active August 29, 2015 14:23
comparison of deep data structures
require 'benchmark'
require 'date'
# from lib/core_ext/hash.rb
class Hash
def at(*path)
path.inject(self) do |ob, key|
ob.respond_to?(:each) ? ob[key] : nil
end
end
@remigijusj
remigijusj / gist:860695
Created March 8, 2011 18:19
transfer local variables
class Binding
def publish(vars)
defs = vars.each_with_object("") do |var,str|
str << "define_method(:#{var}) { #{var} }\n"
str << "private :#{var}\n"
end
self.eval "self.class.module_eval { #{defs} }"
end
end
@remigijusj
remigijusj / gist:870925
Created March 15, 2011 15:57
dm-redis-adapter with "through" associations
require 'dm-core'
require 'dm-redis-adapter'
DataMapper.setup(:default, {:adapter => "redis"})
class Book
include DataMapper::Resource
property :id, Serial
property :name, String
@remigijusj
remigijusj / gist:1385465
Created November 22, 2011 11:30
ruby-golf
# using Ruby 1.9.2
# Hole 1, 41 chars
def fizzbuzz(n)
"#{n%3<1&&f=:Fizz;n%5<1?"#{f}Buzz":f||n}"
end
# Same as Cyrus's solution, optimized
p fizzbuzz(3) # => "Fizz"
p fizzbuzz(10) # => "Buzz"
@remigijusj
remigijusj / gist:3927443
Created October 21, 2012 16:26
GIT pre-commit hook to check .rb and .erb syntax
#!/Ruby192/bin/ruby.exe
#
# A hook script to verify that only syntactically valid ruby code is commited.
# Called by git-commit with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# Put this code into a file called "pre-commit" inside your .git/hooks
# directory, and make sure it is executable ("chmod +x .git/hooks/pre-commit")
@remigijusj
remigijusj / hot-items-queries.sql
Created October 30, 2012 16:26
some hot items SQL
-- ratio with average view count log-trend
SELECT i.id FROM items i
LEFT JOIN global_stats vc ON vc.entry_type=510 AND vc.entry_id = i.id
INNER JOIN user_settings s ON s.user_id = i.user_id
WHERE s.is_publish_photos_agreed = 1 AND is_visible = 1 AND is_replicated = 0 AND is_reserved = 0
AND boutique_id IS NULL
AND created_at >= '2012-10-27'
AND i.catalog_id IN (%cat4%)
AND vc.value / (LN(DATEDIFF(NOW(),created_at)+1)*15.5+12.4) >= 0.8
ORDER BY id DESC