Peter Marklund peter

View recursive_struct.rb
1 2 3 4 5 6 7 8 9 10
# Simple wrapper to allow hashes to be accessed via dot notation recursively.
# Recurses over hashes and arrays. Works with string keys
# and symbol keys - other types of keys are not supported and
# all keys must be of the same type. Write access is only supported via
# []= Hash syntax. Supports accessing hash values with square bracket Hash syntax ([...])
# and access is indifferent to if the key is given as a string or a symbol.
# Supports JSON generation.
#
# Dependencies: Ruby.
#
View faster-unit-testing-in-rails-without-rails.rb
1 2 3 4 5 6 7 8 9 10
# Running a single minimalistic unit test with Rails 4 and Ruby 2.1.1 is a lot faster
# if you avoid loading Rails:
 
# Running the test *with* Rails
 
time ruby -Itest test/models/my_model_test.rb # => real ~ 6s
 
# Running the test *without* Rails
 
time ruby -Itest/no_rails test/models/my_model_test.rb # => real ~ 0.6s
View carrierwave-file-size-limit-example.rb
1 2 3 4 5 6 7 8 9 10
# NOTE: There must be a better way to do this - help appreciated!
 
###############################################################
# uploaded_file.rb - utility class
###############################################################
 
class UploadedFile
def self.size(file)
uploaded?(file) ? file.size : nil
end
View gist:9121297
1 2 3 4 5 6 7 8 9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Hooking up the middleware with the express app
// In app.js
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
var forceSSL = require('../middleware/ssl').force(config.hostname);
if ('production' == app.get('env')) {
app.use(forceSSL);
}
View rack-rest-api-example.rb
1 2 3 4 5 6 7 8 9 10
#################################################################
#
# File: lib/api.rb
#
#################################################################
 
require 'user' # ActiveRecord model
 
# Connect to the db
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'])
View identity_map.rb
1 2 3 4 5 6 7 8 9 10
# Poor man's identity map for reusing ActiveRecord objects and avoid fetching the same
# record multiple times from the database within a request or background job. The check method can
# be used to locate duplicate queries in development. The goal is to instantiate
# each ActiveRecord object only once. The Rails identity map is known to have issues:
# http://stackoverflow.com/questions/6905891/rails-3-1-identity-map-issues
class IdentityMap
# Set one or more ActiveRecord objects in the map
def set(*objects)
objects.each do |object|
map[key(object)] = object
View creating-edgerails-app.sh
1 2 3 4 5 6 7 8 9 10
# 0. Make sure you have Ruby 1.9.3 installed, and optionally RVM and PostgreSQL
 
# 0.2 If you are on the Mac, make sure you have a c compiler by installing XCode Command Line Tools or gcc4.2 with homebrew
# https://github.com/mxcl/homebrew/wiki/Custom-GCC-and-cross-compilers
 
# 0.5 Make sure you have bundler version ~> 1.2 as Rails depends on it
gem install bundler
 
# 1. Get edge Rails source (master branch)
git clone https://github.com/rails/rails.git
View about_scoring_project.rb
1 2 3 4 5 6 7 8 9 10
def counts_from_dice(dice)
dice.inject({}) do |hash, value|
hash[value] ||= 0
hash[value] += 1
hash
end
end
 
def score(dice)
counts_from_dice(dice).inject(0) do |sum, (number, count)|
View ruby_inject_readable.rb
1 2 3 4 5 6 7 8 9
# Traditional idiom
total = 1
[2, 3, 5].each do |value|
total = total * value
end
puts total
 
# Ruby idiom
puts [2, 3, 5].inject { |mult, value| mult * value }
View gist:715518
1 2 3 4 5 6 7 8 9 10
module NullifyTextAttributes
def self.included(base)
base.class_eval do
before_validation :nullify_text_attributes
 
private
def nullify_text_attributes
self.class.columns.select { |c| [:text, :string].include?(c.type) }.each do |column|
send("#{column.name}=", nil) if read_attribute(column.name).blank?
end
Something went wrong with that request. Please try again.