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;
}
@nogtini
nogtini / cols_unique.rb
Created August 15, 2019 17:47
AR Uniqueness of Multiple Columns
# https://stackoverflow.com/questions/34424154/rails-validate-uniqueness-of-two-columns-together
# answer 1
class AddUniqueIndexToReleases < ActiveRecord::Migration
def change
add_index :releases, [:country, :medium], unique: true
end
end
class Release < ActiveRecord::Base
# 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)