Sha256: 52a650b05e69d46813e4581cdfdbd4fd01b49c6e6cf4a7ba7a2c9b43b79813b1

Contents?: true

Size: 860 Bytes

Versions: 8

Compression:

Stored size: 860 Bytes

Contents

module Intercom
  # Sub-class of {Hash} for storing custom data attributes.
  # Doesn't allow nested Hashes or Arrays. And requires {String} or {Symbol} keys.
  class UserCustomData < 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("custom_data does not support nested data structures (key: #{key}, value: #{value}") if value.is_a?(Array) || value.is_a?(Hash)
      raise ArgumentError.new("custom_data key must be String or Symbol: #{key}") unless key.is_a?(String) || key.is_a?(Symbol)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
intercom-0.0.12 lib/intercom/user_custom_data.rb
intercom-0.0.11 lib/intercom/user_custom_data.rb
intercom-0.0.10 lib/intercom/user_custom_data.rb
intercom-0.0.9 lib/intercom/user_custom_data.rb
intercom-0.0.8 lib/intercom/user_custom_data.rb
intercom-0.0.7 lib/intercom/user_custom_data.rb
intercom-0.0.6 lib/intercom/user_custom_data.rb
intercom-0.0.5 lib/intercom/user_custom_data.rb