Sha256: ab95248d4bf2c3aa320e68c8db67f332ef1e4832e4da854909d40b82291f4156

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module DataGenerator

  ##
  #
  # Data can be stored in memory using DataStorage
  #

  module DataStorage
    @data ||= {}

    class << self

      ##
      #
      # Saves data into memory. Marking by namespace and key
      #
      # *Parameters:*
      # * +ns+ - Namespace
      # * +key+ - Key that should be uniq in namespace
      # * +value+ - Data value
      #


      def store(ns, key, value)
        check_ns(ns)
        @data[ns][key] = value
      end

      ##
      #
      # Gets data from memory. Can get all namespace or single data value in namespace using key
      #
      # *Parameters:*
      # * +ns+ - Namespace
      # * +key+ - Key that isn't necessary required (default to: nil)
      #

      def extract(ns, key=nil)
        check_ns(ns)
        key ? @data[ns][key] : @data[ns]
      end

      ##
      #
      # Deletes all records from namespace
      #
      # *Parameters:*
      # * +ns+ - Namespace
      #

      def clear_ns(ns)
        init_ns(ns)
      end

      private

      def check_ns(ns)
        if ns
          init_ns(ns) if ns_absent?(ns)
        else
          raise 'Data storage namespace can not be empty'
        end
      end

      def ns_absent?(ns)
        !@data.key?(ns)
      end

      def init_ns(ns)
        @data[ns] = {}
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
howitzer-1.0.1 lib/howitzer/utils/data_generator/data_storage.rb