Index: /ruby/branches/0.5/lib/open_ehr/rm/demographic.rb
===================================================================
--- /ruby/branches/0.5/lib/open_ehr/rm/demographic.rb	(revision 360)
+++ /ruby/branches/0.5/lib/open_ehr/rm/demographic.rb	(revision 361)
@@ -2,8 +2,10 @@
 # http://www.openehr.org/uml/release-1.0.1/Browsable/_9_5_76d0249_1118674798473_6021_0Report.html
 # Ticket refs #45
+include OpenEHR::RM::Common::Archetyped
+
 module OpenEHR
   module RM
-    module Demogrphic
-      class Address < OpenEHR::RM::Common::Archetyped::Locatable
+    module Demographic
+      class Address < Locatable
         attr_reader :details
 
@@ -17,72 +19,82 @@
       end
 
-      class Capability < OpenEHR::RM::Common::Archetyped::Locatable
+      class Capability < Locatable
         attr_reader :credentials, :time_validity
 
       end
 
-      class Party < OpenEHR::RM::Common::Archetyped::Locatable
-        attr_reader :details, :reverse_relationships, :uid
-        attr_reader :idetities, :contacts, :relationships
-        def initialize(uid, archetype_node_id, name, archetype_details,
-                       feeder_audit, links, identities, contacts,
-                       relationships, reverse_relationships, details)
-          super(uid, archetype_node_id, name, archetype_details,
-                feeder_audit, links, nil)
-          uid_valid(uid)
+      class Party < Locatable
+        attr_reader :uid, :identities, :contacts, :relationships,
+                    :reverse_relationships
+        attr_accessor :details
+        alias :type :name
+
+        def initialize(args = { })
+          super(args)
+          self.uid = args[:uid]
+          self.identities = args[:identities]
+          self.contacts = args[:contacts]
+          self.relationships = args[:relationships]
+          self.reverse_relationships =
+            args[:reverse_relationships]
+          self.details = args[:details]
+        end
+
+        def uid=(uid)
+          raise ArgumentError, 'uid is mandatory' if uid.nil?
           @uid = uid
-          identities_vaild(identities)
+        end
+
+        def identities=(identities)
+          if identities.nil? || identities.empty?
+            raise ArgumentError, 'identities are mandatory'
+          end
           @identities = identities
-          contacts_valid(contacts)
+        end
+
+        def contacts=(contacts)
+          if !contacts.nil? && contacts.empty?
+            raise ArgumentError, 'contacts should not be empty'
+          end
           @contacts = contacts
-          relationships_valid(relationships)
-        end
-        def type
-          return name
-        end
-        def uid=(uid)
-          uid_valid(uid)
-          @uid = uid
-        end
-        def identities=(identities)
-          identities_vaild(identities)
-          @identities = identities
-        def contacts=(contacts)
-          contacts_valid(contacts)
-          @contacts = contacts
-        end
+        end
+
         def parent=(parent)
           @parent = nil
         end
-        private
-        def uid_valid(uid)
-          raise ArgumentError, "uid is not valid" if uid.nil?
-        end
-        def identities_vaild(identities)
-          if identities.nil?
-            raise ArgumentError, "identities must not be nil"
-          elsif identities.empty?
-            raise ArgumentError, "identities must not be empty"
-          end
-        end
-        def contacts_valid(contacts)
-          if contacs.nil?
-            raise ArgumentError, "contacts must not be nil"
-          elsif contacts.empty?
-            raise ArgumentError, "contacts must not be empty"
-          end          
-        end
-      end
-
-      class PartyIdentity < OpenEHR::RM::Common::Archetyped::Locatable
+
+        def relationships=(relationships)
+          unless relationships.nil?
+            if relationships.empty?
+              raise ArgumentError, 'relationships should not be empty?'
+            else
+              relationships.each do |rel|
+                if rel.source.id.value != @uid.value
+                  raise ArgumentError, 'invalid source of relationships'
+                end
+              end
+            end
+          end
+          @relationships = relationships
+        end
+
+        def reverse_relationships=(reverse_relationships)
+          if !reverse_relationships.nil? && reverse_relationships.empty?
+            raise ArgumentError, 'reverse_relationships should not be empty'
+          end
+          @reverse_relationships = reverse_relationships
+        end
+      end
+
+      class PartyIdentity < Locatable
         attr_reader :details
 
       end
 
-      class PartyRelationship < OpenEHR::RM::Common::Archetyped::Locatable
-
-      end
-
-      class Versioned_Party < OpenEHR::RM::Common::Archetyped::Locatable
+      class PartyRelationship < Locatable
+
+      end
+
+      class VersionedParty < Locatable
 
       end
@@ -95,4 +107,5 @@
         LEAGAL_IDENTITY = 'leagal identity'
         attr_reader :languages, :rules
+
         def initialize(uid, archetype_node_id, name, archetype_details,
                        feeder_audit, links, identities, contacts,
@@ -106,4 +119,5 @@
           set_rules(rules)
         end
+
         def has_legal_identity?
           @identities.each {|identity|
@@ -114,7 +128,9 @@
           false
         end
+
         def languages=(languages)
           set_languages(languages)
         end
+
         def values=(values)
           set_values(values)
@@ -129,4 +145,5 @@
           @languages = languages
         end
+
         def set_values(values)
           if values.nil?
@@ -136,6 +153,8 @@
           end
           @values = values
-      end
-      class Contact < OpenEHR::RM::Common::Archetyped::Locatable
+        end
+      end
+
+      class Contact < Locatable
         attr_accessor :time_validity
         attr_reader :addresses
@@ -148,10 +167,13 @@
           @time_validity = time_validity
         end
+
         def purpose
           @name
         end
+
         def purpose=(purpose)
           @name = purpose
         end
+
         def addresses=(addresses)
           address_exists?(addresses)
@@ -159,4 +181,5 @@
         end
         private
+
         def address_exists?(addresses)
           if addresses.nil?
