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 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 |
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
# 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) |
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 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 |
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
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 |
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
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 |
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 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 |
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
require 'dm-core' | |
require 'dm-redis-adapter' | |
DataMapper.setup(:default, {:adapter => "redis"}) | |
class Book | |
include DataMapper::Resource | |
property :id, Serial | |
property :name, String |
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
# 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" |
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
#!/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") |
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
-- 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 |
OlderNewer