Changeset 129

Show
Ignore:
Timestamp:
09/28/06 22:22:49 (2 years ago)
Author:
ahorn
Message:

corex branch: clean up Setup::Template

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/corex/radiant/db/migrate/setup/base.rb

    r128 r129  
    1919   
    2020      def gets 
    21         readline.strip.downcase 
     21        readline.strip 
    2222      end 
    2323     
  • branches/corex/radiant/db/migrate/setup/template.rb

    r128 r129  
    33  def run 
    44    select_template 
    5     feedback "Importing template data" do 
    6       create_records_from_template 
    7     end 
     5    create_records_from_template 
    86  end 
    97 
     
    119    
    1210    def select_template 
    13       templates_directory = File.join(RADIANT_ROOT, 'db', 'templates') 
    14       templates = Dir[File.join(templates_directory, '*.yml')] 
    15       templates.map! { |t| load_template(t) } 
    16       templates = templates.sort_by { |t| t['name'] } 
     11      templates = load_templates 
    1712      loop do 
    18         templates.each_with_index do |t, i| 
    19           puts " #{i + 1}) #{t['name']}" 
    20           t['description'].each_line { |line| puts "    #{line.strip}" } 
    21         end 
     13        print_selection(templates) 
    2214        ask "Select a database template [1-#{templates.size}]:" 
    23         selection = $1.to_i if gets.strip =~ /^(\d+)$/ 
     15        selection = $1.to_i if gets =~ /^(\d+)$/ 
    2416        case selection 
    2517        when (1..templates.size) 
     
    3123      end 
    3224    end 
     25 
     26    def load_templates 
     27      templates_directory = File.join(RADIANT_ROOT, 'db', 'templates') 
     28      templates = Dir[File.join(templates_directory, '*.yml')] 
     29      templates.map! { |template| load_template(template) } 
     30      templates.sort_by { |template| template['name'] } 
     31    end 
    3332     
    3433    def load_template(template) 
    3534      YAML.load_file(template) 
     35    end 
     36 
     37    def print_selection(templates) 
     38      templates.each_with_index do |template, index| 
     39        puts " #{index + 1}) #{template['name']}" 
     40        template['description'].each_line { |line| puts "    #{line.strip}" } 
     41      end 
    3642    end 
    3743     
     
    4147        records.keys.each do |key| 
    4248          feedback "Creating #{key.to_s.underscore.humanize}" do 
    43             model = Object.const_get(key.to_s.singularize
    44             record_pairs = records[key].map { |name, record| [record['id'], record] }.sort { |a, b| a[0] <=> b[0] } 
     49            model = model(key
     50            record_pairs = order_by_id(records[key]) 
    4551            record_pairs.each do |id, record| 
    46               puts record.to_s 
    4752              model.new(record).save 
    4853            end 
     
    5156      end 
    5257    end 
     58 
     59    def model(model_name) 
     60      Object.const_get(model_name.to_s.singularize) 
     61    end 
     62 
     63    def order_by_id(records) 
     64      records.map { |name, record| [record['id'], record] }.sort { |a, b| a[0] <=> b[0] } 
     65    end 
    5366     
    5467end