Module: Booqable::StrictAttributes

Defined in:
lib/booqable/strict_attributes.rb

Overview

Strict attribute reads for resources created by this gem

Sawyer::Resource#method_missing silently answers reads of absent attributes with nil. That turns typos and renamed API fields (e.g. time_zone vs default_timezone) into silent data bugs. For resources created by a SawyerAgent, this module raises MissingAttribute instead — both for plain reads (resource.foo) and predicate reads (resource.foo?).

Attributes that ARE present in the payload with a null value still return nil — only reads of keys that are absent from the payload raise.

Everything else about Sawyer::Resource is unchanged: attribute writes (resource.foo = 1) still define new attributes, hash-style access (resource[:foo]) stays a lenient probe returning nil for absent keys, and to_h/to_attrs, key?, dig, fetch, enumeration, marshaling, and respond_to? semantics all behave as before. Resources created by other gems' Sawyer agents are unaffected.

Constant Summary collapse

ATTRIBUTE_READ_PATTERN =

Matches plain attribute reads (foo) and predicate reads (foo?). Setters (foo=) stay permitted since Sawyer allows adding attributes.

/\A([a-z0-9_]+)(\?)?\z/i

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

Raise MissingAttribute for reads of absent attributes on strict resources; defer to Sawyer's behavior for everything else.



38
39
40
41
42
43
# File 'lib/booqable/strict_attributes.rb', line 38

def method_missing(method, *)
  attr_name = booqable_missing_attribute_read(method)
  raise Booqable::MissingAttribute.new(attr_name, self) if attr_name

  super
end