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



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

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



22
23
24
25
# File 'lib/submodules/ably-ruby/lib/ably/modules/model_common.rb', line 22

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)


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

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)


29
30
31
# File 'lib/submodules/ably-ruby/lib/ably/modules/model_common.rb', line 29

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)


35
36
37
# File 'lib/submodules/ably-ruby/lib/ably/modules/model_common.rb', line 35

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

#to_sObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/submodules/ably-ruby/lib/ably/modules/model_common.rb', line 45

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