Skip to content

Instantly share code, notes, and snippets.

View vsavkin's full-sized avatar

Victor Savkin vsavkin

View GitHub Profile
@vsavkin
vsavkin / spock.groovy
Created May 16, 2012 12:00
Spock Example
def "length of Spock's and his friends' names"() {
expect:
name.size() == length
where:
name | length
"Spock" | 5
"Kirk" | 4
"Scotty" | 6
}
def "length of Spock's and his friends' names"() {
assert "Spock".size() == 5
assert "Kirk".size() == 3
assert "Scotty".size() == 6
}
class Person
validates_presence_of :name
has_many :addresses
end
@vsavkin
vsavkin / struct.rb
Created May 16, 2012 12:05
Struct.new
class Person < Struct.new(:name, :age)
def to_s
"#{name} is #{age} years old"
end
end
@vsavkin
vsavkin / dci_in_ruby_role_injection.rb
Created May 31, 2012 00:35
DCI in Ruby (Role Injection)
# Framework
# ===========================================
module ContextAccessor
def context
Thread.current[:context]
end
end
module Context
include ContextAccessor
@vsavkin
vsavkin / dci_in_ruby_without_injection.rb
Created May 31, 2012 00:37
DCI in Ruby (No Role Injection)
# Framework
# ===========================================
module ContextAccessor
def context
Thread.current[:context]
end
end
module Context
include ContextAccessor
@vsavkin
vsavkin / account.rb
Created August 12, 2012 18:48
Account
class Account
def decrease_balance(amount); end
def increase_balance(amount); end
def balance; end
def update_log(message, amount); end
def self.find(id); end
end
@vsavkin
vsavkin / context.rb
Created August 12, 2012 18:49
Context
class TransferringMoney
include Context
def self.transfer source_account_id, destination_account_id, amount
source = Account.find(source_account_id)
destination = Account.find(destination_account_id)
TransferringMoney.new(source, destination).transfer amount
end
attr_reader :source_account, :destination_account
@vsavkin
vsavkin / roles.rb
Created August 12, 2012 18:50
Roles
class TransferringMoney
include Context
...
def transfer amount
...
end
module SourceAccount
include ContextAccssor
@vsavkin
vsavkin / context_and_roles.rb
Created August 12, 2012 18:51
Context & Roles
class TransferringMoney
include Context
def self.transfer source_account_id, destination_account_id, amount
source = Account.find(source_account_id)
destination = Account.find(destination_account_id)
TransferringMoney.new(source, destination).transfer amount
end
attr_reader :source_account, :destination_account