Sha256: eb1f20abfbd720e4f9ad80d334afa362808c5bd6be96336f1879772311cab5a7

Contents?: true

Size: 901 Bytes

Versions: 85

Compression:

Stored size: 901 Bytes

Contents

module Intercom
  module Lib

    # Sub-class of {Hash} for storing custom data attributes.
    # Doesn't allow nested Hashes or Arrays. And requires {String} or {Symbol} keys.
    class FlatStore < Hash

      def initialize(attributes={})
        (attributes).each do |key, value|
          validate_key_and_value(key, value)
          self[key] = value
        end
      end

      def []=(key, value)
        validate_key_and_value(key, value)
        super(key.to_s, value)
      end

      def [](key)
        super(key.to_s)
      end

      private
      def validate_key_and_value(key, value)
        raise ArgumentError.new("This does not support nested data structures (key: #{key}, value: #{value}") if value.is_a?(Array) || value.is_a?(Hash)
        raise ArgumentError.new("Key must be String or Symbol: #{key}") unless key.is_a?(String) || key.is_a?(Symbol)
      end
    end
  end
end

Version data entries

85 entries across 85 versions & 2 rubygems

Version Path
intercom-4.2.1 lib/intercom/lib/flat_store.rb
intercom-4.2.0 lib/intercom/lib/flat_store.rb
intercom-4.1.3 lib/intercom/lib/flat_store.rb
intercom-4.1.2 lib/intercom/lib/flat_store.rb
intercom-4.1.1 lib/intercom/lib/flat_store.rb
intercom-4.1.0 lib/intercom/lib/flat_store.rb
intercom-4.0.1 lib/intercom/lib/flat_store.rb
intercom-4.0.0 lib/intercom/lib/flat_store.rb
intercom-3.9.5 lib/intercom/lib/flat_store.rb
intercom-3.9.4 lib/intercom/lib/flat_store.rb
intercom-3.9.3 lib/intercom/lib/flat_store.rb
intercom-3.9.2 lib/intercom/lib/flat_store.rb
intercom-3.9.0 lib/intercom/lib/flat_store.rb
intercom-3.8.1 lib/intercom/lib/flat_store.rb
intercom-3.8.0 lib/intercom/lib/flat_store.rb
intercom-3.7.7 lib/intercom/lib/flat_store.rb
intercom-3.7.6 lib/intercom/lib/flat_store.rb
intercom-3.7.5 lib/intercom/lib/flat_store.rb
intercom-3.7.4 lib/intercom/lib/flat_store.rb
intercom-3.7.3 lib/intercom/lib/flat_store.rb