lib/activefacts/generate/ruby.rb in activefacts-0.7.2 vs lib/activefacts/generate/ruby.rb in activefacts-0.7.3

- old
+ new

@@ -35,38 +35,49 @@ if @sql require 'activefacts/persistence' puts "require 'activefacts/persistence'\n" @tables = vocabulary.tables end - puts "\nmodule #{vocabulary.name}\n\n" + puts "\nmodule ::#{vocabulary.name}\n\n" end def vocabulary_end puts "end" end def value_type_dump(o) - return if !o.supertype - if o.name == o.supertype.name - # In ActiveFacts, parameterising a ValueType will create a new datatype - # throw Can't handle parameterized value type of same name as its datatype" if ... + is_special_supertype = !o.supertype && %w{Date Time DateAndTime}.include?(o.name) + + # We map DateAndTime to DateTime; if such a ValueType exists, don't dump this one + return if is_special_supertype && o.name == 'DateAndTime' && o.constellation.ValueType[[[o.vocabulary.name], 'DateTime']] + + return if !o.supertype && !is_special_supertype + if o.supertype && o.name == o.supertype.name + # In ActiveFacts, parameterising a ValueType will create a new datatype + # throw Can't handle parameterized value type of same name as its datatype" if ... end length = (l = o.length) && l > 0 ? ":length => #{l}" : nil scale = (s = o.scale) && s > 0 ? ":scale => #{s}" : nil params = [length,scale].compact * ", " + name = o.name ruby_type_name = - case o.supertype.name + case o.supertype ? o.supertype.name : o.name when "VariableLengthText"; "String" when "Date"; "::Date" + when "DateAndTime"; "::DateTime" + when "Time"; "::Time" else o.supertype.name end - puts " class #{o.name} < #{ruby_type_name}\n" + + name = name.sub(/^[a-z]/) {|i| i.upcase} + puts " class #{name} < #{ruby_type_name}\n" + " value_type #{params}\n" - puts " table" if @sql and o.is_table + if @sql and o.is_table + puts " table" + end puts " \# REVISIT: #{o.name} has restricted values\n" if o.value_restriction puts " \# REVISIT: #{o.name} is in units of #{o.unit.name}\n" if o.unit roles_dump(o) puts " end\n\n" end @@ -76,21 +87,27 @@ secondary_supertypes = o.supertypes-[primary_supertype] puts " class #{o.name} < #{ primary_supertype.name }" puts " identified_by #{identified_by(o, pi)}" if pi puts " supertypes "+secondary_supertypes.map(&:name)*", " if secondary_supertypes.size > 0 - puts " table" if @sql and o.is_table + if @sql and o.is_table + puts " table" + end fact_roles_dump(o.fact_type) if o.fact_type roles_dump(o) puts " end\n\n" @constraints_used[pi] = true if pi end def non_subtype_dump(o, pi) puts " class #{o.name}" + + # We want to name the absorption role only when it's absorbed along its single identifying role. puts " identified_by #{identified_by(o, pi)}" - puts " table" if @sql and o.is_table + if @sql and o.is_table + puts " table" + end fact_roles_dump(o.fact_type) if o.fact_type roles_dump(o) puts " end\n\n" @constraints_used[pi] = true end @@ -120,27 +137,25 @@ @fact_types_dumped[fact_type] = true end def identified_by_roles_and_facts(entity_type, identifying_roles, identifying_facts, preferred_readings) identifying_roles.map{|role| - ":"+preferred_role_name(role) + ":"+preferred_role_name(role, entity_type) }*", " end def unary_dump(role, role_name) puts " maybe :"+role_name end def binary_dump(role, role_name, role_player, one_to_one = nil, readings = nil, other_role_name = nil, other_method_name = nil) # Find whether we need the name of the other role player, and whether it's defined yet: - if role_name.camelcase(true) == role_player.name + if role_name.camelcase(true) == role_player.name.sub(/^[a-z]/) {|i| i.upcase} # Don't use Class name if implied by rolename role_reference = nil - elsif !@concept_types_dumped[role_player] - role_reference = '"'+role_player.name+'"' else - role_reference = role_player.name + role_reference = concept_reference(role_player) end other_role_name = ":"+other_role_name if other_role_name line = " #{one_to_one ? "one_to_one" : "has_one" } " + [ ":"+role_name, @@ -150,9 +165,17 @@ ].compact*", "+" " line += " "*(48-line.length) if line.length < 48 line += "\# See #{role_player.name}.#{other_method_name}" if other_method_name puts line puts " \# REVISIT: #{other_role_name} has restricted values\n" if role.role_value_restriction + end + + def concept_reference concept + if !@concept_types_dumped[concept] + '"'+concept.name+'"' + else + role_reference = concept.name + end end end end end