module Ably::Modules::ModelCommon

Common model functionality shared across many {Ably::Models}

Public Class Methods

included(base) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/model_common.rb, line 11
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

==(other) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/model_common.rb, line 23
def ==(other)
  other.kind_of?(self.class) &&
    attributes == other.attributes
end
[](key) click to toggle source

Provide a normal Hash accessor to the underlying raw message object

@return [Object]

# File lib/submodules/ably-ruby/lib/ably/modules/model_common.rb, line 19
def [](key)
  attributes[key]
end
as_json(*args) click to toggle source

Return a JSON ready object from the underlying attributes using Ably naming conventions for keys

@return [Hash]

# File lib/submodules/ably-ruby/lib/ably/modules/model_common.rb, line 32
def as_json(*args)
  attributes.as_json.reject { |key, val| val.nil? }
end
hash() click to toggle source

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

# File lib/submodules/ably-ruby/lib/ably/modules/model_common.rb, line 46
def hash
  attributes.hash
end
to_json(*args) click to toggle source

Stringify the JSON representation of this object from the underlying attributes

@return [String]

# File lib/submodules/ably-ruby/lib/ably/modules/model_common.rb, line 40
def to_json(*args)
  as_json.to_json(*args)
end
to_s() click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/model_common.rb, line 50
def to_s
  representation = attributes.map do |key, val|
    if val.nil?
      nil
    else
      val_str = val.to_s
      val_str = "#{val_str[0...80]}..." if val_str.length > 80
      "#{key}=#{val_str}"
    end
  end
  "<#{self.class.name}: #{representation.compact.join(', ')}>"
end

Private Instance Methods

ensure_utf8_string_for(attribute, value) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/model_common.rb, line 77
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