Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
# Lean Architecture example in Ruby - with ContextAccessor
# This example keeps interaction state in a "current context", represented
# by a ContextAccessor module. This can be mixed in to any class that needs
# access to the current context. It is implemented as a thread-local variable.
module ContextAccessor
def context
Thread.current[:context]
@nogtini
nogtini / account_example_without_context.rb
Created August 14, 2019 01:33 — forked from st33n/account_example_without_context.rb
DCI example in Ruby, without ContextAccessor
#!/usr/bin/env ruby
# Lean Architecture example in Ruby - without ContextAccessor
# In this example, the context passes the needed roles into each method it
# invokes, and so the roles have no reference back to the context.
# Model class with no external dependenices. Includes a simple find method
# to create and store instances given an id - for illustration purposes only.
class Account
attr_reader :account_id, :balance
@nogtini
nogtini / gist:49c060b8c2247fb99bf5bc8c71fb572f
Created August 14, 2019 01:53 — forked from bugant/gist:4483304
DCI implemented using SimpleDelegator to save method caching
require 'delegate'
class Account < Struct.new(:owner, :amount)
end
class MoneyTransferContext < Struct.new(:source, :destination)
def transfer(amount)
# applico i ruoli ai modelli "stupidi"
source_account = SourceRole.new(source)
destination_account = DestinationRole.new(destination)
@nogtini
nogtini / mti.rb
Created August 15, 2019 17:38 — forked from StevenJL/mti.rb
class Account < ActiveRecord::Base
def withdraw(amount)
# ...
end
def deposit(amount)
# ...
end
def close!
@nogtini
nogtini / sti.rb
Created August 15, 2019 17:38 — forked from StevenJL/sti.rb
class Account < ActiveRecord::Base
def withdraw(amount)
# ...
end
def deposit(amount)
# ...
end
def close!
class Animal < ActiveRecord::Base
def eat
end
end
class Bird < Animal
def fly
end
end
class Animal < ActiveRecord::Base
def eat
end
end
class Bird < Animal
set_table_name "birds"
def fly
end
# Suppose the AccountHolder parent class is used
# to simply have an association with an account so
# both corporations and people can have accounts.
class Account < ActiveRecord::Base
belongs_to :account_holder
end
class AccountHolder < ActiveRecord::Base
has_one :account
# Under STI, find the top 10 accounts with the greatest balances
# with just one query.
top_accounts = Account.order(balance: :desc).limit(10)
# Under MTI, we need to query all accounts and sort them in memory:
top_corporate_accts = CorporateAccount.order(balance: :desc).limit(10)
top_sb_accts = SmallBusinessAccount.order(balance: :desc).limit(10)
top_personal_accts = PersonalAccount.order(balance: :desc).limit(10)
@font-face {
font-family: SegoeUI;
src:
local("Segoe UI Light"),
url(//c.s-microsoft.com/static/fonts/segoe-ui/west-european/light/latest.woff2) format("woff2"),
url(//c.s-microsoft.com/static/fonts/segoe-ui/west-european/light/latest.woff) format("woff"),
url(//c.s-microsoft.com/static/fonts/segoe-ui/west-european/light/latest.ttf) format("truetype");
font-weight: 100;
}