swarm 0.2.0 → 0.3.0

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: c08aee506af8648cba7e34c9bf1a0f6771da43fb
4
- data.tar.gz: fa7e78cf7ccb4c0f134623f8001a8782af141164
3
+ metadata.gz: e4410067eb9ac5847b4add5e8a31f9c78f3a6f18
4
+ data.tar.gz: fdf564f1018be4d4ffdd837777c4fd25d1a1664d
5
5
  SHA512:
6
- metadata.gz: 57e8b887897b7b94f34f0ddec8e90a5ef6024ba9e03deeb03ddb05ca7d802a1e438fcf1b657b750a38020112f7dcfb2fd41ea1b5f9dfbcde4bdc20b00bc0a6e4
7
- data.tar.gz: 3cea3598a30320cc93a15d41e827714980b2c2f82bec4e4fe196f7989470be9560662ca6a09f9513ebb67715c83e38b305bf63192c4307084518f9502150c44a
6
+ metadata.gz: 02303d2b8f04002bad616eddbd69ecec4e49f33cd44b4b3785802147c4815ad4f52e318c267a4f7f2de5be8451de48567b01a0ccdb277cfac7b88c140b716a94
7
+ data.tar.gz: ab727019332409f4e5954f8c23c34b62c1b445497b107b60f58d1c12d6fb76e5bc35c1195c0241983dabe9002ef2db007862ebbf24420a87f45dd3cc90fb4b01
@@ -13,8 +13,10 @@ module Swarm
13
13
  end
14
14
  end
15
15
 
16
- set_columns :parent_id, :position, :workitem, :child_ids, :milestones, :process_id
16
+ set_columns :parent_id, :position, :workitem, :children_ids, :milestones, :process_id
17
17
  many_to_one :process, :class_name => "Swarm::Process"
18
+ many_to_one :parent, :class_name => "Swarm::Expression", :key => :parent_id
19
+ one_to_many :children, :class_name => "Swarm::Expression", :foreign_key => :parent_id
18
20
 
19
21
  def branch_position
20
22
  @branch_position ||= position.last
@@ -4,12 +4,6 @@ module Swarm
4
4
  class BranchExpression < Expression
5
5
  class InvalidPositionError < StandardError; end;
6
6
 
7
- def children
8
- (child_ids || []).map { |child_id|
9
- Expression.fetch(child_id, hive: hive)
10
- }
11
- end
12
-
13
7
  def kick_off_children(at_positions)
14
8
  at_positions.each do |at_position|
15
9
  add_and_apply_child(at_position)
@@ -26,7 +20,7 @@ module Swarm
26
20
  node = tree[at_position]
27
21
  raise InvalidPositionError unless node
28
22
  expression = create_child_expression(node: node, at_position: at_position)
29
- (self.child_ids ||= []) << expression.id
23
+ (self.children_ids ||= []) << expression.id
30
24
  expression
31
25
  end
32
26
 
@@ -40,11 +40,11 @@ module Swarm
40
40
  end
41
41
 
42
42
  def traced
43
- storage["trace"] ||= []
43
+ storage.trace ||= []
44
44
  end
45
45
 
46
46
  def trace(new_element)
47
- storage["trace"] = traced + [new_element]
47
+ storage.trace = traced + [new_element]
48
48
  end
49
49
 
50
50
  def queue(action, object)
@@ -116,14 +116,29 @@ module Swarm
116
116
  @columns = @columns | args
117
117
  end
118
118
 
119
- def many_to_one(type, class_name: nil)
119
+ def one_to_many(type, class_name: nil, foreign_key: nil)
120
120
  define_method(type) do
121
121
  memo = instance_variable_get(:"@#{type}")
122
122
  memo || begin
123
- key = self.send(:"#{type}_id")
124
- return nil unless key
123
+ associations = hive.storage.load_associations(
124
+ type, owner: self, type: class_name || type, foreign_key: foreign_key
125
+ )
126
+ entities = associations.map { |association| hive.reify_from_hash(association) }
127
+ instance_variable_set(:"@#{type}", entities)
128
+ end
129
+ end
130
+ @associations << type
131
+ end
132
+
133
+ def many_to_one(type, class_name: nil, key: nil)
134
+ define_method(type) do
135
+ memo = instance_variable_get(:"@#{type}")
136
+ memo || begin
137
+ key ||= :"#{type}_id"
138
+ associated_id = self.send(key)
139
+ return nil unless associated_id
125
140
  klass = Swarm::Support.constantize("#{class_name || type}")
126
- instance_variable_set(:"@#{type}", klass.fetch(key, :hive => hive))
141
+ instance_variable_set(:"@#{type}", klass.fetch(associated_id, :hive => hive))
127
142
  end
128
143
  end
129
144
  @associations << type
@@ -172,7 +187,9 @@ module Swarm
172
187
  end
173
188
 
174
189
  def all(hive: Hive.default, subtypes: true)
175
- to_a(hive: hive, subtypes: subtypes)
190
+ hive.storage.all_of_type(storage_type, subtypes: subtypes).map { |hsh|
191
+ hive.reify_from_hash(hsh.dup)
192
+ }
176
193
  end
177
194
  end
178
195
  end
@@ -5,6 +5,8 @@ module Swarm
5
5
  set_columns :process_definition_id, :workitem, :root_expression_id, :parent_expression_id
6
6
  many_to_one :process_definition, :class_name => "Swarm::ProcessDefinition"
7
7
  many_to_one :parent_expression, :class_name => "Swarm::Expression"
8
+ many_to_one :root_expression, :class_name => "Swarm::Expression"
9
+ one_to_many :expressions, :class_name => "Swarm::Expression"
8
10
 
9
11
  def wait_until_finished(timeout: 5)
10
12
  Swarm::Support.wait_until(timeout: timeout) { finished? }
@@ -16,28 +18,20 @@ module Swarm
16
18
  end
17
19
 
18
20
  def _launch
19
- root_expression = SequenceExpression.create(
21
+ new_expression = SequenceExpression.create(
20
22
  :hive => hive,
21
23
  :parent_id => id,
22
24
  :position => [0],
23
25
  :workitem => workitem,
24
26
  :process_id => id
25
27
  )
26
- root_expression.apply
27
- self.root_expression_id = root_expression.id
28
+ new_expression.apply
29
+ self.root_expression_id = new_expression.id
28
30
  save
29
31
  end
30
32
 
31
- def root_expression
32
- @root_expression ||= begin
33
- reload! unless root_expression_id
34
- if root_expression_id
35
- Expression.fetch(root_expression_id, hive: hive)
36
- end
37
- end
38
- end
39
-
40
33
  def finished?
34
+ reload!
41
35
  root_expression && root_expression.replied?
42
36
  end
43
37
 
@@ -5,6 +5,7 @@ module Swarm
5
5
  class NotYetPersistedError < StandardError; end
6
6
 
7
7
  set_columns :tree, :name, :version
8
+ one_to_many :processes, :class_name => "Swarm::Process"
8
9
 
9
10
  class << self
10
11
  def create_from_json(json, hive: Hive.default)
@@ -8,6 +8,13 @@ module Swarm
8
8
  keys.map { |key| key.gsub(regex_for_type(type), '\1') }
9
9
  end
10
10
 
11
+ def all_of_type(type, subtypes: true)
12
+ store.select { |key, value|
13
+ key.match(regex_for_type(type)) &&
14
+ (subtypes || value["type"] == type)
15
+ }.values
16
+ end
17
+
11
18
  def delete(key)
12
19
  store.delete(key)
13
20
  end
@@ -7,6 +7,14 @@ module Swarm
7
7
  @store = store
8
8
  end
9
9
 
10
+ def trace
11
+ self["trace"]
12
+ end
13
+
14
+ def trace=(traced)
15
+ self["trace"] = traced
16
+ end
17
+
10
18
  def regex_for_type(type)
11
19
  /^#{type}\:(.*)/
12
20
  end
@@ -15,6 +23,21 @@ module Swarm
15
23
  raise "Not implemented yet!"
16
24
  end
17
25
 
26
+ def all_of_type(type)
27
+ raise "Not implemented yet!"
28
+ end
29
+
30
+ def load_associations(association_name, owner:, type:, foreign_key: nil)
31
+ type = type.split("::").last
32
+ local_association_ids = :"#{association_name}_ids"
33
+ if owner.respond_to?(local_association_ids)
34
+ ids = owner.send(local_association_ids) || []
35
+ ids.map { |id|
36
+ self["#{type}:#{id}"]
37
+ }.compact
38
+ end
39
+ end
40
+
18
41
  def serialize(value)
19
42
  return nil if value.nil?
20
43
  value.to_json
@@ -8,6 +8,15 @@ module Swarm
8
8
  store.keys("#{type}:*").map { |key| key.gsub(regex_for_type(type), '\1') }
9
9
  end
10
10
 
11
+ def all_of_type(type, subtypes: true)
12
+ hsh = store.mapped_mget(*store.keys("#{type}:*"))
13
+ if subtypes
14
+ hsh.values
15
+ else
16
+ hsh.select { |key, value| value["type"] == type }.values
17
+ end
18
+ end
19
+
11
20
  def delete(key)
12
21
  store.del(key)
13
22
  end
@@ -43,6 +43,14 @@ module Swarm
43
43
  "#{Time.now.strftime("%Y%m%d-%H%M%S")}-#{SecureRandom.uuid}"
44
44
  end
45
45
 
46
+ def tokenize(string)
47
+ return nil if string.nil?
48
+ string = string.to_s.gsub(/&/, ' and ').
49
+ gsub(/[ \/]+/, '_').
50
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
51
+ downcase
52
+ end
53
+
46
54
  def camelize(string)
47
55
  string = string.sub(/^[a-z\d]*/) { $&.capitalize }
48
56
  string = string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub('/', '::')
@@ -1,3 +1,3 @@
1
1
  module Swarm
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swarm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ravi Gadad
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-17 00:00:00.000000000 Z
11
+ date: 2016-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis