Class: SchemaRD::RelationGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/schemard/relation_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ RelationGenerator

Returns a new instance of RelationGenerator.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/schemard/relation_generator.rb', line 7

def initialize(argv)
  rails_root = Pathname.pwd
  opt = OptionParser.new
  opt.on('-d VAL', 'Rails.root.directory') {|v| rails_root = Pathname.new(v).expand_path }
  opt.parse(argv)

  require_path = rails_root + "config/environment.rb"
  unless require_path.exist?
    puts "<#{rails_root}> is not Rails.root Directory, Abort!"
    puts "Usage: schemard -d <Rails.root.dir>"
  else
    Dir.chdir(rails_root) do
      require require_path.to_s
    end
  end
end

Instance Method Details

#ready?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/schemard/relation_generator.rb', line 23

def ready?
  defined?(Rails)
end

#runObject



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
# File 'lib/schemard/relation_generator.rb', line 26

def run
  Dir.glob(Rails.root + "app/models/**/*")
  .reject{|path| Dir.exist?(path) }.each{|filepath| require filepath }

  hash = ObjectSpace.each_object(Class)
  .select{|o| o.ancestors.include?(ActiveRecord::Base) && o != ActiveRecord::Base }
  .select{|o| o.table_name }
  .each_with_object({}) do |model, hash|
    hash[model.table_name] = {}

    relation_selector = ->(klass){ model._reflections.values.select{|r| r.is_a?(klass) } }
    has_one_rels = relation_selector.call(ActiveRecord::Reflection::HasOneReflection)
    has_many_rels = relation_selector.call(ActiveRecord::Reflection::HasManyReflection)
    belongs_to_rels = relation_selector.call(ActiveRecord::Reflection::BelongsToReflection)

    if has_one_rels.present?
      hash[model.table_name]["has_one"] = has_one_rels.map{|r| r.klass.table_name }
    end
    if has_many_rels.present?
      hash[model.table_name]["has_many"] = has_many_rels.map{|r| r.klass.table_name }
    end
    if belongs_to_rels.present?
      hash[model.table_name]["belongs_to"] = belongs_to_rels.map{|r| r.klass.table_name }
    end
  end
  puts YAML.dump({ "tables" => hash })
end