wherever-positions 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.5.1
@@ -7,6 +7,7 @@ require 'wherever/wherever/adder'
7
7
  require 'wherever/wherever/getter'
8
8
  require 'wherever/wherever/lookup'
9
9
  require 'wherever/wherever/mark'
10
+ require 'wherever/wherever/recalculate'
10
11
  require 'wherever/wherever'
11
12
  require 'wherever/configure'
12
13
  require 'wherever/wherever/adder'
@@ -7,6 +7,7 @@ class Wherever
7
7
  include Adder
8
8
  include Getter
9
9
  include Lookup
10
+ include Recalculate
10
11
  include Mark
11
12
  attr_reader :config, :marker
12
13
 
@@ -24,9 +25,17 @@ class Wherever
24
25
  end
25
26
  end
26
27
  end
27
-
28
+ @summing = {}
28
29
  if options['method_logging']
29
30
  MethodLogging.add(self.class)
30
31
  end
31
32
  end
33
+
34
+ def add_grouping(&grouping)
35
+ @grouping = grouping
36
+ end
37
+
38
+ def add_summing(key, options)
39
+ @summing[key] = options
40
+ end
32
41
  end
@@ -37,34 +37,7 @@ class Wherever
37
37
  lookup.lookups[get_marker(keys)] = (version || lookup.current)
38
38
  lookup.save
39
39
  end
40
-
41
- def recalculate(name)
42
- keys = get_lookup(name).keys
43
- config.key_groups.each do |group|
44
- if keys & group != keys
45
- get_key_store(*group).datasets.delete_all
46
- end
47
- end
48
-
49
- identifier_set.datasets.all.each do |record|
50
- @grouping.call(record.values, nil, record, config.keys)
51
- record.save!
52
- config.key_groups.each do |group|
53
- if keys & group != keys
54
- for_group(group, record.values, record, false)
55
- end
56
- end
57
- end
58
- config.key_groups.each do |group|
59
- if keys & group == keys
60
- get_key_store(*group).datasets.all.each do |record|
61
- @grouping.call(record.values, nil, record, group)
62
- record.save!
63
- end
64
- end
65
- end
66
- end
67
-
40
+
68
41
  def get_lookup_record(name, marker)
69
42
  lookup = get_lookup(name)
70
43
  return [lookup, lookup.versions.find_or_create_by(:name => lookup.lookups[marker])]
@@ -0,0 +1,57 @@
1
+ class Wherever
2
+ module Recalculate
3
+ def recalculate(name)
4
+ keys = get_lookup(name).keys
5
+
6
+ recalculate_identifier
7
+
8
+ groups = config.key_groups
9
+ groups_with_key, groups_without_key = groups.partition do |group|
10
+ group & keys == keys
11
+ end
12
+
13
+ groups_with_key.each do |group|
14
+ recalculate_with_key(group)
15
+ end
16
+
17
+ @summing.each do |group, options|
18
+ groups_without_key.delete(group)
19
+ get_key_store(*group).datasets.delete_all
20
+ sum_group_for(StringHelper.add_method_to_id(group, false), options)
21
+ end
22
+ groups_without_key.each do |group|
23
+ get_key_store(*group).datasets.delete_all
24
+ identifier_set.datasets.all.each do |record|
25
+ for_group(group, record.values, record, false)
26
+ end
27
+ end
28
+ end
29
+
30
+ def sum_group_for(group, options)
31
+ group_by = options['group_by'] || lambda do |record|
32
+ group.inject({}) { |r, v| r[v.to_id] = record[v.to_id]; r }
33
+ end
34
+ summer = options['summer'] || raise('Summer must be set.')
35
+ source = options['source'] || ['identifier']
36
+
37
+ grouped = get_key_store(*source).datasets.all.group_by{|record| group_by.call(record)}
38
+ grouped.map do |key, records|
39
+ get_key_store(*group).datasets.create(key.merge(summer.call(records)))
40
+ end
41
+ end
42
+
43
+ def recalculate_identifier
44
+ identifier_set.datasets.all.each do |record|
45
+ @grouping.call(record.values, nil, record, config.keys)
46
+ record.save!
47
+ end
48
+ end
49
+
50
+ def recalculate_with_key(group)
51
+ get_key_store(*group).datasets.all.each do |record|
52
+ @grouping.call(record.values, nil, record, group)
53
+ record.save!
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Wherever, 'with a custom grouping' do
4
+ let(:create_options) { ["keys" => keys, "database" => 'wherever_test', "key_groups" => key_groups, "key" => "trade_id"] }
5
+ let(:wherever) { Wherever.new(*create_options) }
6
+
7
+ before do
8
+ wherever.add_grouping do |values, data, record, keys|
9
+ values["settled"] += data["settled"]
10
+ values["unsettled"] += data["settled"] * -2.5
11
+ end
12
+ end
13
+
14
+ context 'add a record' do
15
+ let(:keys) { ["fund"] }
16
+ let(:key_groups) { nil }
17
+ let(:options) { {"unique" => {"trade_id" => 12, "version" => 1}, "keys" => {"fund_id" => 2}} }
18
+
19
+ before do
20
+ wherever.add({"settled" => 100, "unsettled" => 0}, options)
21
+ end
22
+
23
+ it 'the identifier record' do
24
+ wherever.get_key_store("identifier").datasets.all.should ==
25
+ [DbStore::Dataset.new("values" => {"unsettled" => -250, "settled" => 100}, "fund_id" => 2, "version" => 1, "trade_id" => 12)]
26
+ end
27
+
28
+ it 'the grouped record' do
29
+ wherever.get_key_store("fund").datasets.all.should ==
30
+ [DbStore::Dataset.new("values" => {"unsettled" => -250, "settled" => 100}, "fund_id" => 2)]
31
+ end
32
+ end
33
+
34
+ context 'get a record' do
35
+ let(:keys) { ["fund", "security"] }
36
+ let(:key_groups) { ["fund"] }
37
+ let(:options_one) { {"unique" => {"trade_id" => 12, "version" => 1}, "keys" => {"fund_id" => 2, "security_id" => 14}} }
38
+ let(:options_two) { {"unique" => {"trade_id" => 15, "version" => 1}, "keys" => {"fund_id" => 2, "security_id" => 14}} }
39
+
40
+ before do
41
+ wherever.add({"settled" => 100, "unsettled" => 0}, options_one)
42
+ end
43
+
44
+ it 'gets the value for a non key field' do
45
+ wherever.get("security_id" => 14).should == {"settled" => 100, "unsettled" => -250}
46
+ end
47
+
48
+ it 'gets the value for a non key field when multiple records' do
49
+ wherever.add({"settled" => 200, "unsettled" => 0}, options_two)
50
+ wherever.get("security_id" => 14).should == {"settled" => 300, "unsettled" => -750}
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe Wherever, 'with a custom grouping' do
4
+ let(:create_options) { ["keys" => keys, "database" => 'wherever_test', "key_groups" => key_groups, "key" => "trade_id"] }
5
+ let(:wherever) { Wherever.new(*create_options) }
6
+
7
+ before do
8
+ wherever.add_grouping do |values, data, record, keys|
9
+ if data
10
+ values["settled"] += data["settled"]
11
+ values["unsettled"] += data["settled"] * -2.5
12
+ end
13
+ end
14
+
15
+ summer = lambda do |records|
16
+ settled = records.sum{|r| r.values['settled']}
17
+ unsettled = records.sum{|r| r.values['unsettled']}
18
+ {'values' => {'settled' => 2 * settled, 'unsettled' => 2 * unsettled } }
19
+ end
20
+ wherever.add_summing(['fund', 'technical_instrument'], 'summer' => summer)
21
+ wherever.add_summing(['fund'], 'summer' => summer)
22
+ wherever.create_lookup('price', ["technical_instrument"])
23
+ end
24
+
25
+ context 'add a record' do
26
+ let(:keys) { ["fund"] }
27
+ let(:key_groups) { nil }
28
+ let(:options) { {"unique" => {"trade_id" => 12, "version" => 1}, "keys" => {"fund_id" => 2, "technical_instrument_id" => 14}} }
29
+
30
+ before do
31
+ wherever.add({"settled" => 100, "unsettled" => 0}, options)
32
+ wherever.set_price('current', {4 => 12.5})
33
+ end
34
+
35
+ it 'the identifier record' do
36
+ wherever.get_key_store("identifier").datasets.all.to_a.should ==
37
+ [DbStore::Dataset.new("values" => {"unsettled" => -250, "settled" => 100},
38
+ "fund_id" => 2, "technical_instrument_id" => 14, "version" => 1, "trade_id" => 12)]
39
+ end
40
+
41
+ it 'the grouped record' do
42
+ wherever.get_key_store("fund").datasets.all.to_a.should ==
43
+ [DbStore::Dataset.new("values" => {"unsettled" => -500, "settled" => 200}, "fund_id" => 2)]
44
+ end
45
+ end
46
+
47
+ context 'get a record' do
48
+ let(:keys) { ["fund", "technical_instrument"] }
49
+ let(:key_groups) { ["fund"] }
50
+ let(:options_one) { {"unique" => {"trade_id" => 12, "version" => 1}, "keys" => {"fund_id" => 2, "technical_instrument_id" => 14}} }
51
+ let(:options_two) { {"unique" => {"trade_id" => 15, "version" => 1}, "keys" => {"fund_id" => 2, "technical_instrument_id" => 14}} }
52
+
53
+ before do
54
+ wherever.add({"settled" => 100, "unsettled" => 0}, options_one)
55
+ wherever.set_price('current', {4 => 12.5})
56
+ end
57
+
58
+ it 'gets the value for a non key field' do
59
+ wherever.get("technical_instrument_id" => 14).should == {"settled" => 100, "unsettled" => -250}
60
+ end
61
+
62
+ it 'gets the value for a non key field when multiple records' do
63
+ wherever.add({"settled" => 200, "unsettled" => 0}, options_two)
64
+ wherever.get("technical_instrument_id" => 14).should == {"settled" => 300, "unsettled" => -750}
65
+ end
66
+ end
67
+ end
@@ -1,29 +1,30 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Wherever, 'run file test' do
4
- let(:wherever) do
5
- Wherever.new("keys" => keys, "database" => 'wherever_test',
6
- "key_groups" => key_groups, "key" => "trade_id") do |values, data, record, keys|
4
+ let(:wherever) { Wherever.new("keys" => keys, "database" => 'wherever_test',
5
+ "key_groups" => key_groups, "key" => "trade_id") }
6
+ let(:keys) { ["fund", "technical_instrument"] }
7
+ let(:key_groups) { [["fund"], ["fund", "technical_instrument"]] }
8
+
9
+ before do
10
+ wherever.add_grouping do |values, data, record, keys|
7
11
  price = wherever.get_price('current', record)
8
- if keys.include?("technical_instrument")
9
- values["position"] ||= 0
10
- values["position"] += data["position"] if data
11
- values["price"] = price
12
- end
12
+ values["price"] = price
13
13
  if data
14
+ values["position"] ||= 0
15
+ values["position"] += data["position"]
14
16
  values["trade_value"] ||= 0
15
- values["trade_value"] += data["position"] * price
17
+ values["trade_value"] += data["position"] * price
16
18
  else
17
- debugger unless values["position"]
18
- debugger unless price
19
19
  values["trade_value"] = values["position"] * price
20
20
  end
21
21
  end
22
- end
23
- let(:keys) { ["fund", "technical_instrument"] }
24
- # let(:key_groups) { [["fund"], ["fund", "technical_instrument"]] }
25
- let(:key_groups) { [["fund"]] }
26
- before do
22
+
23
+ summer = lambda do |records|
24
+ trade_value = records.sum{|r| r.values['trade_value']}
25
+ {'values' => {'trade_value' => trade_value } }
26
+ end
27
+ wherever.add_summing(['fund'], 'summer' => summer, 'source' => ['fund', 'technical_instrument'])
27
28
  wherever.create_lookup('price', ["technical_instrument"])
28
29
  end
29
30
 
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{wherever-positions}
8
- s.version = "0.5.0"
8
+ s.version = "0.5.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["David Henry"]
@@ -45,9 +45,12 @@ Gem::Specification.new do |s|
45
45
  "lib/wherever/wherever/getter.rb",
46
46
  "lib/wherever/wherever/lookup.rb",
47
47
  "lib/wherever/wherever/mark.rb",
48
+ "lib/wherever/wherever/recalculate.rb",
48
49
  "spec/data/test_file_1.script",
49
50
  "spec/method_logging_spec.rb",
50
51
  "spec/spec_helper.rb",
52
+ "spec/wherever/add_grouping_spec.rb",
53
+ "spec/wherever/add_summing_spec.rb",
51
54
  "spec/wherever/adder_spec.rb",
52
55
  "spec/wherever/custom_grouping_spec.rb",
53
56
  "spec/wherever/full_run_spec.rb",
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wherever-positions
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 0
10
- version: 0.5.0
9
+ - 1
10
+ version: 0.5.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - David Henry
@@ -202,9 +202,12 @@ files:
202
202
  - lib/wherever/wherever/getter.rb
203
203
  - lib/wherever/wherever/lookup.rb
204
204
  - lib/wherever/wherever/mark.rb
205
+ - lib/wherever/wherever/recalculate.rb
205
206
  - spec/data/test_file_1.script
206
207
  - spec/method_logging_spec.rb
207
208
  - spec/spec_helper.rb
209
+ - spec/wherever/add_grouping_spec.rb
210
+ - spec/wherever/add_summing_spec.rb
208
211
  - spec/wherever/adder_spec.rb
209
212
  - spec/wherever/custom_grouping_spec.rb
210
213
  - spec/wherever/full_run_spec.rb