module ThingTank::InstanceMethods

Public Class Methods

included(base) click to toggle source
# File lib/thingtank/instance_methods.rb, line 5
def self.included(base)

  base.class_eval do

    def initialize(*args)
      super
      @dependencies = ThingTank::Dependencies.new(self)
    end

    def dependencies
      @dependencies
    end

    def to_sym
      if id = self.id
        "!##{id}"
      else
        nil
      end
    end

    # it is not possible for whatever reason to overwrite valid?, check it out!
    #def valid?
    #  invalid_characters.empty? && self.errors.messages.empty?
    #end

    # 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
    #  super
    #  @dependencies.reload_character_objects()
    #end

    def first(key)
      [self[key.to_s]].flatten.last
    end
  
    def last(key)
      [self[key.to_s]].flatten.first
    end

    # export a property to its own document and replaces the original reference with the doc["_id"]
    def export(property)
      raise ArgumentError, "doc.database required for extracting" unless database
      new_doc = self[property.to_s]
      raise ArgumentError, "#{new_doc.inspect} is no CouchRest::Document or Hash" unless new_doc.is_a?(CouchRest::Document) or new_doc.is_a?(Hash)
      result = database.save_doc new_doc
      if result['ok']
        self["#{property}_id"] = result["id"]
        self[property.to_s] = nil
      end
      result['ok']
    end

  end

end
new(*args) click to toggle source
Calls superclass method
# File lib/thingtank/instance_methods.rb, line 9
def initialize(*args)
  super
  @dependencies = ThingTank::Dependencies.new(self)
end

Public Instance Methods

dependencies() click to toggle source
# File lib/thingtank/instance_methods.rb, line 14
def dependencies
  @dependencies
end
export(property) click to toggle source

export a property to its own document and replaces the original reference with the doc

# File lib/thingtank/instance_methods.rb, line 48
def export(property)
  raise ArgumentError, "doc.database required for extracting" unless database
  new_doc = self[property.to_s]
  raise ArgumentError, "#{new_doc.inspect} is no CouchRest::Document or Hash" unless new_doc.is_a?(CouchRest::Document) or new_doc.is_a?(Hash)
  result = database.save_doc new_doc
  if result['ok']
    self["#{property}_id"] = result["id"]
    self[property.to_s] = nil
  end
  result['ok']
end
first(key) 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

super
@dependencies.reload_character_objects()

end

# File lib/thingtank/instance_methods.rb, line 39
def first(key)
  [self[key.to_s]].flatten.last
end
last(key) click to toggle source
# File lib/thingtank/instance_methods.rb, line 43
def last(key)
  [self[key.to_s]].flatten.first
end
to_sym() click to toggle source
# File lib/thingtank/instance_methods.rb, line 18
def to_sym
  if id = self.id
    "!##{id}"
  else
    nil
  end
end