command_post 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d72f7139b440a1258cc5ed8fb18d4aa9dc27bcf
|
4
|
+
data.tar.gz: 4e6d5edd913c5163521af6254a1263302fd25a0c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ead892be9876a67322fd188c890bef660351544c04d27dc0b00447ca1014aac5dafafb0e5399a6133af60fef1771f0f038f2b2156bd1fcec0be77a81066a6259
|
7
|
+
data.tar.gz: 12edc1df51fcb7f4342aeecfd29164035660e273912378214d5f04f381a7460fffabeb2ab0ce63091c1a5e8d6c6f05a7fa6bc80a24da3d23d888700c0a93f542
|
@@ -39,7 +39,9 @@ CREATE INDEX aggregate_lookup_idx ON aggregates(aggregate_lookup_value);
|
|
39
39
|
CREATE TABLE aggregate_indexes (
|
40
40
|
aggregate_id BIGINT NOT NULL,
|
41
41
|
index_field VARCHAR(100) NOT NULL,
|
42
|
-
|
42
|
+
index_value_text TEXT,
|
43
|
+
index_value_integer INTEGER,
|
44
|
+
index_value_decimal DECIMAL(20,4),
|
43
45
|
PRIMARY KEY (aggregate_id,index_field)
|
44
46
|
) ;
|
45
47
|
|
@@ -11,12 +11,13 @@ module CommandPost
|
|
11
11
|
class Aggregate
|
12
12
|
|
13
13
|
@@prep_stmt_insert ||= $DB[:aggregates].prepare(:insert, :insert_aggregate, :aggregate_id => :$aggregate_id, :aggregate_type => :$aggregate_type, :content => :$content, :aggregate_lookup_value => :$aggregate_lookup_value )
|
14
|
-
@@prep_stmt_insert_index ||= $DB[:aggregate_indexes].prepare(:insert, :insert_aggregate_indexes, :aggregate_id => :$aggregate_id, :index_field => :$index_field, :index_value => :$index_value )
|
15
|
-
|
16
|
-
|
17
|
-
def self.replace object, aggregate_lookup_value
|
18
14
|
|
15
|
+
def update_index object, index_value , field
|
16
|
+
index_field = "#{object.class.to_s}.#{field.to_s}"
|
17
|
+
$DB["UPDATE aggregate_indexes set #{Persistence.compute_index_column_name(field) } = ? where aggregate_id = ? and index_field = ?", index_value, aggregate_id.to_i, index_field ].update
|
18
|
+
end
|
19
19
|
|
20
|
+
def self.replace object, aggregate_lookup_value
|
20
21
|
content = JSON.generate object
|
21
22
|
aggregate_id = object[:aggregate_info][:aggregate_id]
|
22
23
|
aggregate_type = object[:aggregate_info][:aggregate_type]
|
@@ -31,7 +32,7 @@ module CommandPost
|
|
31
32
|
object.index_fields.each do |field|
|
32
33
|
index_value = object.send field
|
33
34
|
index_field = "#{object.class.to_s}.#{field.to_s}"
|
34
|
-
|
35
|
+
$DB["INSERT INTO aggregate_indexes (aggregate_id , #{Persistence.compute_index_column_name(index_value)}, index_field ) values (?, ?, ?) ", aggregate_id, index_value, index_field ].insert
|
35
36
|
end
|
36
37
|
else
|
37
38
|
$DB["UPDATE aggregates set content = ?, aggregate_lookup_value = ? where aggregate_id = ?", content, aggregate_lookup_value, aggregate_id ].update
|
@@ -39,8 +40,7 @@ module CommandPost
|
|
39
40
|
@@prep_stmt_insert.call(:aggregate_id => aggregate_id.to_i, :aggregate_type => aggregate_type , :content => content, :aggregate_lookup_value => aggregate_lookup_value )
|
40
41
|
object.index_fields.each do |field|
|
41
42
|
index_value = object.send field
|
42
|
-
|
43
|
-
$DB["UPDATE aggregate_indexes set index_value = ? where aggregate_id = ? and index_field = ?", index_value, aggregate_id.to_i, index_field ].update
|
43
|
+
update_index object, index_value, field
|
44
44
|
end
|
45
45
|
end
|
46
46
|
end
|
@@ -2,6 +2,9 @@ require_relative './schema_validation'
|
|
2
2
|
require_relative './data_validation'
|
3
3
|
require_relative './auto_load'
|
4
4
|
|
5
|
+
require 'pry'
|
6
|
+
require 'pry-debugger'
|
7
|
+
|
5
8
|
|
6
9
|
module CommandPost
|
7
10
|
|
@@ -66,34 +69,50 @@ module CommandPost
|
|
66
69
|
|
67
70
|
end
|
68
71
|
|
69
|
-
def self.
|
70
|
-
|
71
|
-
|
72
|
+
def self.listify(values)
|
73
|
+
return values.collect { |x| "'#{x}'"}.join(',') if values.first.is_a? String
|
74
|
+
values.collect { |x| "#{x}" }.join(',')
|
72
75
|
end
|
73
76
|
|
74
77
|
|
75
|
-
def self.
|
78
|
+
def self.get_sql_for_in name, values
|
79
|
+
"SELECT aggregate_id FROM aggregate_indexes WHERE index_field = '#{name}' AND #{self.compute_index_column_name(values.first)} in (#{self.listify(values)}) "
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
def self.get_sql_for_is name , value
|
84
|
+
"SELECT aggregate_id FROM aggregate_indexes WHERE index_field = '#{name}' AND #{self.compute_index_column_name(value)} = ? "
|
85
|
+
end
|
86
|
+
|
76
87
|
|
77
|
-
values = args[0][0]
|
78
|
-
name = "#{self.to_s}.#{index_name.to_s.sub(/_in$/,'')}"
|
79
|
-
sql = get_index_sql name, values
|
80
|
-
results = Array.new
|
81
|
-
$DB.fetch(sql) do |row|
|
82
|
-
results << row[:aggregate_id]
|
83
|
-
end
|
84
88
|
|
85
|
-
|
89
|
+
def self.compute_index_column_name(field)
|
90
|
+
return 'INDEX_VALUE_INTEGER' if field.is_a? Fixnum
|
91
|
+
return 'INDEX_VALUE_DECIMAL' if field.is_a? BigDecimal
|
92
|
+
'INDEX_VALUE_TEXT'
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
def self.get_ids_for_in index_name, *args
|
97
|
+
name = "#{self.to_s}.#{index_name.to_s.sub(/_in$/,'')}"
|
98
|
+
$DB.fetch(get_sql_for_in name, args.last).collect { |row| row[:aggregate_id] }
|
99
|
+
end
|
86
100
|
|
101
|
+
def self.get_ids_for_is index_name, value
|
102
|
+
name = "#{self.to_s}.#{index_name.to_s.sub(/_is$/,'')}"
|
103
|
+
$DB.fetch(get_sql_for_is(name,value),value.to_s).collect { |row| row[:aggregate_id] }
|
87
104
|
end
|
88
105
|
|
89
106
|
|
90
107
|
def self.method_missing nm, *args , &block
|
91
108
|
name = nm.to_s
|
92
|
-
|
93
|
-
|
94
|
-
ids = self.get_ids_for_index nm, args
|
109
|
+
if (name.end_with?('_in') && self.indexes.include?(name.gsub(/_in/,'').to_sym))
|
110
|
+
ids = self.get_ids_for_in nm, args
|
95
111
|
return ids
|
96
|
-
|
112
|
+
elsif (name.end_with?('_is') && self.indexes.include?(name.gsub(/_is/,'').to_sym))
|
113
|
+
ids = self.get_ids_for_is nm, args.last
|
114
|
+
return ids
|
115
|
+
else
|
97
116
|
begin
|
98
117
|
super
|
99
118
|
rescue Exception => e
|
data/lib/command_post/version.rb
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
module CommandPost
|
2
|
-
VERSION = "0.0.
|
2
|
+
VERSION = "0.0.6"
|
3
3
|
end
|
4
4
|
|
5
|
-
#
|
5
|
+
# 1. bump version number in this file
|
6
|
+
# 2. run the command below from the command line.
|
7
|
+
# $ gem build command_post.gemspec
|
8
|
+
# $ gem build command_post-0.0.6.gem
|
9
|
+
|
@@ -1,10 +1,14 @@
|
|
1
1
|
require 'faker'
|
2
2
|
require 'securerandom'
|
3
|
+
|
4
|
+
require 'pry'
|
5
|
+
require 'pry-debugger'
|
6
|
+
|
3
7
|
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
4
8
|
|
5
9
|
|
6
10
|
|
7
|
-
class
|
11
|
+
class TestXXXPerson < CommandPost::Persistence
|
8
12
|
include CommandPost::Identity
|
9
13
|
|
10
14
|
def initialize
|
@@ -15,60 +19,115 @@ class Test100Person < CommandPost::Persistence
|
|
15
19
|
fields[ :first_name ] = { :required => true, :type => String, :location => :local }
|
16
20
|
fields[ :last_name ] = { :required => true, :type => String, :location => :local }
|
17
21
|
fields[ :ssn ] = { :required => true, :type => String, :location => :local }
|
22
|
+
fields[ :state ] = { :required => true, :type => String, :location => :local }
|
18
23
|
fields[ :favorite_number ] = { :required => true, :type => Fixnum, :location => :local }
|
19
24
|
fields[ :lookup ] = { :use => :ssn }
|
20
25
|
fields
|
21
26
|
end
|
22
27
|
def self.indexes
|
23
|
-
[:favorite_number]
|
28
|
+
[:favorite_number, :state]
|
24
29
|
end
|
25
30
|
end
|
26
31
|
|
27
32
|
|
33
|
+
$DB["delete from aggregates"].delete
|
34
|
+
$DB["delete from aggregate_events"].delete
|
35
|
+
$DB["delete from aggregate_indexes"].delete
|
28
36
|
|
29
|
-
describe CommandPost::Identity do
|
30
|
-
it 'should aggregate_ids via index methods' do
|
31
37
|
|
32
38
|
|
39
|
+
1000.times do |i|
|
33
40
|
|
34
|
-
1000.times do |i|
|
35
41
|
|
42
|
+
puts "count = #{i.to_s}"
|
43
|
+
params = Hash.new # like a web request...
|
36
44
|
|
37
|
-
|
38
|
-
|
45
|
+
params['first_name'] = Faker::Name.first_name #hash key is a string to mimic a web post/put
|
46
|
+
params['last_name'] = Faker::Name.last_name #hash key is a string to mimic a web post/put
|
47
|
+
params['ssn'] = "%09d" % CommandPost::SequenceGenerator.misc #hash key is a string to mimic a web post/put
|
48
|
+
params['favorite_number'] = rand(5)
|
49
|
+
params['state'] = Faker::Address.state_abbr
|
39
50
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
51
|
+
#----------------------------------------------------------------
|
52
|
+
# The code below will eventually be replaced by the
|
53
|
+
# 'handle' method of the CommandXXXXXX class.
|
54
|
+
#----------------------------------------------------------------
|
44
55
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
56
|
+
object = TestXXXPerson.load_from_hash TestXXXPerson, params
|
57
|
+
puts "OBJECT IS NIL #{'=' * 80}" if object.nil?
|
58
|
+
event = CommandPost::AggregateEvent.new
|
59
|
+
event.aggregate_id = object.aggregate_id
|
60
|
+
event.object = object
|
61
|
+
event.aggregate_type = TestXXXPerson
|
62
|
+
event.event_description = 'hired'
|
63
|
+
event.user_id = 'test'
|
64
|
+
event.publish
|
49
65
|
|
50
|
-
object = Test100Person.load_from_hash Test100Person, params
|
51
|
-
puts "OBJECT IS NIL #{'=' * 80}" if object.nil?
|
52
|
-
event = CommandPost::AggregateEvent.new
|
53
|
-
event.aggregate_id = object.aggregate_id
|
54
|
-
event.object = object
|
55
|
-
event.aggregate_type = Test100Person
|
56
|
-
event.event_description = 'hired'
|
57
|
-
event.user_id = 'test'
|
58
|
-
event.publish
|
59
66
|
|
67
|
+
#----------------------------------------------------------------
|
68
|
+
# Retrieve the object by both aggregate_id and aggregate_lookup_value
|
69
|
+
# Both ways should retrieve the same object and the fields of both
|
70
|
+
# should match the original values used to create the object.
|
71
|
+
#----------------------------------------------------------------
|
60
72
|
|
61
|
-
#----------------------------------------------------------------
|
62
|
-
# Retrieve the object by both aggregate_id and aggregate_lookup_value
|
63
|
-
# Both ways should retrieve the same object and the fields of both
|
64
|
-
# should match the original values used to create the object.
|
65
|
-
#----------------------------------------------------------------
|
66
73
|
|
74
|
+
saved_person = CommandPost::Aggregate.get_by_aggregate_id TestXXXPerson, event.aggregate_id
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
describe CommandPost::Identity do
|
81
|
+
it 'test 1' do
|
67
82
|
|
68
|
-
|
83
|
+
sql_cnts_in_array = 0
|
69
84
|
|
85
|
+
$DB.fetch("SELECT * FROM aggregate_indexes WHERE index_field = 'TestXXXPerson.favorite_number' and index_value_integer in (1,2,3) " ) do |row|
|
86
|
+
sql_cnts_in_array += 1
|
70
87
|
end
|
88
|
+
|
89
|
+
ids = TestXXXPerson.favorite_number_in(1,2,3)
|
90
|
+
ids.length.must_equal sql_cnts_in_array
|
91
|
+
|
92
|
+
|
71
93
|
end
|
94
|
+
|
95
|
+
it 'should pass test 2' do
|
96
|
+
|
97
|
+
sql_cnts_eq = 0
|
98
|
+
$DB.fetch("SELECT * FROM aggregate_indexes WHERE index_field = 'TestXXXPerson.favorite_number' and index_value_integer = 4 " ) do |row|
|
99
|
+
sql_cnts_eq += 1
|
100
|
+
end
|
101
|
+
|
102
|
+
ids = TestXXXPerson.favorite_number_is(4)
|
103
|
+
ids.length.must_equal sql_cnts_eq
|
104
|
+
sql_cnts_eq.wont_equal 0
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'should pass test 3' do
|
109
|
+
|
110
|
+
mi_counts_eq = 0
|
111
|
+
|
112
|
+
$DB.fetch("SELECT * FROM aggregate_indexes WHERE index_field = 'TestXXXPerson.state' and index_value_text = 'MI' " ) do |row|
|
113
|
+
mi_counts_eq += 1
|
114
|
+
end
|
115
|
+
|
116
|
+
ids = TestXXXPerson.state_is('MI')
|
117
|
+
ids.length.must_equal mi_counts_eq
|
118
|
+
mi_counts_eq.wont_equal 0
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'should pass test 3' do
|
123
|
+
|
124
|
+
puts "count of people from Michigan whose favorite number is even. #{(TestXXXPerson.state_is('MI') & TestXXXPerson.favorite_number_in(2,4)).length}."
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
72
131
|
end
|
73
132
|
|
74
133
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: command_post
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Meirow
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-08-
|
11
|
+
date: 2013-08-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|