Skip to content

Instantly share code, notes, and snippets.

View vmoravec's full-sized avatar

Vladimir Moravec vmoravec

View GitHub Profile
@vmoravec
vmoravec / Gemfile
Created September 13, 2012 18:14
Rails Lightweight Stack. Most of this is detailed on Crafting Rails Applications - http://pragprog.com/book/jvrails/crafting-rails-applications
source :rubygems
# We are not loading Active Record, nor Active Resources etc.
# We can do this in any app by simply replacing the rails gem
# by the parts we want to use.
gem "actionpack", "~> 3.2"
gem "railties", "~> 3.2"
gem "tzinfo"
# Let's use thin
@vmoravec
vmoravec / i18n.coffee
Created October 31, 2012 14:56 — forked from madrobby/i18n.coffee
Backbone i18n with CoffeeScript
# before this file is loaded, a locale should be set:
#
# In a browser environment, you can use:
# ```<script>__locale='en';</script>```
#
# In a server environment (specifically node.js):
# ```global.__locale = 'en';```
# normalize in-app locale string to "en" or "de-AT"
parts = @__locale.split('-')
@vmoravec
vmoravec / chat.rb
Created December 4, 2012 18:50 — forked from rkh/chat.rb
Simple Chat Application using the Sinatra Streaming API
# coding: utf-8
require 'sinatra'
set server: 'thin', connections: []
get '/' do
halt erb(:login) unless params[:user]
erb :chat, locals: { user: params[:user].gsub(/\W/, '') }
end
get '/stream', provides: 'text/event-stream' do
# This is a possible approach to baking in side-effect free support into
# the context object for DCI. The #on_call would support a declarative way of
# knowing which roles to mixin/unmix on a given object. This could
# use something like mixology or another implementation under the covers.
class AddToCartContext < Context
attr_reader :user, :book
def self.call(user, book)
AddToCartContext.new(user, book).call
@vmoravec
vmoravec / command-pattern.rb
Created January 5, 2013 20:01
Command pattern
class Command
def initialize(description, proc1, proc2)
@description = description
@function = proc1
@inverse = proc2
end
def execute
@function.call unless @function.nil?
end
# ---role
module BookManager
def create_book(attrs)
self.books.create! attrs
end
def update_book(book_id,attrs)
book = self.books.find(book_id)
book.update_attributes attrs
namespace :gemfile do
desc "filters the gemfile, leaving only gems belonging to specific groups"
task :filter => :environment do
groups = (ENV['groups'] || "").split(',')
if groups.empty?
puts "! Invalid usage"
puts
puts "Please specify the groups you want to filter. Example:"
class RPMSpec
attr_reader :spec
def initialize(spec)
@tags = Hash.new {|h,k| h[k] = []}
@defines = {}
@spec = spec
@sections = Hash.new {|h,k| h[k] = []}
#@rpm_build_dir = rpm_build_dir
@vmoravec
vmoravec / spec_parser.rb
Created August 2, 2013 15:51
Parse rpm spec file
# a naive version outputing a map of binaries and packages
class SpecParser
module Tag
REQUIRES = /^Requires:/
BUILD_REQUIRES = /^BuildRequires:/
end
class << self
attr_reader :spec_file

I'm a fan of MiniTest::Spec. It strikes a nice balance between the simplicity of TestUnit and the readable syntax of RSpec. When I first switched from RSpec to MiniTest::Spec, one thing I was worried I would miss was the ability to add matchers. (A note in terminology: "matchers" in MiniTest::Spec refer to something completely different than "matchers" in RSpec. I won't get into it, but from now on, let's use the proper term: "expectations").

Understanding MiniTest::Expectations

Let's take a look in the code (I'm specifically referring to the gem, not the standard library that's built into Ruby 1.9):

# minitest/spec.rb

module MiniTest::Expectations