Class: MetaModel::Command::Build::Translator
- Inherits:
-
Object
- Object
- MetaModel::Command::Build::Translator
- Defined in:
- lib/metamodel/command/build/translator.rb
Instance Attribute Summary collapse
-
#associations ⇒ Object
readonly
Returns the value of attribute associations.
-
#models ⇒ Object
readonly
Returns the value of attribute models.
Instance Method Summary collapse
-
#initialize(models, associations) ⇒ Translator
constructor
A new instance of Translator.
- #translate ⇒ Object
Constructor Details
#initialize(models, associations) ⇒ Translator
Returns a new instance of Translator.
12 13 14 15 |
# File 'lib/metamodel/command/build/translator.rb', line 12 def initialize(models, associations) @models = models @associations = associations end |
Instance Attribute Details
#associations ⇒ Object (readonly)
Returns the value of attribute associations.
10 11 12 |
# File 'lib/metamodel/command/build/translator.rb', line 10 def associations @associations end |
#models ⇒ Object (readonly)
Returns the value of attribute models.
9 10 11 |
# File 'lib/metamodel/command/build/translator.rb', line 9 def models @models end |
Instance Method Details
#translate ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/metamodel/command/build/translator.rb', line 17 def translate name_model_hash = Hash[@models.collect { |model| [model.name, model] }] @associations.map! do |association| major_model = name_model_hash[association.major_model] major_model.associations << association association.major_model = major_model association.secondary_model = name_model_hash[association.secondary_model] size = association.through ? 3 : 2 association.through = name_model_hash[association.through] raise Informative, "Associations not satisfied in `Metafile`" \ unless [association.major_model, association.secondary_model, association.through].compact.size == size association end satisfy_constraint = @associations.reduce([]) do |remain, association| expect = remain.select { |assoc| assoc.expect_constraint? association } if expect.empty? remain << association else remain.delete expect.first end remain end raise Informative, "Unsatisfied constraints in #{satisfy_constraint.map \ { |x| x.debug_description }}" \ if satisfy_constraint.size > 0 @associations.each do |association| major_model = association.major_model secondary_model = association.secondary_model case association.relation when :has_one, :has_many then name = association.through ? association.through.foreign_id : major_model.foreign_id property = Record::Property.new(name, :int, :foreign, :default => 0) secondary_model.properties << property when :belongs_to then name = association.through ? association.through.foreign_id : secondary_model.foreign_id property = Record::Property.new(name, :int, :foreign, :default => 0) major_model.properties << property end end @models.each do |model| model.properties.uniq! { |prop| [prop.name] } end end |