Sha256: e400e5a934225e0602bb180790e21a8df2ea861688260d5980eb243d09f860b6

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

module DCA
  module Storage
    extend ActiveSupport::Concern

    module ClassMethods
      def establish_connection config_name = :db
        @config = APP_CONFIG[config_name]
      end

      def root_storage
        return @storage if @storage

        return storage if @config

        superclass.root_storage if superclass != Object
      end

      def storage
        return @storage unless @storage.nil?

        if @config
          driver_class = "DCA::#{@config[:driver]}Storage".constantize
          connection = driver_class.establish_connection @config
          @storage = driver_class.new connection, self, @config
        else
          @storage = superclass.root_storage.context self
        end
      end
    end

    def state
      self.class.storage.state self
    end

    def save
      return false unless valid?

      current_state = state

      callback = "before_#{state}"
      send callback if self.respond_to? callback

      self.class.storage.refresh self, current_state

      callback = "after_#{state}"
      send callback if self.respond_to? callback

      current_state
    end

    def destroy
      self.class.storage.refresh self, :remove
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dca-0.1.1 lib/dca/storage/storage.rb
dca-0.1.0 lib/dca/storage/storage.rb