lib/pg_objects/manager.rb in pg_objects-1.0.3 vs lib/pg_objects/manager.rb in pg_objects-1.2.0
- old
+ new
@@ -1,69 +1,67 @@
-module PgObjects
- ##
- # Manages process to create objects
- #
- # Usage:
- #
- # Manager.new.load_files(:before).create_objects
- #
- # or
- #
- # Manager.new.load_files(:after).create_objects
- class Manager
- attr_reader :objects, :config, :log
+##
+# Manages process to create objects
+#
+# Usage:
+#
+# Manager.new(config, logger).load_files(:before).create_objects
+#
+# or
+#
+# Manager.new(config, logger).load_files(:after).create_objects
+class PgObjects::Manager
+ attr_reader :objects, :config
- def initialize
- raise UnsupportedAdapterError if ActiveRecord::Base.connection.adapter_name != 'PostgreSQL'
+ def initialize(config, logger)
+ raise PgObjects::UnsupportedAdapterError if ActiveRecord::Base.connection.adapter_name != 'PostgreSQL'
- @objects = []
- @config = PgObjects.config
- @log = Logger.new(silent: config.silent)
- end
+ @objects = []
+ @config = config
+ @log = logger.mute(config.silent) # Logger.new(silent: config.silent)
+ end
- ##
- # event: +:before+ or +:after+
- #
- # used to reference configuration settings +before_path+ and +after_path+
- def load_files(event)
- dir = config.send "#{event}_path"
- Dir[File.join(dir, '**', "*.{#{config.extensions.join(',')}}")].each do |path|
- @objects << PgObjects::DbObject.new(path)
- end
-
- self
+ ##
+ # event: +:before+ or +:after+
+ #
+ # used to reference configuration settings +before_path+ and +after_path+
+ def load_files(event)
+ dir = config.send "#{event}_path"
+ Dir[File.join(dir, '**', "*.{#{config.extensions.join(',')}}")].each do |path|
+ @objects << PgObjects::DbObject.new(path).create
end
- def create_objects
- @objects.each { |obj| create_object obj }
- end
+ self
+ end
- private
+ def create_objects
+ objects.each { create_object(_1) }
+ end
- def create_object(obj)
- return if obj.status == :done
- raise CyclicDependencyError, obj.name if obj.status == :processing
+ private
- obj.status = :processing
+ def create_object(obj)
+ return if obj.status == :done
+ raise PgObjects::CyclicDependencyError, obj.name if obj.status == :processing
- create_dependencies(obj.dependencies)
+ obj.status = :processing
- log.write("creating #{obj.name}")
- ActiveRecord::Base.connection.execute obj.sql_query
+ create_dependencies(obj.dependencies)
- obj.status = :done
- end
+ @log.write("creating #{obj.name}")
+ ActiveRecord::Base.connection.execute obj.sql_query
- def create_dependencies(dependencies)
- dependencies.each { |dep_name| create_object find_object(dep_name) }
- end
+ obj.status = :done
+ end
- def find_object(dep_name)
- result = @objects.select { |obj| [obj.name, obj.full_name, obj.object_name].compact.include? dep_name }
+ def create_dependencies(dependencies)
+ dependencies.each { |dep_name| create_object(find_object(dep_name)) }
+ end
- raise AmbiguousDependencyError, dep_name if result.size > 1
- raise DependencyNotExistError, dep_name if result.empty?
+ def find_object(dep_name)
+ result = @objects.select { |obj| [obj.name, obj.full_name, obj.object_name].compact.include? dep_name }
- result[0]
- end
+ raise PgObjects::AmbiguousDependencyError, dep_name if result.size > 1
+ raise PgObjects::DependencyNotExistError, dep_name if result.empty?
+
+ result[0]
end
end