Skip to content

Instantly share code, notes, and snippets.

@adamstrickland
Last active December 27, 2015 15:39
Show Gist options
  • Save adamstrickland/7349109 to your computer and use it in GitHub Desktop.
Save adamstrickland/7349109 to your computer and use it in GitHub Desktop.
class Product
field :traits, type: Array, default: []
after_initialize :load_traits!
on_save :validates_methods
def load_traits!
self.traits.each do |trait|
self << class
self.send(:include, trait.camelize.constantize)
end
end
end
def validates_methods
[:settlement_periods].all?{ |m| self.respond_to?(m) }
end
end
Product.new(name: "Foo", traits: [:nymex_gas])
module SettlementConcepts
module LastDayOfMonth
def settlement_periods(trade)
[(trade.tenor_start - 1.month).end_of_month]
end
end
module LastDayOfEveryMonthInTenor
def settlement_periods(trade)
(trade.tenor_start..trade.tenor_end).to_a.map(&:end_of_month).uniq
end
end
end
class SettlementService
include Molecule::Services::Base
def call(trades, options=nil)
options ||= {}
Hash[*trades.map do |trade|
[trade, trade.settlement_periods]
end.flatten]
end
end
class Trade
delegate :settlement_periods, to: :product
end
module NymexGas
include SettlementConcepts::LastDayOfMonth
include PositionConcepts::CalculateMonthly
include PositionConcepts::SomethingElse
end
module Power
include SettlementConcepts::DayOfFlow
include PositionConcepts::GroupByBlock
end
module Ercot
include PositionConcepts::FullDayBlock
include PositionConcepts::OnPeakAtEightInTheMorning
end
module PJM
include PositionConcepts::FullDayBlock
include PositionConcepts::OnPeakAtSevenInTheMorning
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment