Sha256: a44ca6c4b148f870286c874f0941b93d75b4e2464c0cefdb14b80c3020bb304e

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

# Copyright (C) 2011 AMEE UK Ltd. - http://www.amee.com
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.

# :title: Class: AMEE::Db::BaseConfig

require 'singleton'

module AMEE
  module Db
    
    # A singleton class for configuration. Automatically initialised on first use
    # Use like so:
    #
    #   AMEE::Db::Config.instance.store_everything?
    #
    # Separated into BaseConfig and Config to allow unit testing of singleton.
    #
    class BaseConfig
    
      def initialize
        # Default is metadata
        @storage_method = load_storage_method || :metadata
      end
            
      attr_reader :storage_method

      def store_metadata?
        [:metadata, :outputs, :everything].include? storage_method
      end

      def store_outputs?
        [:outputs, :everything].include? storage_method
      end

      def store_everything?
        [:everything].include? storage_method
      end

      private
      
      def load_storage_method
        m = YAML.load_file("#{::Rails.root}/config/persistence.yml")['method'].to_sym rescue nil
        raise "amee-data-persistence: Invalid storage method" unless [:metadata, :outputs, :everything].include? m
        m
      end

    end
    
    class Config < BaseConfig
      include Singleton
    end
      
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
amee-data-persistence-2.1.0 lib/amee/db/config.rb
amee-data-persistence-1.2.0 lib/amee/db/config.rb