Sha256: db3030c48b264e31f52d739afb04407ccac64151c6427cadf3b06b8f00e2f85e

Contents?: true

Size: 1.87 KB

Versions: 7

Compression:

Stored size: 1.87 KB

Contents

require 'base64'
require 'ably/modules/conversions'
require 'ably/modules/message_pack'

module Ably::Modules
  # Common model functionality shared across many {Ably::Models}
  module ModelCommon
    include Conversions
    include MessagePack

    def self.included(base)
      base.extend(ClassMethods)
    end

    # Provide a normal Hash accessor to the underlying raw message object
    #
    # @return [Object]
    def [](key)
      attributes[key]
    end

    def ==(other)
      other.kind_of?(self.class) &&
        attributes == other.attributes
    end

    # Return a JSON ready object from the underlying #attributes using Ably naming conventions for keys
    # @return [Hash]
    def as_json(*args)
      attributes.as_json.reject { |key, val| val.nil? }
    end

    # Stringify the JSON representation of this object from the underlying #attributes
    # @return [String]
    def to_json(*args)
      as_json.to_json(*args)
    end

    # @!attribute [r] hash
    # @return [Integer] Compute a hash-code for this hash. Two hashes with the same content will have the same hash code
    def hash
      attributes.hash
    end

    module ClassMethods
      # Return a new instance of this object using the provided JSON-like object or JSON string
      # @param [Hash, String] JSON-like object or JSON string
      # @return a new instance o this object
      def from_json(json_like_object)
        if json_like_object.kind_of?(String)
          new(JSON.parse(json_like_object))
        else
          new(json_like_object)
        end
      end
    end

    private
    def ensure_utf8_string_for(attribute, value)
      if value
        raise ArgumentError, "#{attribute} must be a String" unless value.kind_of?(String)
        raise ArgumentError, "#{attribute} cannot use ASCII_8BIT encoding, please use UTF_8 encoding" unless value.encoding == Encoding::UTF_8
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
ably-1.0.7 lib/ably/modules/model_common.rb
ably-rest-1.0.6 lib/submodules/ably-ruby/lib/ably/modules/model_common.rb
ably-1.0.6 lib/ably/modules/model_common.rb
ably-rest-1.0.5 lib/submodules/ably-ruby/lib/ably/modules/model_common.rb
ably-1.0.5 lib/ably/modules/model_common.rb
ably-1.0.4 lib/ably/modules/model_common.rb
ably-1.0.3 lib/ably/modules/model_common.rb