Module: Ably::Modules::ModelCommon

Overview

Common model functionality shared across many Ably::Models

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MessagePack

#to_msgpack

Instance Attribute Details

#hashInteger (readonly)

Returns Compute a hash-code for this hash. Two hashes with the same content will have the same hash code.

Returns:

  • (Integer)

    Compute a hash-code for this hash. Two hashes with the same content will have the same hash code



46
47
48
# File 'lib/submodules/ably-ruby/lib/ably/modules/model_common.rb', line 46

def hash
  attributes.hash
end

Class Method Details

.included(base) ⇒ Object



11
12
13
# File 'lib/submodules/ably-ruby/lib/ably/modules/model_common.rb', line 11

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

Instance Method Details

#==(other) ⇒ Object



23
24
25
26
# 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) ⇒ Object

Provide a normal Hash accessor to the underlying raw message object

Returns:

  • (Object)


19
20
21
# File 'lib/submodules/ably-ruby/lib/ably/modules/model_common.rb', line 19

def [](key)
  attributes[key]
end

#as_json(*args) ⇒ Hash

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

Returns:

  • (Hash)


32
33
34
# 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

#to_json(*args) ⇒ String

Stringify the JSON representation of this object from the underlying #attributes

Returns:

  • (String)


40
41
42
# File 'lib/submodules/ably-ruby/lib/ably/modules/model_common.rb', line 40

def to_json(*args)
  as_json.to_json(*args)
end

#to_sObject



50
51
52
53
54
55
56
57
58
59
60
61
# 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