lib/activefacts/generate/oo.rb in activefacts-0.7.0 vs lib/activefacts/generate/oo.rb in activefacts-0.7.1
- old
+ new
@@ -1,16 +1,18 @@
#
-# OO Generation support for the ActiveFacts API from an ActiveFacts vocabulary.
-# Copyright (c) 2008 Clifford Heath. Read the LICENSE file.
+# ActiveFacts Generators.
+# Base class for generators of class libraries in any object-oriented language that supports the ActiveFacts API.
#
+# Copyright (c) 2009 Clifford Heath. Read the LICENSE file.
+#
require 'activefacts/vocabulary'
require 'activefacts/generate/ordered'
module ActiveFacts
-
module Generate
- class OO < OrderedDumper
+ # Base class for generators of object-oriented class libraries for an ActiveFacts vocabulary.
+ class OO < OrderedDumper #:nodoc:
include Metamodel
def constraints_dump(constraints_used)
# Stub, not needed.
end
@@ -21,17 +23,16 @@
def value_type_end
end
def roles_dump(o)
o.all_role.
+ select{|role|
+ role.fact_type.all_role.size <= 2
+ }.
sort_by{|role|
- other_role = role.fact_type.all_role[role.fact_type.all_role[0] != role ? 0 : -1]
- other_role ? preferred_role_name(other_role) : ""
- #puts "\t#{role.fact_type.describe(other_role)} by #{p}"
+ preferred_role_name(role.fact_type.all_role.select{|r2| r2 != role}[0] || role)
}.each{|role|
- fact_type = role.fact_type
- next if fact_type.all_role.size > 2
role_dump(role)
}
end
def role_dump(role)
@@ -47,12 +48,11 @@
if TypeInheritance === fact_type
# debug "Ignoring role #{role} in #{fact_type}, subtype fact type"
return
end
- other_role_number = fact_type.all_role[0] == role ? 1 : 0
- other_role = fact_type.all_role[other_role_number]
+ other_role = fact_type.all_role.select{|r| r != role}[0]
other_role_name = preferred_role_name(other_role)
other_player = other_role.concept
# Find any uniqueness constraint over this role:
fact_constraints = @presence_constraints_by_fact[fact_type]
@@ -78,13 +78,17 @@
# It's a one_to_one if there's a uniqueness constraint on the other role:
one_to_one = ucs.find {|c| c.role_sequence.all_role_ref.map(&:role) == [other_role] }
# Find role name:
role_method = preferred_role_name(role)
- by = other_role_name != other_player.name.snakecase ? "_by_#{other_role_name}" : ""
+ as = other_role_name != other_player.name.snakecase ? "_as_#{other_role_name}" : ""
other_role_method = one_to_one ? role_method : "all_"+role_method
- other_role_method += by
+ # puts "---"+role.role_name if role.role_name
+ if other_role_name != other_player.name.snakecase and
+ role_method == role.concept.name.snakecase
+ other_role_method += "_as_#{other_role_name}"
+ end
role_name = role_method
role_name = nil if role_name == role.concept.name.snakecase
binary_dump(role, other_role_name, other_player, one_to_one, nil, role_name, other_role_method)
@@ -132,12 +136,12 @@
def fact_roles_dump(fact)
fact.all_role.sort_by{|role|
preferred_role_name(role)
}.each{|role|
role_name = preferred_role_name(role)
- by = role_name != role.concept.name.snakecase ? "_by_#{role_name}" : ""
+ as = role_name != role.concept.name.snakecase ? "_as_#{role_name}" : ""
raise "Fact #{fact.describe} type is not objectified" unless fact.entity_type
- other_role_method = "all_"+fact.entity_type.name.snakecase+by
+ other_role_method = "all_"+fact.entity_type.name.snakecase+as
binary_dump(role, role_name, role.concept, false, nil, nil, other_role_method)
}
end
def entity_type_banner