class Dread::AssociationGraph

Attributes

clazz[R]
collected_clazzes[R]
dependable_collection[R]

Public Class Methods

new(clazz_data, pluralized=false, collected_clazzes=[]) click to toggle source
# File lib/dread/association_graph.rb, line 8
def initialize(clazz_data, pluralized=false, collected_clazzes=[])
  set_and_verify_clazz_and_relation(clazz_data)
  @pluralized = pluralized
  @collected_clazzes = collected_clazzes
end

Public Instance Methods

draw(output='console_output') click to toggle source
# File lib/dread/association_graph.rb, line 23
def draw(output='console_output')
  case output
  when 'console_output'
    ConsoleOutput.generate(dependable_collection)
  end
end

Private Instance Methods

collect_dependables() click to toggle source
# File lib/dread/association_graph.rb, line 55
def collect_dependables
  Hash.new.tap do |relation_hash|
    @clazz.reflections.each do |assoc_name, assoc_data|
      case assoc_data.options[:dependent]
      when :delete
        relation_hash[assoc_name] = {}
      when :destroy
        relation_hash.merge!(
          AssociationGraph.new(assoc_data, assoc_data.macro == :has_many, collected_clazzes + [self.clazz]).dependable_collection)
      end
    end
  end
end
set_and_verify_clazz_and_relation(clazz_data) click to toggle source
# File lib/dread/association_graph.rb, line 32
def set_and_verify_clazz_and_relation(clazz_data)
  begin
    set_clazz_and_relation(clazz_data)
  rescue NameError => e
    raise Error.new("Unable to find class called #{clazz_data.classify}")
  end
end
set_clazz_and_relation(clazz_data) click to toggle source
# File lib/dread/association_graph.rb, line 40
def set_clazz_and_relation(clazz_data)
  case clazz_data
  when ActiveRecord::Reflection::AssociationReflection
    @clazz = (clazz_data.klass.to_s || clazz_data.table_name).constantize
    @relation = clazz_data.name
  when String
    @clazz = (clazz_data.classify).constantize
    @relation = clazz_data.underscore
  when NilClass
    raise Error.new('Please pass a env var called class to proceed. E.g: rake dread class=user')
  else
    raise Error.new("Unable to proceed with #{clazz_data.class}")
  end
end