Class: Yandex360::Record
- Inherits:
-
Object
- Object
- Yandex360::Record
- Defined in:
- lib/yandex360/record.rb
Overview
Base for every response object.
The parsed body is kept as it arrived and declared fields are read from it through real methods, so a misspelled attribute raises NoMethodError rather than quietly returning nil, and a field the API adds tomorrow is still reachable through #[] without waiting for a release.
Declared names are snake_case. Where the API spells a field differently the original name is kept as a deprecated alias; see #attribute.
Direct Known Subclasses
Alias, AllowList, AuditEvent, Department, DepartmentAlias, DnsRecord, Domain, Domain2FA, DomainDkim, DomainPassword, DomainPolicy, DomainSession, DomainStatus, ExternalContact, Group, GroupMembers, MailAddressBook, MailRule, MailSenderInfo, MailUserRules, Mailbox, MailboxActor, MailboxResource, MailboxTask, Organization, Response, RoutingRules, ServiceApplication, User, User2FA
Class Method Summary collapse
-
.attribute(name, from: nil) ⇒ Object
Declares a reader.
- .attribute_keys ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#[](key) ⇒ Object
Reads any key, declared or not.
- #hash ⇒ Object
-
#initialize(data) ⇒ Record
constructor
A new instance of Record.
- #inspect ⇒ Object
- #key?(key) ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(data) ⇒ Record
Returns a new instance of Record.
37 38 39 40 |
# File 'lib/yandex360/record.rb', line 37 def initialize(data) @data = data.is_a?(Hash) ? data : {} @wrapped = {} end |
Class Method Details
.attribute(name, from: nil) ⇒ Object
Declares a reader. from gives the key in the response when it differs
from the Ruby name, and that original spelling stays available as a
deprecated alias so existing code keeps working through 3.x.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/yandex360/record.rb', line 18 def attribute(name, from: nil) key = (from || name).to_s attribute_keys[name.to_sym] = key define_method(name) { self[key] } return if key == name.to_s define_method(key) do warn "[yandex360] #{self.class}##{key} is deprecated, use ##{name}", uplevel: 1 self[key] end end |
.attribute_keys ⇒ Object
31 32 33 34 |
# File 'lib/yandex360/record.rb', line 31 def attribute_keys @attribute_keys ||= superclass.respond_to?(:attribute_keys) ? superclass.attribute_keys.dup : {} end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
59 60 61 |
# File 'lib/yandex360/record.rb', line 59 def ==(other) other.is_a?(self.class) && other.to_h == to_h end |
#[](key) ⇒ Object
Reads any key, declared or not. Nested hashes and arrays of hashes come back wrapped, so user.name.first and rule.actions.first.action work.
44 45 46 47 48 49 |
# File 'lib/yandex360/record.rb', line 44 def [](key) key = key.to_s return @wrapped[key] if @wrapped.key?(key) @wrapped[key] = wrap(@data[key]) end |
#hash ⇒ Object
64 65 66 |
# File 'lib/yandex360/record.rb', line 64 def hash [self.class, @data].hash end |
#inspect ⇒ Object
68 69 70 |
# File 'lib/yandex360/record.rb', line 68 def inspect "#<#{self.class.name} #{@data.keys.join(', ')}>" end |
#key?(key) ⇒ Boolean
55 56 57 |
# File 'lib/yandex360/record.rb', line 55 def key?(key) @data.key?(key.to_s) end |
#to_h ⇒ Object
51 52 53 |
# File 'lib/yandex360/record.rb', line 51 def to_h @data end |