Skip to content

Instantly share code, notes, and snippets.

View vsavkin's full-sized avatar

Victor Savkin vsavkin

View GitHub Profile
@vsavkin
vsavkin / DCI in Groovy
Created April 4, 2011 23:38
A small chunk of code showing how to use mixins to write code in DCI style
//Syntax Sugar-------------------------------------------
Object.metaClass.addRole = {role->
delegate.metaClass.mixin role
}
//Model classes and the database-------------------------
class User {
int id
String name
int age
@vsavkin
vsavkin / testing_framework_in_io.io
Created January 20, 2012 00:34
Testing Framework in IO
AssertionFailed := Exception clone
// --------------------------------------------------------------------------
SpecDefinition := Object clone
SpecDefinition new := method(name,
r := SpecDefinition clone
r name := name
@vsavkin
vsavkin / example1.rb
Created February 13, 2012 00:33
Example 1 for "Messaging for Rubyists"
require 'bunny'
# connecting to a broker
b = Bunny.new({}, {:spec => "09"})
b.start
# setup
e = b.exchange("exchange-01")
q = b.queue("queue-01")
q.bind e
@vsavkin
vsavkin / example2.rb
Created February 13, 2012 00:35
Example 2 for "Messaging for Rubyists"
require 'bunny'
# connecting to a broker
b = Bunny.new({}, {:spec => "09"})
b.start
# setup
e = b.exchange("exchange-02", :durable => true)
q = b.queue("queue-02")
q.bind e
@vsavkin
vsavkin / example3.rb
Created February 13, 2012 00:37
Example 3 for "Messaging for Rubyists"
require 'bunny'
# connecting to a broker
b = Bunny.new({}, {:spec => "09"})
b.start
# setup
e = b.exchange("exchange-03")
q = b.queue("queue-03")
q.bind e
@vsavkin
vsavkin / example4.rb
Created February 13, 2012 00:38
Example 4 for "Messaging for Rubyists"
require 'bunny'
b = Bunny.new({}, {:spec => "09"})
b.start
# setup
e = b.exchange("exchange-04")
q1 = b.queue("queue-04-01")
q1.bind e
@vsavkin
vsavkin / ddd_example1.rb
Created March 4, 2012 21:54
JSON Serialization in Controller
def update
p = Person.find(params[:id])
if p.update_bank_information params[:bank_information]
render :json => p.as_json
else
render :json => "some kind of error"
end
end
@vsavkin
vsavkin / ddd_example1_2.rb
Created March 4, 2012 21:56
PersonJsonSerializer
module PersonJsonSerializer
def self.as_json person
if person.errors.present?
person.as_json(:root => "root-attrs")
else
{:errors => person.errors.full_messages}
end
end
end
@vsavkin
vsavkin / ddd_example1_3.rb
Created March 4, 2012 21:57
Updated Controller
def update
p = Person.find(params[:id])
p.update_bank_information params[:bank_information]
render :json => PersonJsonSerializer.as_json(p)
end
def sell_book
@book = Book.find(params[:id])
if book.sold?
book.errors.add :base, "The book is already sold"
else
book.sell
end
end