class SequelSchemaDotGenerator::Generator
Public Class Methods
new(params)
click to toggle source
@todo Consider write the params as block
@param [Hash] params Setup of Generator
instance
:db [Sequel::Database] required From this one will be generated the diagram :logger [Logger] optional Will be used for logging :dot_template_path [String] optional Template is supposed to be ERB template. Will be used as template for resulting Dot file :colored_associations [Boolean] optional Whether lines representing associations will be draw in color (=False) :schema_source_type [Symbol] optional How will be data acquired (:column|:model)
# File lib/sequel_schema_dot_generator.rb, line 27 def initialize params check_params params @db = params[:db] @logger = params[:logger] || Logger.new(STDERR) @dot_template_path = params[:dot_template_path] || File.join( File.dirname(__FILE__), 'sequel_schema_dot_generator/diagram.dot.erb') @colored_associations = params[:colored_associations] initialize_ss params[:schema_source_type] end
Public Instance Methods
generate()
click to toggle source
@return [String] Content which can be passed to dot parsing tool
# File lib/sequel_schema_dot_generator.rb, line 50 def generate tables = [] @db.tables.each do |table_name| next if table_name == :schema_info tables << [table_name, @db.schema(table_name)] end relations = @schema_source.relations if @colored_associations edge_colors = random_color_set relations.count else edge_colors = Array.new relations.count, '000000' end ERB.new( File.read(@dot_template_path),nil,'>' ).result(binding) end
initialize_ss(type)
click to toggle source
# File lib/sequel_schema_dot_generator.rb, line 38 def initialize_ss type sst_params = @db, @db.tables if type == :model @schema_source = SchemaSourceType::Model.new *sst_params end # Column Source as fallback @schema_source ||= SchemaSourceType::Column.new *sst_params end
Private Instance Methods
check_params(params)
click to toggle source
Checks if all parameters for new Object are alright
# File lib/sequel_schema_dot_generator.rb, line 98 def check_params params unless params[:db].is_a?Sequel::Database raise 'Database connection is supposed to be Sequel::Database. %s given'%params[:database_connection].class.name end unless params[:dot_template_path].nil? || params[:dot_template_path].is_a?(String) || File.exist?(params[:dot_template_path]) raise 'Template path is supposed to be string with an existing file. %s given'%params[:dot_template_path].inspect end unless params[:logger].nil? || params[:logger].is_a?(Logger) raise 'Logger is supposed to be... Logger, know. %s given'%params[:logger].inspect end unless params[:colored_associations].nil? || !!params[:colored_associations] == params[:colored_associations] raise 'Colored association is supposed to be boolean. %s given'%params[:colored_associations].inspect end end
random_color_set(number)
click to toggle source
@param [Integer] number of colors we want to generate
@return [Array] of random and unique colors
# File lib/sequel_schema_dot_generator.rb, line 74 def random_color_set number raise 'Number of colors must be greater than 0' if number < 1 colors = [] (1..number).each do colors << random_unique_hex_color(colors) end colors end
random_unique_hex_color(existing)
click to toggle source
@param [Array] existing colors which should be avoided
@return [String] Random color which is not presented in existing param
# File lib/sequel_schema_dot_generator.rb, line 88 def random_unique_hex_color existing color = '%06x' % (rand * 0xffffff) if existing.include?(color) random_unique_hex_color existing else color end end