Skip to content

Instantly share code, notes, and snippets.

View nogtini's full-sized avatar

Joey Di Nardo nogtini

  • Virginia Beach, VA
View GitHub Profile
# How expensive is DCI in Ruby?
# http://news.ycombinator.com/item?id=4997498
RUBY_VERSION # => "1.9.3"
RUBY_PATCHLEVEL # => 362
RUBY_PLATFORM # => "x86_64-darwin12.2.0"
require 'benchmark'
require 'delegate'
require 'forwardable'
@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;
}
# 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)
# 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
class Animal < ActiveRecord::Base
def eat
end
end
class Bird < Animal
set_table_name "birds"
def fly
end
class Animal < ActiveRecord::Base
def eat
end
end
class Bird < Animal
def fly
end
end
@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!
@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 / 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 / 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