Class: DatabasusRuby::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/databasus_ruby/object.rb,
sig/databasus_ruby.rbs

Overview

A thin, duck-typed wrapper around a decoded JSON payload.

database = client.databases.get(id)
database.name                 # => "primary"
database.workspace_id         # => same as database.workspaceId
database.postgresqlLogical.host

Nested hashes (and arrays of hashes) are wrapped on access, so the whole tree reads the same way. The Databasus API omits empty fields, so reading a key that is not in the payload returns nil rather than raising.

Direct Known Subclasses

Auth, Backups, Databases, Notifiers, Record, Storages, Users, Workspaces

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Object

Returns a new instance of Object.

Parameters:

  • attributes (payload) (defaults to: {})


15
16
17
# File 'lib/databasus_ruby/object.rb', line 15

def initialize(attributes = {})
  @attributes = normalize(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ void

This method returns an undefined value.

Parameters:



52
53
54
55
56
57
58
59
# File 'lib/databasus_ruby/object.rb', line 52

def method_missing(name, *args, &block)
  # Endpoint classes inherit from Object without carrying a payload; for
  # them an unknown method is a genuine mistake, not a missing field.
  return super if @attributes.nil?
  return super unless args.empty? && block.nil?

  fetch_attribute(name.to_s)
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Parameters:

Returns:

  • (Boolean)


39
40
41
# File 'lib/databasus_ruby/object.rb', line 39

def ==(other)
  other.is_a?(Object) && to_h == other.to_h
end

#[](key) ⇒ Object

Parameters:

  • key (String, Symbol)

Returns:



24
25
26
# File 'lib/databasus_ruby/object.rb', line 24

def [](key)
  fetch_attribute(key.to_s)
end

#hashInteger

Returns:

  • (Integer)


44
45
46
# File 'lib/databasus_ruby/object.rb', line 44

def hash
  to_h.hash
end

#inspectString

Returns:

  • (String)


48
49
50
# File 'lib/databasus_ruby/object.rb', line 48

def inspect
  "#<#{self.class.name} #{keys.join(", ")}>"
end

#key?(key) ⇒ Boolean Also known as: has_key?

Parameters:

  • key (String, Symbol)

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/databasus_ruby/object.rb', line 28

def key?(key)
  return false if @attributes.nil?

  @attributes.key?(key.to_s) || @attributes.key?(camelize(key.to_s))
end

#keysArray[String]

Returns:

  • (Array[String])


35
36
37
# File 'lib/databasus_ruby/object.rb', line 35

def keys
  @attributes.nil? ? [] : @attributes.keys
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Parameters:

  • name (Symbol)
  • include_private (Boolean) (defaults to: false)

Returns:

  • (Boolean)


61
62
63
# File 'lib/databasus_ruby/object.rb', line 61

def respond_to_missing?(name, include_private = false)
  @attributes.nil? ? super : true
end

#to_hpayload Also known as: to_hash

Returns:

  • (payload)


19
20
21
# File 'lib/databasus_ruby/object.rb', line 19

def to_h
  @attributes
end