Skip to content

Commit 28653ab

Browse files
committed
Refactoring
1 parent 6d438ca commit 28653ab

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

lib/model/instrument.ml

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ module Instrument : Instrument_sig = struct
2121

2222
module Ccy = struct
2323
type t = {
24-
base: instrument_base;
2524
instrument_type: instrument_type;
2625
}
2726
end
2827

2928
module Equity = struct
3029
type t = {
31-
base: instrument_base;
3230
instrument_type: instrument_type;
3331
issue_date: CalendarLib.Calendar.t;
3432
unit_price: unit_price;
@@ -37,7 +35,6 @@ module Instrument : Instrument_sig = struct
3735

3836
module FixedIncome = struct
3937
type t = {
40-
base: instrument_base;
4138
instrument_type: instrument_type;
4239
issue_date: CalendarLib.Calendar.t;
4340
maturity_date: CalendarLib.Calendar.t option;
@@ -46,11 +43,16 @@ module Instrument : Instrument_sig = struct
4643
}
4744
end
4845

49-
type t =
46+
type custom =
5047
| Ccy of Ccy.t
5148
| Equity of Equity.t
5249
| FixedIncome of FixedIncome.t
5350

51+
type t = {
52+
base: instrument_base;
53+
custom: custom;
54+
}
55+
5456
let build_instrument_base isin name lot_size = {
5557
isin;
5658
name;
@@ -70,52 +72,47 @@ module Instrument : Instrument_sig = struct
7072
let ccy ~isin ~name =
7173
let valid = validate_base ~isin ~name ~lot_size: 1 in
7274
match valid with
73-
| Ok base -> Ok (Ccy { base = base; instrument_type = CCY })
75+
| Ok base -> Ok { base = base; custom = Ccy { instrument_type = CCY } }
7476
| Error e -> Error e
7577

7678

7779
let equity ~isin ~name ~lot_size ~unit_price ~issue_date =
7880
let valid = validate_base ~isin ~name ~lot_size in
7981
match valid with
80-
| Ok base -> Ok (
81-
Equity {
82-
base = base;
82+
| Ok base -> Ok {
83+
base = base;
84+
custom = Equity {
8385
instrument_type = Equity;
8486
issue_date = issue_date;
8587
unit_price = unit_price
8688
}
87-
)
89+
}
8890
| Error e -> Error e
8991

9092

9193
let fixed_income ~isin ~name ~lot_size ~issue_date ~maturity_date ~coupon_rate ~coupon_frequency =
9294
let valid = validate_base ~isin ~name ~lot_size in
9395
match valid with
94-
| Ok base -> Ok (
95-
FixedIncome {
96-
base = base;
96+
| Ok base -> Ok {
97+
base = base;
98+
custom = FixedIncome {
9799
instrument_type = FixedIncome;
98100
issue_date = issue_date;
99101
maturity_date = maturity_date;
100102
coupon_rate = coupon_rate;
101103
coupon_frequency = coupon_frequency;
102104
}
103-
)
105+
}
104106
| Error e -> Error e
105107

106-
let get_instrument_type = function
107-
| Ccy _ -> CCY
108-
| Equity _ -> Equity
109-
| FixedIncome _ -> FixedIncome
108+
let get_instrument_type instrument =
109+
match instrument.custom with
110+
| Ccy ccy -> ccy.instrument_type
111+
| Equity equity -> equity.instrument_type
112+
| FixedIncome fixed_income -> fixed_income.instrument_type
110113

111-
let get_isin = function
112-
| Ccy ccy -> ccy.base.isin
113-
| Equity equity -> equity.base.isin
114-
| FixedIncome fixed_income -> fixed_income.base.isin
114+
let get_isin instrument = instrument.base.isin
115115

116-
let get_name = function
117-
| Ccy ccy -> ccy.base.name
118-
| Equity equity -> equity.base.name
119-
| FixedIncome fixed_income -> fixed_income.base.name
116+
let get_name instrument = instrument.base.name
120117

121118
end

0 commit comments

Comments
 (0)