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)


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

def hash
  attributes.hash
end

Class Method Details

.included(base) ⇒ Object


11
12
13
# File '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/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


18
19
20
# File '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


29
30
31
# File '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


35
36
37
# File '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/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