# Ruby Topic Maps (RTM) http://rtm.rubyforge.org/
module RTM::IO
  # YAML Export.
  # Each Topic Maps Construct gets a to_xtm2 method.
  module TOYAML

    module TopicMap
      def to_yaml_hash(*a)
        y={}
        #j['base_locator'] = base_locator, # with absolute item_identifiers its not needed
        y['reifier'] = reifier.to_yaml_ref if reifier
        y['item_identifiers'] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
        y['topics'] = topics.map{|i| i.to_yaml_hash} unless topics.empty?
        y['associations'] = associations.map{|i| i.to_yaml_hash} unless associations.empty?
        y
      end
      # returns the YAML representation of this topic map
      def to_yaml(*a)
        to_yaml_hash.to_yaml(*a)
      end
    end

    module Topic
      # returns the YAML representation of this topic
      def to_yaml_hash(*a)
        y={}
        y['item_identifiers'] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
        y['subject_identifiers'] = subject_identifiers.map{|i| i.reference} unless subject_identifiers.empty?
        unless y['item_identifiers'] || y['subject_identifiers']
          y['item_identifiers'] = [to_yaml_ref]
        end
        y['subject_locators'] = subject_locators.map{|i| i.reference} unless subject_locators.empty?
        y['names'] = names.map{|i| i.to_yaml_hash} unless names.empty?
        y['occurrences'] = occurrences.map{|i| i.to_yaml_hash} unless occurrences.empty?
        y
      end
      def to_yaml(*a)
        to_yaml_hash.to_yaml(*a)
      end
      def to_yaml_ref
        if subject_identifiers.first
          return subject_identifiers.first.reference
        end
        if item_identifiers.first
          return item_identifiers.first.reference
        end
        "t#{id}"
      end
    end

    module Association
      def to_yaml_hash(*a)
        y={}
        y['reifier'] = reifier.to_yaml_ref if reifier
        y['item_identifiers'] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
        y['type'] = type.to_yaml_ref if type
        y['scope'] = scope.map{|i| i.to_yaml_hash} unless scope.empty?
        y['roles'] = roles.map{|i| i.to_yaml_hash} unless roles.empty?
        y
      end
      # returns the YAML representation of this association
      def to_yaml(*a)
        to_yaml_hash.to_yaml(*a)
      end
    end

    module TopicName
      def to_yaml_hash(*a)
        y={}
        y['reifier'] = reifier.to_yaml_ref if reifier
        y['item_identifiers'] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
        y['value'] = value if value
        y['type'] = type.to_yaml_ref if type
        y['scope'] = scope.map{|i| i.to_yaml_hash} unless scope.empty?
        y['variants'] = variants.map{|i| i.to_yaml_hash} unless variants.empty?
        y
      end
      def to_yaml(*a)
        to_yaml_hash.to_yaml(*a)
      end
    end

    module Occurrence
      def to_yaml_hash(*a)
        y={}
        y['reifier'] = reifier.to_yaml_ref if reifier
        y['item_identifiers'] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
        y['value'] = value if value
        y['datatype'] = datatype if datatype
        y['type'] = type.to_yaml_ref if type
        y['scope'] = scope.map{|i| i.to_yaml_hash} unless scope.empty?
        y
      end
      def to_yaml(*a)
        to_yaml_hash.to_yaml(*a)
      end
    end

    module AssociationRole
      def to_yaml_hash(*a)
        y={}
        y['reifier'] = reifier.to_yaml_ref if reifier
        y['item_identifiers'] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
        y['player'] = player.to_yaml_ref if player
        y['type'] = type.to_yaml_ref if type
        y
      end
      def to_yaml(*a)
        to_yaml_hash.to_yaml(*a)
      end
    end

    module Variant
      def to_yaml_hash(*a)
        y={}
        y['reifier'] = reifier.to_yaml_ref if reifier
        y['item_identifiers'] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
        y['value'] = value if value
        y['scope'] = scope.map{|i| i.to_yaml_hash} unless scope.empty?
        y
      end
      def to_yaml(*a)
        to_yaml_hash.to_yaml(*a)
      end
    end
  
    RTM.register_extension( self )
  end
end