lib/legion/data.rb in legion-data-0.1.1 vs lib/legion/data.rb in legion-data-0.2.0

- old
+ new

@@ -1,43 +1,63 @@ require 'legion/data/version' +require_relative 'data/settings' require 'sequel' -require 'legion/data/connection' -require 'legion/data/migration' -require 'legion/data/model' +require_relative 'data/connection' +require_relative 'data/model' +require_relative 'data/migration' + module Legion module Data - def build(options = {}) - @connected = false - options.merge!(default_options) { |_key, v1, _v2| v1 } - @connection = connect(options[:connection]) if options[:connection][:auto_connect] - return unless options[:connection][:auto_connect] + class << self + def setup + connection_setup + migrate + load_models + # setup_cache + end - migrate(@connection, options[:migration]) - model(options[:model]) if options[:model][:auto_load] - end + def connection_setup + return if Legion::Settings[:data][:connected] - def connect(_options = {}) - Legion::Data::Connection.new - end + Legion::Data::Connection.setup + end - def migrate(connection, options = {}) - return false if options.key?(:auto_migrate) && options[:auto_migrate] + def migrate + Legion::Data::Migration.migrate + end - Legion::Data::Migration.new(connection.database.connection) - end + def load_models + Legion::Data::Models.load + end - def model(_options = {}) - Legion::Data::Models.new - end + def connection + Legion::Data::Connection.sequel + end - def default_options - { migration: { auto_migrate: true }, - model: { auto_load: true }, - connection: { auto_connect: true } } - end + def setup_cache # rubocop:disable Metrics/AbcSize + return if Legion::Settings[:data][:cache][:enable] - def merge_options(options, default = default_options) - options.merge!(default) { |_key, v1, _v2| v1 } + unless Legion::Settings[:cache][:connected] + Legion::Logging.warn 'Legion::Data has caching enabled but Legion::Cache is not started' + end + return unless Legion::Settings[:cache][:connected] + + # Legion::Data::Model::Relationship.plugin :caching, Legion::Cache, ttl: 10 + Legion::Data::Model::Chain.plugin :caching, Legion::Cache, ttl: 60 + Legion::Data::Model::Datacenter.plugin :caching, Legion::Cache, ttl: 120 + Legion::Data::Model::Extension.plugin :caching, Legion::Cache, ttl: 120 + Legion::Data::Model::Function.plugin :caching, Legion::Cache, ttl: 120 + Legion::Data::Model::Namespace.plugin :caching, Legion::Cache, ttl: 120 + Legion::Data::Model::Node.plugin :caching, Legion::Cache, ttl: 10 + Legion::Data::Model::TaskLog.plugin :caching, Legion::Cache, ttl: 12 + Legion::Data::Model::Task.plugin :caching, Legion::Cache, ttl: 10 + Legion::Data::Model::User.plugin :caching, Legion::Cache, ttl: 120 + Legion::Data::Model::Group.plugin :caching, Legion::Cache, ttl: 120 + end + + def shutdown + Legion::Data::Connection.shutdown + end end end end