Class: TocDoc::Resource
- Inherits:
-
Object
- Object
- TocDoc::Resource
- Defined in:
- lib/toc_doc/models/resource.rb
Overview
A lightweight wrapper providing dot-notation access to response fields.
Backed by a Hash, with method_missing for attribute access and #to_h for
round-tripping back to a plain Hash.
Unlike Sawyer, TocDoc does not use hypermedia relations, so this wrapper intentionally stays minimal.
Direct Known Subclasses
Agenda, Availability, Place, Profile, Speciality, VisitMotive
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
Class Method Summary collapse
-
.main_attrs(*keys) ⇒ Array<String>?
Declares which attribute keys are shown in
#inspect. -
.normalize_attrs(attrs) ⇒ Hash{String => Object}
Normalises a raw attribute hash to string keys, mirroring what #initialize does internally.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Equality comparison.
-
#[](key) ⇒ Object?
Read an attribute by name.
-
#[]=(key, value) ⇒ Object
Write an attribute by name.
-
#attribute_names ⇒ Array<String>
Returns the list of attribute names present on this resource.
-
#initialize(attrs = {}) ⇒ Resource
constructor
A new instance of Resource.
-
#inspect ⇒ String
Returns a human-readable representation of the resource showing only the declared Resource.main_attrs (or all attrs when none are declared).
-
#method_missing(method_name, *_args) ⇒ Object
Provides dot-notation access to response fields.
-
#to_h ⇒ Hash{String => Object}
Return a plain Hash representation with all nested Resource values recursively converted to plain Hashes.
-
#to_json ⇒ String
Serialize the resource to a JSON string.
Constructor Details
#initialize(attrs = {}) ⇒ Resource
Returns a new instance of Resource.
56 57 58 |
# File 'lib/toc_doc/models/resource.rb', line 56 def initialize(attrs = {}) @attrs = self.class.normalize_attrs(attrs) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *_args) ⇒ Object
Provides dot-notation access to response fields.
On first access, defines a singleton method so that subsequent calls
bypass method_missing entirely. The defined method reads live from
@attrs, so mutations via []= are always reflected.
136 137 138 139 140 141 142 143 144 |
# File 'lib/toc_doc/models/resource.rb', line 136 def method_missing(method_name, *_args) key = method_name.to_s if @attrs.key?(key) define_singleton_method(key) { @attrs[key] } @attrs[key] else super end end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
19 20 21 |
# File 'lib/toc_doc/models/resource.rb', line 19 def attrs @attrs end |
Class Method Details
.main_attrs(*keys) ⇒ Array<String>?
Declares which attribute keys are shown in #inspect.
When called with arguments, sets the list for this class.
When called with no arguments, returns the list (or nil if unset),
walking the ancestor chain so subclasses inherit the declaration.
Subclasses that never call main_attrs fall back to showing all attrs.
44 45 46 47 48 49 50 51 52 |
# File 'lib/toc_doc/models/resource.rb', line 44 def main_attrs(*keys) if keys.empty? ancestor = ancestors.find { |a| a.instance_variable_defined?(:@main_attrs) } ancestor&.instance_variable_get(:@main_attrs) else inherited = superclass.respond_to?(:main_attrs) ? (superclass.main_attrs || []) : [] @main_attrs = (inherited + keys.map(&:to_s)).uniq end end |
.normalize_attrs(attrs) ⇒ Hash{String => Object}
Normalises a raw attribute hash to string keys, mirroring what #initialize does internally. Useful in class-level factory methods that need to inspect attrs before wrapping them in a Resource instance.
28 29 30 |
# File 'lib/toc_doc/models/resource.rb', line 28 def normalize_attrs(attrs) attrs.transform_keys(&:to_s) end |
Instance Method Details
#==(other) ⇒ Boolean
Equality comparison.
Two TocDoc::Resource instances are equal when their attribute hashes match.
A TocDoc::Resource is also equal to a plain Hash with equivalent keys.
103 104 105 106 107 108 109 |
# File 'lib/toc_doc/models/resource.rb', line 103 def ==(other) case other when Resource then @attrs == other.to_h when Hash then @attrs == self.class.normalize_attrs(other) else false end end |
#[](key) ⇒ Object?
Read an attribute by name.
67 68 69 |
# File 'lib/toc_doc/models/resource.rb', line 67 def [](key) @attrs[key.to_s] end |
#[]=(key, value) ⇒ Object
Write an attribute by name.
76 77 78 |
# File 'lib/toc_doc/models/resource.rb', line 76 def []=(key, value) @attrs[key.to_s] = value end |
#attribute_names ⇒ Array<String>
Returns the list of attribute names present on this resource.
123 124 125 |
# File 'lib/toc_doc/models/resource.rb', line 123 def attribute_names @attrs.keys end |
#inspect ⇒ String
Returns a human-readable representation of the resource showing only the declared main_attrs (or all attrs when none are declared).
150 151 152 153 154 155 156 |
# File 'lib/toc_doc/models/resource.rb', line 150 def inspect pairs = inspect_hash.map do |key, value| "@#{key}=#{value.inspect}" end.join(', ') "#<#{self.class} #{pairs}>" end |
#to_h ⇒ Hash{String => Object}
Return a plain Hash representation with all nested TocDoc::Resource values recursively converted to plain Hashes.
84 85 86 |
# File 'lib/toc_doc/models/resource.rb', line 84 def to_h @attrs.transform_values { |v| deep_convert(v) } end |
#to_json ⇒ String
Serialize the resource to a JSON string.
92 93 94 |
# File 'lib/toc_doc/models/resource.rb', line 92 def to_json(*) to_h.to_json(*) end |