# Ruby Topic Maps (RTM) http://rtm.rubyforge.org/
module RTM::IO
  # Each Topic Maps Construct gets a custom to_s method which supports optional :short or :long output.
  module TOSTRING

    module TopicMap
      # Returns different String representations. Try :short (default), :long, :super (original)
      def to_s(style=:short)
        case style
        when :short
          "#<RTM::TopicMap id=#{id} base_locator=\"#{base_locator}\">"
        when :long
          r = " #{reifier.to_s(:short)}" if reifier
          i = " iids=#{item_identifiers.to_s}" unless item_identifiers.empty?
          "#<RTM::TopicMap id=#{id} base_locator=\"#{base_locator}\"#{r}#{i} topics:#{topics.size} associations:#{associations.size}>"
        else
          super() # these () are needed, otherwise the own parameters are passed in
        end
      end    
    end

    module Topic
      # Returns different String representations. Try :short (default), :long or something else for the original method.
      # :name returns one (random) name for the Topic or an empty string if the topic has no name
      def to_s(style=:short)
        case style
        when :short
          ssid = " sids=#{subject_identifiers.to_s}" unless subject_identifiers.empty?
          sslo = " slos=#{subject_locators.to_s}" unless subject_locators.empty?
          siid = " iids=#{item_identifiers.to_s}" unless item_identifiers.empty?
          "#<RTM::Topic id=#{id}#{ssid}#{sslo}#{siid}>"
        when :long
          ssid = " sids=#{subject_identifiers.to_s}" unless subject_identifiers.empty?
          sslo = " slos=#{subject_locators.to_s}" unless subject_locators.empty?
          siid = " iids=#{item_identifiers.to_s}" unless item_identifiers.empty?
          "#<RTM::Topic id=#{id}#{ssid}#{sslo}#{siid} names:#{names.size} occurrences:#{occurrences.size}>"
        when :name
          return names.first.to_s(:short) if names.first
          return ""
        else
          super() # these () are needed, otherwise the own parameters are passed in
        end
      end    
    end

    module Association
      def to_s(style=:short)
        case style
        when :short
          r = " #{reifier.to_s(:short)}" if reifier
          i = " iids=#{item_identifiers.to_s}" unless item_identifiers.empty?
          "#<RTM::Association id=#{id}#{i}#{r}>"
        when :long
          r = " #{reifier.to_s(:short)}" if reifier
          i = " iids=#{item_identifiers.to_s}" unless item_identifiers.empty?
          "#<RTM::Association id=#{id}#{i}#{r} type=#{type.to_s(:short)} roles=[#{roles.entries.map { |ar| ar.to_s }.join(", ")}]>"
        else
          super() # these () are needed, otherwise the own parameters are passed in
        end
      end
    end

    module AssociationRole
      def to_s(style=:short)
        case style
        when :short
          r = " #{reifier.to_s(:short)}" if reifier
          i = " iids=#{item_identifiers.to_s}" unless item_identifiers.empty?
          "#<RTM::AssociationRole id=#{id}#{i}#{r}>"
        when :long
          r = " #{reifier.to_s(:short)}" if reifier
          i = " iids=#{item_identifiers.to_s}" unless item_identifiers.empty?
          "#<RTM::Association id=#{id}#{i}#{r} type=#{type.to_s(:short)} player=#{player.to_s(:short)}>"
        else
          super() # these () are needed, otherwise the own parameters are passed in
        end
      end
    end

    module TopicName
      def to_s(style=:short)
        case style
        when :short
          value
        when :long
          r = " #{reifier.to_s(:short)}" if reifier
          i = " iids=#{item_identifiers.to_s}" unless item_identifiers.empty?
          "#<RTM::TopicName id=#{id}#{i}#{r} type=#{type.to_s(:short)} value=#{value} variants:#{variants.size}>"
        else
          super() # these () are needed, otherwise the own parameters are passed in
        end
      end
    end

    module Occurrence
      def to_s(style=:short)
        case style
        when :short
          value
        when :long
          r = " #{reifier.to_s(:short)}" if reifier
          i = " iids=#{item_identifiers.to_s}" unless item_identifiers.empty?
          "#<RTM::Occurrence id=#{id}#{i}#{r} type=#{type.to_s(:short)} value=#{value} datatype=#{datatype}>"
        else
          super() # these () are needed, otherwise the own parameters are passed in
        end
      end
    end

    module Variant
      def to_s(style=:short)
        case style
        when :short
          value
        when :long
          r = " #{reifier.to_s(:short)}" if reifier
          i = " iids=#{item_identifiers.to_s}" unless item_identifiers.empty?
          "#<RTM::Variant id=#{id}#{i}#{r} value=#{value} scope=[scope.to_s]>"
        else
          super() # these () are needed, otherwise the own parameters are passed in
        end
      end
    end
  
    RTM.register_extension( self )
  end
end