Skip to content

Instantly share code, notes, and snippets.

View anga's full-sized avatar
🧉
<- This is my fuel

Andrés B. anga

🧉
<- This is my fuel
View GitHub Profile
@anga
anga / gist:3194130
Created July 28, 2012 17:36
Load all observers (Ruby on Rails)
# config/application.rb
module RailsApp
class Application < Rails::Application
# ...
files = File.join(Rails.root, 'app', 'models') + File::SEPARATOR + '*_observer.rb'
observers = []
Dir[files].each do |file|
observers << File.basename(file).gsub('.rb','').to_sym
end
@anga
anga / gist:3498649
Created August 28, 2012 14:46
[Ruby] Basic read and write in the same function
class Foo
def foo(bar=nil)
@bar = (bar || @bar)
end
end
# Example
foo = Foo.new
foo.foo() # => nil
foo.foo(10) # => 10
@anga
anga / gist:4644133
Last active December 11, 2015 18:48
declarative_authorization macro
module Pivilages
def load_privilages_for(user)
Authorization.current_user = user
file_name = File.join(Rails.root, 'config', 'authorization_rules.rb')
rules = File.read(file_name)
load_engine_analyzer_for rules
end
@anga
anga / gist:4644188
Created January 26, 2013 19:52
declarative_authorization rspec matcher
RSpec::Matchers.define :permit do |action|
match do |object|
read_user object
permit?(action, user: @user, context: @object)
end
def read_user(object)
return if @user
@anga
anga / gist:3df46fb4381a715bb9c6aeb99a8cf421
Created December 16, 2020 16:30
Merging Hases in Ruby 2.7.1p83
require 'benchmark'
a = {foo: 'Hello'}
b = {bar: ' world'}
n = 100_000_000
Benchmark.bm do |x|
x.report("{**a,**b}") do
n.times {
{**a, **b}