module ThingTank::SharedMethods

Public Class Methods

included(klass) click to toggle source
# File lib/thingtank/shared_methods.rb, line 3
def self.included(klass)
  klass.class_eval do

    # only export the changed properties
    def changed_to_character_hash(exclude_characters=false)
      skips = skip_keys(exclude_characters)
      hsh = {}
      changes.each do |key,arr|
        _old, _new = arr
        unless skips.include? key
          hsh[key] = _new
        end
      end
      hsh
    end

    def skip_keys(exclude_characters=false)
      arr = %w|_id _rev type update_me updated_at created_at|
      arr << 'characters' if exclude_characters
      arr
    end

    def to_character_hash(exclude_characters=false)
      hsh = to_hash
      skip_keys(exclude_characters).each{ |k|  hsh.delete k }
      hsh
    end

    def nth_character(klass, key, n, &code)
      orig_character = to_character(klass, key)
      list = [orig_character].flatten
      character = case n
      when :last
        list.last
      when :first
        list.first
      else
        list[n]
      end
      if code
        code.call(character)
        character.flush_to_doc
      end
      if orig_character.is_a?(Array)
        character._character_doc.dependencies.refresh_parent
      end
      character
    end

    def last_character(klass, key, &code)
      nth_character(klass, key, :last, &code)
    end

    def first_character(klass, key, &code)
      nth_character(klass, key, :first, &code)
    end

  end
end

Public Instance Methods

changed_to_character_hash(exclude_characters=false) click to toggle source

only export the changed properties

# File lib/thingtank/shared_methods.rb, line 7
def changed_to_character_hash(exclude_characters=false)
  skips = skip_keys(exclude_characters)
  hsh = {}
  changes.each do |key,arr|
    _old, _new = arr
    unless skips.include? key
      hsh[key] = _new
    end
  end
  hsh
end
first_character(klass, key, &code) click to toggle source
# File lib/thingtank/shared_methods.rb, line 56
def first_character(klass, key, &code)
  nth_character(klass, key, :first, &code)
end
last_character(klass, key, &code) click to toggle source
# File lib/thingtank/shared_methods.rb, line 52
def last_character(klass, key, &code)
  nth_character(klass, key, :last, &code)
end
nth_character(klass, key, n, &code) click to toggle source
# File lib/thingtank/shared_methods.rb, line 31
def nth_character(klass, key, n, &code)
  orig_character = to_character(klass, key)
  list = [orig_character].flatten
  character = case n
  when :last
    list.last
  when :first
    list.first
  else
    list[n]
  end
  if code
    code.call(character)
    character.flush_to_doc
  end
  if orig_character.is_a?(Array)
    character._character_doc.dependencies.refresh_parent
  end
  character
end
skip_keys(exclude_characters=false) click to toggle source
# File lib/thingtank/shared_methods.rb, line 19
def skip_keys(exclude_characters=false)
  arr = %w|_id _rev type update_me updated_at created_at|
  arr << 'characters' if exclude_characters
  arr
end
to_character_hash(exclude_characters=false) click to toggle source
# File lib/thingtank/shared_methods.rb, line 25
def to_character_hash(exclude_characters=false)
  hsh = to_hash
  skip_keys(exclude_characters).each{ |k|  hsh.delete k }
  hsh
end