Class: ActiveFacts::Generate::DM
- Inherits:
-
Helpers::OO
- Object
- Helpers::OrderedDumper
- Helpers::OO
- ActiveFacts::Generate::DM
- Includes:
- Persistence
- Defined in:
- lib/activefacts/generate/dm.rb
Overview
:nodoc:
Instance Method Summary collapse
- #class_name(name) ⇒ Object
- #column_name(column) ⇒ Object
-
#generate(out = $>) ⇒ Object
:nodoc:.
-
#initialize(vocabulary, *options) ⇒ DM
constructor
A new instance of DM.
- #key_fields(ref, reverse = false) ⇒ Object
- #model_file(name) ⇒ Object
- #new_output(name) ⇒ Object
-
#normalise_type(type, length) ⇒ Object
Return DataMapper type and (modified?) length for the passed base type.
- #puts(s) ⇒ Object
- #symbol_name(name) ⇒ Object
Methods included from Persistence
rails_plural_name, rails_singular_name, rails_type
Methods inherited from Helpers::OO
#append_ring_to_reading, #constraint_banner, #constraint_dump, #constraint_end, #constraints_dump, #entity_type_banner, #entity_type_dump, #entity_type_group_end, #fact_roles_dump, #fact_type_banner, #fact_type_end, #preferred_role_name, #role_dump, #roles_dump, #skip_fact_type, #value_type_banner, #value_type_end
Methods inherited from Helpers::OrderedDumper
#append_ring_to_reading, #build_entity_dependencies, #build_indices, #constraint_banner, #constraint_dump, #constraint_end, #constraint_sort_key, #constraints_dump, #data_type_dump, #derivation_precursors_complete, #describe_fact_type, #describe_role_sequence, #describe_roles, #entity_type_banner, #entity_type_group_end, #entity_types_dump, #fact_instances_dump, #fact_type_banner, #fact_type_dump, #fact_type_dump_with_dependents, #fact_type_end, #fact_type_key, #fact_types_dump, #identified_by, #non_subtype_dump, #print, #released_fact_types_dump, #role_ref_key, #set_option, #skip_fact_type, #subtype_dump, #unit_dump, #units_banner, #units_dump, #units_end, #value_type_banner, #value_type_chain_dump, #value_type_dump, #value_type_end, #value_type_fork, #value_types_dump, #vocabulary_end, #vocabulary_start
Constructor Details
#initialize(vocabulary, *options) ⇒ DM
Returns a new instance of DM.
28 29 30 31 32 33 |
# File 'lib/activefacts/generate/dm.rb', line 28 def initialize(vocabulary, *) @vocabulary = vocabulary @vocabulary = @vocabulary.Vocabulary.values[0] if ActiveFacts::API::Constellation === @vocabulary @mixins = .grep(/^dir=/)[-1] @mixins && @mixins.sub!(/^dir=/,'') end |
Instance Method Details
#class_name(name) ⇒ Object
43 44 45 |
# File 'lib/activefacts/generate/dm.rb', line 43 def class_name(name) name.gsub(/\s/,'') end |
#column_name(column) ⇒ Object
47 48 49 |
# File 'lib/activefacts/generate/dm.rb', line 47 def column_name(column) column.name('_').snakecase end |
#generate(out = $>) ⇒ Object
:nodoc:
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/activefacts/generate/dm.rb', line 81 def generate(out = $>) #:nodoc: @out = out # Calculate the relational absorption: tables = @vocabulary.tables # Figure out which ObjectType will be models (tables and their subtypes) models = @vocabulary.all_object_type.sort_by{|o| o.name}.select do |o| next false if o.name =~ /_?ImplicitBooleanValueType/ o.is_table || (o.absorbed_via && o.absorbed_via.role_type == :supertype) end is_model = models.inject({}) { |h, m| h[m] = true; h } puts "require 'dm-core'" puts "require 'dm-constraints'" puts "\n" # Dump tables until all done, subtypes before supertypes: until models.empty? # Choose another object type that we can dump now: o = models.detect do |o| next true if o.is_table next true if a = o.absorbed_via and a.role_type == :supertype and supertype = a.from and !models.include?(supertype) false end models.delete(o) supertype = (a = o.absorbed_via and a.role_type == :supertype) ? supertype = a.from : nil if o.is_a?(ActiveFacts::Metamodel::EntityType) if secondary_supertypes = o.supertypes-[supertype] and secondary_supertypes.size > 0 and secondary_supertypes.detect do |sst| sst_ref_facts = sst.preferred_identifier.role_sequence.all_role_ref.map{|rr| rr.role.fact_type}.uniq = sst.references_from.reject do |ref| sst_ref_facts.include?(ref.fact_type) end .size > 0 end raise "Cannot map classes like #{o.name} with roles inherited from external supertypes (#{secondary_supertypes.map{|t|t.name}*", "})" end pi = o.preferred_identifier = pi.role_sequence.all_role_ref.sort_by{|role_ref| role_ref.ordinal} = ([o.fact_type]+.map{|rr| rr.role.fact_type }).compact.uniq else = [] end # REVISIT: STI fails where the base class is absorbed into another table, like Incident in Insurance for example. # In this case you get the subtype fields absorbed and should not get an STI model. puts "class #{class_name(o.name)}#{supertype ? " < #{class_name(supertype.name)}" : ''}" puts " include DataMapper::Resource\n\n" unless supertype columns = o.columns o.references_from.sort_by{|r| r.to_s}.each do |ref| # A (set of) columns if !columns # absorbed subtypes didn't have columns populated columns = o.all_columns({}) end next if [:subtype, :supertype].include?(ref.role_type) # debugger if ref_columns.detect{|c| [:subtype, :supertype].include?(c.references[0].role_type)} ref_columns = columns.select{|c| c.references[0] == ref } # puts " \# #{ref.reading}:" ref_columns.sort_by{|column| column_name(column)}.each do |column| type, params, constraints = column.type length = params[:length] length &&= length.to_i scale = params[:scale] scale &&= scale.to_i type, length = normalise_type(type, length) key = .include?(column.references[0].fact_type) || (.empty? && ref.is_self_value) cname = column_name(column) required = column.is_mandatory && !key ? ", :required => true" : "" # Key fields are implicitly required if type == 'Serial' if !key || o.preferred_identifier.role_sequence.all_role_ref.size != 1 type = 'Integer' else key = false # This is implicit end end $stderr.puts "Warning: non-mandatory key field #{o.name}.#{column.name} is forced to mandatory" if !column.is_mandatory && key puts " property :#{column_name(column)}, #{type}#{length ? ", :length => "+length.to_s : ''}#{required}#{key ? ', :key => true' : ''}\t\# #{column.comment}" end if is_model[ref.to] # An association reverse = false association_type = case ref.role_type when :one_one reverse = true "has 1," when :one_many, :many_one "belongs_to" when :supertype next when :subtype next else raise "role type #{ref.role_type} not handled" end association_name = (ref.to_names*'_') model_name = association_name != ref.to.name ? model_name = ", '#{class_name(ref.to.name)}'" : '' comment = o.fact_type ? "#{association_name} is involved in #{o.name}" : ref.reading keys = key_fields(ref, reverse) puts " #{association_type} :#{association_name.downcase}#{model_name}#{keys}\t\# #{comment}" end end # Emit the "has n," associations # REVISIT: Need to use ActiveSupport to pluralise these names, or disable inflexion somehow. o.references_to.sort_by{|r| [r.to_s, (r.from_role||r.to_role).ordinal] }.each do |ref| next unless is_model[ref.from] constraint = '' association_type = case ref.role_type when :one_one "has 1," when :many_one, :one_many constraint = ', :constraint => :destroy' # REVISIT: Check mandatory, and use nullify? "has n," else next end prr = ref.fact_type.preferred_reading.role_sequence.all_role_ref.detect{|rr| rr.role == ref.to_role} association_name = (ref.from_names*'_') if prr && (prr.role.role_name || prr.leading_adjective || prr.trailing_adjective) association_name += "_as_"+symbol_name(ref.to_names*'_') end model_name = association_name != ref.from.name ? model_name = ", '#{class_name(ref.from.name)}'" : '' comment = o.is_a?(ActiveFacts::Metamodel::EntityType) && o.fact_type ? "#{association_name} is involved in #{o.name}" : ref.reading keys = key_fields(ref) puts " #{association_type} :#{association_name.downcase}#{model_name}#{keys}\t\# #{comment}" end puts "end\n\n" end end |
#key_fields(ref, reverse = false) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/activefacts/generate/dm.rb', line 62 def key_fields(ref, reverse = false) # Compute and return child_key and parent_key if necessary fk = ref.from.foreign_keys.detect{|k| k.jump_reference == ref} unless fk debugger raise "foreign key not found for #{ref}" end child_key = fk.from_columns.map{|c| column_name(c)} parent_key = fk.to_columns.map{|c| column_name(c)} if child_key != parent_key c, p = *(reverse ? ['parent', 'child'] : ['child', 'parent']) ", :#{c}_key => [:#{child_key*', :'}], :#{p}_key => [:#{parent_key*', :'}]" else '' end end |
#model_file(name) ⇒ Object
39 40 41 |
# File 'lib/activefacts/generate/dm.rb', line 39 def model_file(name) @mixins+'/'+name.gsub(/\s/,'')+'.rb' end |
#new_output(name) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/activefacts/generate/dm.rb', line 55 def new_output(name) return unless @mixins @out.flush @out = File.open(model_file(name), "w") puts "require 'datamapper'\n\n" end |
#normalise_type(type, length) ⇒ Object
Return DataMapper type and (modified?) length for the passed base type
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/activefacts/generate/dm.rb', line 228 def normalise_type(type, length) dm_type = case type when /^Auto ?Counter$/ 'Serial' when /^Unsigned ?Integer$/, /^Signed ?Integer$/, /^Unsigned ?Small ?Integer$/, /^Signed ?Small ?Integer$/, /^Unsigned ?Tiny ?Integer$/ length = nil 'Integer' when /^Decimal$/ 'Decimal' when /^Fixed ?Length ?Text$/, /^Char$/ 'String' when /^Variable ?Length ?Text$/, /^String$/ 'String' when /^Large ?Length ?Text$/, /^Text$/ 'Text' when /^Date ?And ?Time$/, /^Date ?Time$/ 'DateTime' when /^Date$/ 'DateTime' when /^Time$/ 'DateTime' when /^Auto ?Time ?Stamp$/ 'DateTime' when /^Money$/ 'Decimal' when /^Picture ?Raw ?Data$/, /^Image$/ 'String' when /^Variable ?Length ?Raw ?Data$/, /^Blob$/ 'String' when /^BIT$/ 'Boolean' else # raise "DataMapper type unknown for standard type #{type}" type end [dm_type, length] end |
#puts(s) ⇒ Object
35 36 37 |
# File 'lib/activefacts/generate/dm.rb', line 35 def puts s @out.puts s end |
#symbol_name(name) ⇒ Object
51 52 53 |
# File 'lib/activefacts/generate/dm.rb', line 51 def symbol_name(name) name.gsub(/\s/,'_').snakecase end |