Class: ThingTank::Dependencies
- Inherits:
-
Object
- Object
- ThingTank::Dependencies
- Defined in:
- lib/thingtank/dependencies.rb
Overview
tracks the dependencies of a ThingTank, such as saving of characters, children ThingTanks, registration of characters
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #add_character(character) ⇒ Object
- #add_child(key, child) ⇒ Object
- #already_saved(character_instance) ⇒ Object
- #find_root_doc ⇒ Object
- #has_character?(klass) ⇒ Boolean
-
#initialize(doc) ⇒ Dependencies
constructor
A new instance of Dependencies.
- #inspect ⇒ Object
- #refresh(save = false, with_parent = true) ⇒ Object
- #refresh_parent ⇒ Object
- #register_characters ⇒ Object
- #remove_character(character) ⇒ Object
-
#save_all ⇒ Object
we don’t do this.
- #save_character(character_instance) ⇒ Object
Constructor Details
#initialize(doc) ⇒ Dependencies
Returns a new instance of Dependencies.
5 6 7 8 9 10 11 12 |
# File 'lib/thingtank/dependencies.rb', line 5 def initialize(doc) @doc = doc @registration = [] @save = [] @children = {} #@character_objects = [] #@just_reloaded = [] end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
14 15 16 |
# File 'lib/thingtank/dependencies.rb', line 14 def parent @parent end |
Instance Method Details
#add_character(character) ⇒ Object
49 50 51 |
# File 'lib/thingtank/dependencies.rb', line 49 def add_character(character) @registration << character end |
#add_child(key, child) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/thingtank/dependencies.rb', line 92 def add_child(key,child) raise "something wrong here #{key} #{child.inspect}" unless child.is_a?(ThingTank) || child.is_a?(Array) if child.is_a? Array child.each do |c| c.dependencies.set_parent(@doc) end else child.dependencies.set_parent(@doc) end @children[key] = child end |
#already_saved(character_instance) ⇒ Object
65 66 67 |
# File 'lib/thingtank/dependencies.rb', line 65 def already_saved(character_instance) @save.delete character_instance end |
#find_root_doc ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/thingtank/dependencies.rb', line 104 def find_root_doc() return @doc unless @parent _d = @doc doc = _d while doc.dependencies.parent do doc = doc.dependencies.parent end return doc end |
#has_character?(klass) ⇒ Boolean
57 58 59 |
# File 'lib/thingtank/dependencies.rb', line 57 def has_character?(klass) @registration.include? klass end |
#inspect ⇒ Object
16 17 18 |
# File 'lib/thingtank/dependencies.rb', line 16 def inspect { :registration => @registration, :save => @save, :children => @children }.inspect end |
#refresh(save = false, with_parent = true) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/thingtank/dependencies.rb', line 69 def refresh(save=false, with_parent=true) register_characters @children.each do |key,child| @doc[key] = case child when Array child.collect do |d| d.dependencies.refresh(save, false) d.save if save d.to_character_hash end else child.dependencies.refresh(save, false) child.save if save child.to_character_hash end end refresh_parent() if with_parent end |
#refresh_parent ⇒ Object
88 89 90 |
# File 'lib/thingtank/dependencies.rb', line 88 def refresh_parent() @parent.dependencies.refresh if @parent end |
#register_characters ⇒ Object
20 21 22 |
# File 'lib/thingtank/dependencies.rb', line 20 def register_characters @registration.each { |character| @doc.register_character(character) } end |
#remove_character(character) ⇒ Object
53 54 55 |
# File 'lib/thingtank/dependencies.rb', line 53 def remove_character(character) @registration.delete(character) if has_character?(character) end |
#save_all ⇒ Object
we don’t do this. there is no proper way to update all given characters one must not rely on a character properties after the doc has been manipulated use Character#reload to get the latest from the doc object to the character and Character#reload! to reload the doc from the database and reload the character then def reload_character_objects()
@just_reloaded = []
@character_objects.each do |character|
unless @just_reloaded.include?(character)
@just_reloaded << character
character.reload
end
end
end
44 45 46 47 |
# File 'lib/thingtank/dependencies.rb', line 44 def save_all() refresh true @save.each { |character_instance| character_instance.save } end |
#save_character(character_instance) ⇒ Object
61 62 63 |
# File 'lib/thingtank/dependencies.rb', line 61 def save_character(character_instance) @save << character_instance unless @save.include? character_instance end |