universe_compiler 0.3.3 → 0.3.4

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: 4ea5203e9600c96c96a3bfb287cbf907541fc11f
4
- data.tar.gz: 6c4ad7c180f953ba9b5d7ef5bca9a12d504323c7
3
+ metadata.gz: 703e444fd84e095b79e09bc5dea6e2a4690950c4
4
+ data.tar.gz: ad94bfaacc67708722298fb3e10a4d4988d84975
5
5
  SHA512:
6
- metadata.gz: d12ebd2dafea9bb9ece44239590830cda8db5df6e6e4bc776599ee66b44bb8ea23a24abeee6b38beadf2ed32ddf0051fab2627720d569e0f903553d3619c1fdc
7
- data.tar.gz: e755b450a6d46a789befc9643f1be6e39115d0e2718290a9118638c8090217e8d2f4955b83db8cdae81142b898b0383e4e307bbae659c8816fde30c4f0264956
6
+ metadata.gz: 916ae17c012bfcb90660c7690e2adc86bde93d7cebdcfb8684635d0d7170dd3d4328f3c9b21f372e1aa2f5e35a972d669602bf2325581acfe478c4427cbd3921
7
+ data.tar.gz: 4d4e1acf886009371248702e18eaeac34299e8ddb7a65b3be430b60e91dc686c71ccb0cf59df35405c186729f0979e0417832a839ca12f2137a289ae1a3fd698
@@ -3,21 +3,6 @@ module UniverseCompiler
3
3
 
4
4
  module FieldManagement
5
5
 
6
- attr_accessor :auto_create_fields
7
-
8
- def method_missing(method_name, *args)
9
- if auto_create_fields
10
- candidate_field_name = method_name
11
- method_name.to_s.match (/^(?<field_name>[^=]+)\s*=?$/) do |md|
12
- candidate_field_name = md['field_name'].to_sym
13
- end
14
- define_field_accessor candidate_field_name
15
- raise "Invalid method '#{method_name}' in #{self}" unless self.respond_to? method_name
16
- self.send method_name, *args
17
- else
18
- super(method_name, *args)
19
- end
20
- end
21
6
 
22
7
  private
23
8
 
@@ -33,10 +33,45 @@ module UniverseCompiler
33
33
  end
34
34
 
35
35
  def delete(entity)
36
+ # Remove references to entity
37
+ entities.each do |e|
38
+ e.class.fields_constraints.each do |field_name, constraints|
39
+ if constraints.keys.include? :has_one
40
+ e[field_name] = nil if e[field_name] == entity
41
+ end
42
+ if constraints.keys.include? :has_many
43
+ e[field_name].delete entity if e[field_name].include? entity
44
+ end
45
+ end
46
+ end
47
+ # Then delete the entity
36
48
  entities.delete entity
37
49
  reindex_all entities
38
50
  end
39
51
 
52
+ def replace(entity, by)
53
+ raise UniverseCompiler::Error, "Wrong type, cannot replace '#{entity.as_path}' by '#{by.as_path}' !" unless entity.type == by.type
54
+ raise UniverseCompiler::Error, "Cannot replace '#{entity.as_path}' with '#{by.as_path}' which is alreadyin the universe!" if entity.universe == by.universe
55
+
56
+ # Change references to entity
57
+ entities.each do |e|
58
+ e.class.fields_constraints.each do |field_name, constraints|
59
+ if constraints.keys.include? :has_one
60
+ e[field_name] = by if e[field_name] == entity
61
+ end
62
+ if constraints.keys.include? :has_many
63
+ if e[field_name].map! do |entity_list_item|
64
+ (entity == entity_list_item) ? by : entity_list_item
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ # Then replace the entity
71
+ entities[entities.index(entity)] = by
72
+ reindex_all entities
73
+ end
74
+
40
75
  def clear
41
76
  entities.clear
42
77
  clear_indices
@@ -1,3 +1,5 @@
1
+ require 'securerandom'
2
+
1
3
  module UniverseCompiler
2
4
  module Universe
3
5
 
@@ -15,26 +17,17 @@ module UniverseCompiler
15
17
  end
16
18
 
17
19
  def get_unique_name(seed = DEFAULT_UNIVERSE_NAME)
18
- max_index = 1
19
- universes.keys.each do |universe_name|
20
- universe_name.match /^#{seed}(?:(?: - #)(?<index>\d+))?$/ do |md|
21
- index = md['index'] || '1'
22
- index = index.to_i
23
- max_index = index > max_index ? index : max_index
24
- end
25
- end
26
- if max_index == 1
27
- seed
20
+ if universes[seed]
21
+ format_name seed, SecureRandom.uuid
28
22
  else
29
- UniverseCompiler.logger.debug "Universe #{seed} reached its #{max_index} iteration."
30
- format_name(seed, max_index)
23
+ seed
31
24
  end
32
25
  end
33
26
 
34
27
  private
35
28
 
36
- def format_name(name, index)
37
- '%s - #%d' % [name, index]
29
+ def format_name(name, uuid)
30
+ '%s - #%s' % [name, uuid]
38
31
  end
39
32
 
40
33
  end
@@ -1,3 +1,3 @@
1
1
  module UniverseCompiler
2
- VERSION = '0.3.3'.freeze
2
+ VERSION = '0.3.4'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: universe_compiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Laurent B.