scheherazade 0.1.2 → 0.1.3

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: d624c9331ac258bae8c1cad405214505c3295cc3
4
- data.tar.gz: d2c0f723ce6c5bf83bd92eacbb8623edc880bf23
3
+ metadata.gz: 1e64766948d3f9ebcce55c189ae83e8ed4a03dd0
4
+ data.tar.gz: 639cee4cc9df6a85c476367a480fd1ccf078f890
5
5
  SHA512:
6
- metadata.gz: caa91c5084c99072d4c8cc0ecf6cdbec4a980d36767979ff3481f73a7585b6e6176d09fcbcaf037669e9eda9ba7a394ecf64e967f7b38175d71960a8a9ff6867
7
- data.tar.gz: 3e46fff4918ed4eb9a77c58be70bcbf54025113be08a085806a4852a2c73000caa941278e8fbeab48245fed4e9b193048bfe0deb8d66040718babe27256e5731
6
+ metadata.gz: 24f11fccef514fa2ce5db387bc0e3e8cfdcf65c0d4c254dc7851fef220b2a9beae14102168c70ee15feb3ad6fd5d5bbaf559afbcbc20affc7c78629b21e1487c
7
+ data.tar.gz: 9382d464cebedb64bd10bbdacf75026f32e19208d4bfbcc898b4e640f8f9bad60b7c789e73d30862cbaad51bd3db77d4e5a8faea8f07841d14979a10dd7c51e2
@@ -1,3 +1,5 @@
1
- %w[current_hash log story character_builder extension].each do |lib|
1
+ require 'active_record'
2
+
3
+ %w[log story character_builder extension].each do |lib|
2
4
  require_relative lib
3
5
  end
@@ -61,8 +61,16 @@ module Scheherazade
61
61
 
62
62
  def initialize(parent = self.class.current)
63
63
  super(){|h, k| parent[k] if parent }
64
+ @borrowed = []
64
65
  @parent = parent
65
- @current = CurrentHash.new(self)
66
+ @current = Hash.new do |h, k|
67
+ if (char = to_character!(k))
68
+ h[char]
69
+ else
70
+ h[k] = borrow(parent.current[k]) if parent
71
+ end
72
+ end
73
+
66
74
  @fill_attributes = Hash.new{|h, k| parent.fill_attributes[k] if parent }
67
75
  @characters = Hash.new {|h, k| parent.characters[k] if parent }
68
76
  @counter = parent ? parent.counter.dup : Hash.new(0)
@@ -175,9 +183,14 @@ module Scheherazade
175
183
  end
176
184
  end
177
185
 
186
+ def borrow(ar_instance)
187
+ @borrowed << ar_instance if ar_instance
188
+ ar_instance
189
+ end
190
+
178
191
  def rollback(hard)
179
- current.restore
180
192
  @built.each(&:destroy) if hard
193
+ @borrowed.each(&:reload)
181
194
  end
182
195
 
183
196
  def building(ar)
@@ -1,3 +1,3 @@
1
1
  module Scheherazade
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scheherazade
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marc-André Lafortune
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-19 00:00:00.000000000 Z
11
+ date: 2015-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -66,7 +66,6 @@ files:
66
66
  - lib/scheherazade.rb
67
67
  - lib/scheherazade/bare.rb
68
68
  - lib/scheherazade/character_builder.rb
69
- - lib/scheherazade/current_hash.rb
70
69
  - lib/scheherazade/extension.rb
71
70
  - lib/scheherazade/log.rb
72
71
  - lib/scheherazade/story.rb
@@ -1,55 +0,0 @@
1
- module Scheherazade
2
-
3
- # A subclass of Hash, with automatic lookup up
4
- # the story chain and ability to restore the characters
5
- #
6
- class CurrentHash < Hash
7
- def initialize(story)
8
- @restore = []
9
- super() do |h, k|
10
- if (char = story.to_character!(k))
11
- h[char]
12
- else
13
- if up = story.parent
14
- h[k] = remember(up.current[k])
15
- end
16
- end
17
- end
18
- end
19
-
20
- # restore all characters borrowed to parent stories
21
- # to their original state.
22
- #
23
- def restore
24
- @restore.each_slice(3) do |obj, instance_variable_names, values|
25
- instance_variable_names.zip(values) do |iv, val|
26
- obj.instance_variable_set(iv, val)
27
- end
28
- end
29
- self
30
- end
31
-
32
- protected
33
-
34
- # Implementation note: the reason why we remember the object
35
- # and then reset it, instead of attempting to clone it is
36
- # that active record doesn't implement a meaningful clone
37
- # and dup creates a different object altogether.
38
- #
39
- # Attempting to clone an AR object, including it's loaded relations
40
- # would require playing too close to the metal and would be too brittle
41
- #
42
- def remember(object)
43
- return object unless object
44
- ivs = object.instance_variables
45
- @restore << object << ivs << ivs.map do |iv|
46
- v = object.instance_variable_get(iv)
47
- v = v.clone if v.duplicable?
48
- v
49
- end
50
- object
51
- end
52
-
53
- end
54
-
55
- end