Sha256: 9110194610ee537dd9e06d913e5882e19d86c6d15fab1700b49a87d30132e80d

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

module Ar2gostruct
  class Gostruct
    def initialize(model_dir)
      @model_dir = model_dir
      get_models
      puts "package #{ENV["package"]}\n\n" unless ENV["package"].blank?
    end
    attr_accessor :model_dir, :models

    def convert!
      self.models.each do |m|
        class_name = m.sub(/\.rb$/,'').camelize
        begin
          klass = class_name.split('::').inject(Object){ |klass,part| klass.const_get(part) }
          if klass < ActiveRecord::Base && !klass.abstract_class?
            convert_to_gostruct!(klass)
          end
        rescue Exception => e
          puts "// Unable to convert #{class_name}: #{e.message}"
        end
      end
    end

    private

      def get_models
        Dir.chdir(self.model_dir) do
          self.models = Dir["**/*.rb"]
        end
      end

      def convert_to_gostruct!(klass)
        converter = Converter.new klass, :plural => ENV["plural"], :orm => ENV["orm"], :association => ENV["association"]
        info = converter.convert!
        model_file_name = File.join(self.model_dir, klass.name.underscore + ".rb")
        puts "// #{model_file_name}"
        puts info
      end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ar2gostruct-0.2.2 lib/ar2gostruct/gostruct.rb