wherever-positions 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.3
1
+ 0.4.4
@@ -0,0 +1,16 @@
1
+ module StringHelper
2
+ class << self
3
+ def add_method_to_id(strings)
4
+ return nil unless strings
5
+ strings.each do |string|
6
+ if string.is_a?(Array)
7
+ add_method_to_id(string)
8
+ else
9
+ def string.to_id
10
+ "#{self}_id"
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
data/lib/wherever.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'mongo'
2
2
  require 'mongoid'
3
+ require 'string_helper'
3
4
  require 'wherever/wherever/accessors'
4
5
  require 'wherever/wherever/adder'
5
6
  require 'wherever/wherever/getter'
@@ -13,8 +13,8 @@ class Configure
13
13
  config.master = mongo_connection
14
14
  end
15
15
 
16
- @keys = options["keys"]
17
- groups = options["key_groups"] || @keys.map{|k| k.gsub(/_id$/,'')}
16
+ @keys = StringHelper.add_method_to_id(options["keys"])
17
+ groups = StringHelper.add_method_to_id(options["key_groups"]) || @keys
18
18
  @key_groups = groups.map{|g| Array(g)}
19
19
  end
20
20
  end
@@ -31,9 +31,10 @@ module DbStore
31
31
 
32
32
  def get_lookup(name, keys)
33
33
  DbStore::Lookup.find_or_create_by(:name => name, :keys => keys) if keys
34
- return "DbStore::Lookup#{name}".constantize if DbStore.constants.include?("Lookup#{name}")
34
+ class_name = "Lookup#{name.titlecase}"
35
+ return "DbStore::#{class_name}".constantize if DbStore.constants.include?(class_name)
35
36
  raise "Missing lookup key from definition" unless keys
36
- build_class("Lookup#{name}", DbStore::VersionConfig, :"lookup_#{name}")
37
+ build_class(class_name, DbStore::VersionConfig, :"lookup_#{name}")
37
38
  end
38
39
 
39
40
  def build_class(name, module_object, store_in)
@@ -5,6 +5,10 @@ module DbStore
5
5
  field :keys, :type => Hash
6
6
  field :lookups, :type => Hash, :default => {}
7
7
  field :current, :type => String
8
+
9
+ def keys
10
+ StringHelper.add_method_to_id(self['keys'])
11
+ end
8
12
  end
9
13
 
10
14
  class Version
@@ -52,8 +52,7 @@ class Wherever
52
52
  def for_group(group_keys, values, id_record)
53
53
  key = {}
54
54
  group_keys.each do |key_values|
55
- k_id = "#{key_values}_id"
56
- key.merge!(k_id => @options["keys"][k_id])
55
+ key.merge!(key_values.to_id => @options["keys"][key_values.to_id])
57
56
  end
58
57
  store = get_key_store(*group_keys)
59
58
  record = store.datasets.find_or_create_by(key)
@@ -5,7 +5,7 @@ class Wherever
5
5
  result = Hash.new(0)
6
6
  if keys.first == "identifier"
7
7
  get_key_store(*keys).datasets.where(selector).all.each do |record|
8
- @grouping.call(result, record.values, result, config)
8
+ @grouping.call(result, record.values, result, keys)
9
9
  end
10
10
  result
11
11
  else
@@ -24,7 +24,7 @@ class Wherever
24
24
  end
25
25
 
26
26
  def check_keys(keys)
27
- invalid_keys = (keys - config.keys)
27
+ invalid_keys = (keys - config.keys.map(&:to_id))
28
28
  raise InvalidSelector,"Unknown Selector: #{invalid_keys.join(' ')}" unless invalid_keys.empty?
29
29
  end
30
30
  end
@@ -16,7 +16,7 @@ class Wherever
16
16
  lookup = get_lookup(name)
17
17
  lookup_data = lookup_class.where(:name => lookup.lookups[marker]).first
18
18
  return 0 unless lookup_data
19
- value_key = lookup.keys.map{|key| data[key]}.join('_')
19
+ value_key = lookup.keys.map{|key| data[key.to_id]}.join('_')
20
20
  lookup_data.values[value_key] || 0
21
21
  end
22
22
  end
@@ -56,16 +56,13 @@ class Wherever
56
56
  config.key_groups.each do |group|
57
57
  if keys & group == keys
58
58
  get_key_store(*group).datasets.all.each do |record|
59
- @grouping.call(record.values, nil, record, nil)
59
+ @grouping.call(record.values, nil, record, group)
60
+ record.save!
60
61
  end
61
- record.save!
62
62
  end
63
63
  end
64
64
  end
65
65
 
66
- def temp(group_keys, values, id_record)
67
- end
68
-
69
66
  def get_lookup_record(name, marker)
70
67
  lookup = get_lookup(name)
71
68
  return [lookup, lookup.versions.find_or_create_by(:name => lookup.lookups[marker])]
@@ -1,7 +1,7 @@
1
1
  class Wherever
2
2
  module Mark
3
3
  def mark(name)
4
- copy_collections("current_identifier", "#{name}_identifier")
4
+ copy_collections("current_identifier_dataset", "#{name}_identifier_dataset")
5
5
  config.key_groups.each do |keys|
6
6
  [:identifier, :dataset].each do |db_type|
7
7
  db_id = "#{keys.join('_')}_#{db_type}"
@@ -2,7 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe "Wherever" do
4
4
  let(:wherever) { Wherever.new("keys" => keys, "database" => 'wherever_test', "key_groups" => key_groups, "key" => "trade_id") }
5
- let(:keys) { ["fund_id"] }
5
+ let(:keys) { ["fund"] }
6
6
  let(:key_groups) { nil }
7
7
 
8
8
  context 'adding the first record' do
@@ -35,7 +35,7 @@ describe "Wherever" do
35
35
  end
36
36
 
37
37
  context 'with a multiple keys' do
38
- let(:keys) { ["fund_id", "security_id"] }
38
+ let(:keys) { ["fund", "security"] }
39
39
  let(:options) { {"unique" => {"trade_id" => 12, "version" => 1}, "keys" => {"fund_id" => 2, "security_id" => 4}} }
40
40
 
41
41
  it 'inserts a record for each key combination' do
@@ -54,7 +54,7 @@ describe "Wherever" do
54
54
  end
55
55
 
56
56
  context 'with a multiple keys and grouping configured' do
57
- let(:keys) { ["fund_id", "security_id"] }
57
+ let(:keys) { ["fund", "security"] }
58
58
  let(:key_groups) { ["fund", "security", ["fund", "security"]] }
59
59
  let(:options) { {"unique" => {"trade_id" => 12, "version" => 1}, "keys" => {"fund_id" => 2, "security_id" => 4}} }
60
60
 
@@ -10,7 +10,7 @@ describe Wherever, 'with a custom grouping' do
10
10
  }
11
11
 
12
12
  context 'add a record' do
13
- let(:keys) { ["fund_id"] }
13
+ let(:keys) { ["fund"] }
14
14
  let(:key_groups) { nil }
15
15
  let(:options) { {"unique" => {"trade_id" => 12, "version" => 1}, "keys" => {"fund_id" => 2}} }
16
16
 
@@ -30,8 +30,8 @@ describe Wherever, 'with a custom grouping' do
30
30
  end
31
31
 
32
32
  context 'get a record' do
33
- let(:keys) { ["fund_id", "security_id"] }
34
- let(:key_groups) { ["fund_id"] }
33
+ let(:keys) { ["fund", "security"] }
34
+ let(:key_groups) { ["fund"] }
35
35
  let(:options_one) { {"unique" => {"trade_id" => 12, "version" => 1}, "keys" => {"fund_id" => 2, "security_id" => 14}} }
36
36
  let(:options_two) { {"unique" => {"trade_id" => 15, "version" => 1}, "keys" => {"fund_id" => 2, "security_id" => 14}} }
37
37
 
@@ -2,7 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe "Wherever" do
4
4
  let(:wherever) { Wherever.new("keys" => keys, "database" => 'wherever_test', "key_groups" => key_groups, "key" => "trade_id") }
5
- let(:keys) { ["fund_id"] }
5
+ let(:keys) { ["fund"] }
6
6
  let(:key_groups) { nil }
7
7
 
8
8
  context 'getting the record data' do
@@ -20,7 +20,7 @@ describe "Wherever" do
20
20
  end
21
21
 
22
22
  context 'a more complex example' do
23
- let(:keys) { ["fund_id", "security_id"] }
23
+ let(:keys) { ["fund", "security"] }
24
24
  let(:key_groups) { ["fund", ["fund", "security"]] }
25
25
  let(:options) { {"unique" => {"trade_id" => 12, "version" => 2}, "keys" => {"fund_id" => 3, "security_id" => 12}} }
26
26
 
@@ -2,7 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe "Wherever" do
4
4
  let(:wherever) { Wherever.new("keys" => keys, "database" => 'wherever_test', "key_groups" => key_groups, "key" => "trade_id") }
5
- let(:keys) { ["fund_id"] }
5
+ let(:keys) { ["fund"] }
6
6
  let(:key_groups) { nil }
7
7
 
8
8
  context 'get the key store' do
@@ -5,7 +5,7 @@ describe Wherever, 'mark a time point' do
5
5
  let(:wherever) {
6
6
  Wherever.new(*create_options) do |values, data, record, keys|
7
7
  price = wherever.get_price('current', record)
8
- if keys.include?("security_id")
8
+ if keys.include?("security")
9
9
  values["position"] += data["position"] if data
10
10
  values["price"] = price
11
11
  end
@@ -16,14 +16,14 @@ describe Wherever, 'mark a time point' do
16
16
  end
17
17
  end
18
18
  }
19
- let(:keys) { ["fund_id", "security_id"] }
19
+ let(:keys) { ["fund", "security"] }
20
20
  let(:key_groups) { ["fund", "security", ["fund", "security"]] }
21
21
 
22
22
  let(:options_one) { {"unique" => {"trade_id" => 12, "version" => 1}, "keys" => {"fund_id" => 2, "security_id" => 4}} }
23
23
  let(:options_two) { {"unique" => {"trade_id" => 14, "version" => 1}, "keys" => {"fund_id" => 2, "security_id" => 4}} }
24
24
 
25
25
  before do
26
- wherever.create_lookup('price', ["security_id"])
26
+ wherever.create_lookup('price', ["security"])
27
27
  wherever.set_price('20111029_01', {4 => 12.5})
28
28
  wherever.add({"position" => 100}, options_one)
29
29
  wherever.mark('COB_20111009')
@@ -2,12 +2,12 @@ require 'spec_helper'
2
2
 
3
3
  describe Wherever, 'setting values' do
4
4
  let(:wherever) { Wherever.new("keys" => keys, "database" => 'wherever_test', "key_groups" => key_groups, "key" => "trade_id") }
5
- let(:keys) { ["fund_id"] }
5
+ let(:keys) { ["fund"] }
6
6
  let(:key_groups) { nil }
7
7
 
8
8
  context 'data storage' do
9
9
  before do
10
- wherever.create_lookup('price', ["security_id"])
10
+ wherever.create_lookup('price', ["security"])
11
11
  end
12
12
 
13
13
  it 'can save and retrieve version data' do
@@ -5,7 +5,7 @@ describe Wherever, 'using lookup in grouping calculation' do
5
5
  let(:wherever) {
6
6
  Wherever.new(*create_options) do |values, data, record, keys|
7
7
  price = wherever.get_price('current', record)
8
- if keys.include?("security_id")
8
+ if keys.include?("security")
9
9
  values["position"] += data["position"] if data
10
10
  values["price"] = price
11
11
  end
@@ -16,7 +16,7 @@ describe Wherever, 'using lookup in grouping calculation' do
16
16
  end
17
17
  end
18
18
  }
19
- let(:keys) { ["fund_id", "security_id"] }
19
+ let(:keys) { ["fund", "security"] }
20
20
  let(:key_groups) { ["fund", "security", ["fund", "security"]] }
21
21
 
22
22
  context 'add record after price' do
@@ -26,7 +26,7 @@ describe Wherever, 'using lookup in grouping calculation' do
26
26
  let(:options_three) { {"unique" => {"trade_id" => 14, "version" => 1}, "keys" => {"fund_id" => 2, "security_id" => 4}} }
27
27
 
28
28
  before do
29
- wherever.create_lookup('price', ["security_id"])
29
+ wherever.create_lookup('price', ["security"])
30
30
  wherever.set_price('current', {4 => 12.5})
31
31
  wherever.add({"position" => 100}, options_one)
32
32
  end
@@ -63,12 +63,12 @@ describe Wherever, 'using lookup in grouping calculation' do
63
63
  let(:options_three) { {"unique" => {"trade_id" => 14, "version" => 1}, "keys" => {"fund_id" => 2, "security_id" => 4}} }
64
64
 
65
65
  before do
66
- wherever.create_lookup('price', ["security_id"])
66
+ wherever.create_lookup('price', ["security"])
67
67
  wherever.add({"position" => 100}, options_one)
68
68
  end
69
69
 
70
70
  it 'on identifier record' do
71
- wherever.create_lookup('price', ["security_id"])
71
+ wherever.create_lookup('price', ["security"])
72
72
  wherever.set_price('current', {4 => 12.5})
73
73
  wherever.get_key_store("identifier").datasets.all.should ==
74
74
  [DbStore::Dataset.new("values" => {"position" => 100, "price" => 12.5, "trade_value" => 1250},
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{wherever-positions}
8
- s.version = "0.4.3"
8
+ s.version = "0.4.4"
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"]
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
30
30
  "features/support/env.rb",
31
31
  "features/wherever.feature",
32
32
  "lib/monkeypatch_for_mongoid_1.9.rb",
33
+ "lib/string_helper.rb",
33
34
  "lib/wherever.rb",
34
35
  "lib/wherever/configure.rb",
35
36
  "lib/wherever/db_store.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: 9
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 3
10
- version: 0.4.3
9
+ - 4
10
+ version: 0.4.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - David Henry
@@ -173,6 +173,7 @@ files:
173
173
  - features/support/env.rb
174
174
  - features/wherever.feature
175
175
  - lib/monkeypatch_for_mongoid_1.9.rb
176
+ - lib/string_helper.rb
176
177
  - lib/wherever.rb
177
178
  - lib/wherever/configure.rb
178
179
  - lib/wherever/db_store.rb