Class: DatabasusRuby::Object
- Inherits:
-
Object
- Object
- DatabasusRuby::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 database.workspace_id 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.
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Object
Returns a new instance of Object.
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.
52
53
54
55
56
57
58
59
|
# File 'lib/databasus_ruby/object.rb', line 52
def method_missing(name, *args, &block)
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?
39
40
41
|
# File 'lib/databasus_ruby/object.rb', line 39
def ==(other)
other.is_a?(Object) && to_h == other.to_h
end
|
24
25
26
|
# File 'lib/databasus_ruby/object.rb', line 24
def [](key)
fetch_attribute(key.to_s)
end
|
#hash ⇒ Integer
44
45
46
|
# File 'lib/databasus_ruby/object.rb', line 44
def hash
to_h.hash
end
|
#inspect ⇒ 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?
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
|
#keys ⇒ 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
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_h ⇒ payload
Also known as:
to_hash
19
20
21
|
# File 'lib/databasus_ruby/object.rb', line 19
def to_h
@attributes
end
|