class ThingTank::Dependencies

tracks the dependencies of a ThingTank, such as saving of characters, children ThingTanks, registration of characters

Attributes

parent[R]

Public Class Methods

new(doc) click to toggle source
# File lib/thingtank/dependencies.rb, line 5
def initialize(doc)
  @doc = doc
  @registration = []
  @save = []
  @children = {}
  #@character_objects = []
  #@just_reloaded = []
end

Public Instance Methods

add_character(character) click to toggle source
# File lib/thingtank/dependencies.rb, line 49
def add_character(character)
  @registration << character
end
add_child(key,child) click to toggle source
# 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) click to toggle source
# File lib/thingtank/dependencies.rb, line 65
def already_saved(character_instance)
  @save.delete character_instance
end
find_root_doc() click to toggle source
# 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) click to toggle source
# File lib/thingtank/dependencies.rb, line 57
def has_character?(klass)
  @registration.include? klass
end
inspect() click to toggle source
# File lib/thingtank/dependencies.rb, line 16
def inspect
  { :registration => @registration, :save => @save, :children => @children }.inspect
end
refresh(save=false, with_parent=true) click to toggle source
# 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() click to toggle source
# File lib/thingtank/dependencies.rb, line 88
def refresh_parent() 
  @parent.dependencies.refresh if @parent
end
register_characters() click to toggle source
# File lib/thingtank/dependencies.rb, line 20
def register_characters
  @registration.each { |character| @doc.register_character(character) }
end
remove_character(character) click to toggle source
# File lib/thingtank/dependencies.rb, line 53
def remove_character(character)
  @registration.delete(character) if has_character?(character)
end
save_all() click to toggle source
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

# File lib/thingtank/dependencies.rb, line 44
def save_all()
  refresh true
  @save.each { |character_instance| character_instance.save }
end
save_character(character_instance) click to toggle source
# File lib/thingtank/dependencies.rb, line 61
def save_character(character_instance)
  @save << character_instance unless @save.include? character_instance
end

Protected Instance Methods

set_parent(parent) click to toggle source
# File lib/thingtank/dependencies.rb, line 116
def set_parent(parent)
  @parent = parent
end