Class: ReactorSDK::Resources::BaseResource
- Inherits:
-
Object
- Object
- ReactorSDK::Resources::BaseResource
- Defined in:
- lib/reactor_sdk/resources/base_resource.rb
Direct Known Subclasses
AppConfiguration, AuditEvent, Build, Callback, Company, DataElement, Environment, Extension, ExtensionPackage, ExtensionPackageUsageAuthorization, Host, Library, LibraryWithResources, Note, Profile, Property, Revision, Rule, RuleComponent, Secret
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
readonly
Full raw attributes hash from the JSON:API response Available for accessing fields not declared with the attribute macro.
-
#id ⇒ String
readonly
Adobe resource ID (e.g. “PR1234abcd…”).
-
#meta ⇒ Hash
readonly
Meta hash from the JSON:API response.
-
#relationships ⇒ Hash
readonly
Relationships hash from the JSON:API response.
-
#type ⇒ String
readonly
JSON:API resource type (e.g. “rules”, “properties”).
Class Method Summary collapse
-
.attribute(name, as: nil, default: nil) ⇒ Object
Class-level macro for declaring typed attribute readers.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Two resources are equal if they represent the same Adobe resource — same id and same type, regardless of which attributes were loaded.
-
#[](key) ⇒ Object?
Provides fallback access to any attribute by name.
-
#initialize(id:, type:, attributes: {}, meta: {}, relationships: {}) ⇒ BaseResource
constructor
A new instance of BaseResource.
-
#inspect ⇒ String
Returns a readable string representation for debugging.
-
#relationship_data(name) ⇒ Hash?
Returns the raw relationship object for a given relationship name.
-
#relationship_id(name) ⇒ String?
Returns the related resource ID when the relationship contains a single linkage.
-
#relationship_ids(name) ⇒ Array<String>
Returns related resource IDs when the relationship contains collection linkage.
-
#to_h ⇒ Hash
Returns a plain hash representation of the resource.
Constructor Details
#initialize(id:, type:, attributes: {}, meta: {}, relationships: {}) ⇒ BaseResource
Returns a new instance of BaseResource.
88 89 90 91 92 93 94 |
# File 'lib/reactor_sdk/resources/base_resource.rb', line 88 def initialize(id:, type:, attributes: {}, meta: {}, relationships: {}) @id = id @type = type @attributes = attributes @meta = @relationships = relationships end |
Instance Attribute Details
#attributes ⇒ Hash (readonly)
Returns Full raw attributes hash from the JSON:API response Available for accessing fields not declared with the attribute macro.
42 43 44 |
# File 'lib/reactor_sdk/resources/base_resource.rb', line 42 def attributes @attributes end |
#id ⇒ String (readonly)
Returns Adobe resource ID (e.g. “PR1234abcd…”).
35 36 37 |
# File 'lib/reactor_sdk/resources/base_resource.rb', line 35 def id @id end |
#meta ⇒ Hash (readonly)
Returns Meta hash from the JSON:API response.
45 46 47 |
# File 'lib/reactor_sdk/resources/base_resource.rb', line 45 def @meta end |
#relationships ⇒ Hash (readonly)
Returns Relationships hash from the JSON:API response.
48 49 50 |
# File 'lib/reactor_sdk/resources/base_resource.rb', line 48 def relationships @relationships end |
#type ⇒ String (readonly)
Returns JSON:API resource type (e.g. “rules”, “properties”).
38 39 40 |
# File 'lib/reactor_sdk/resources/base_resource.rb', line 38 def type @type end |
Class Method Details
.attribute(name, as: nil, default: nil) ⇒ Object
Class-level macro for declaring typed attribute readers. Called in subclass bodies to define which JSON:API attributes are exposed as Ruby methods on the resource object.
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/reactor_sdk/resources/base_resource.rb', line 68 def self.attribute(name, as: nil, default: nil) # Define the reader method that pulls from the attributes hash define_method(name) do value = @attributes[name.to_s] value.nil? ? default : value end # Boolean fields get a ? alias automatically # e.g. attribute :enabled, as: :boolean # generates both enabled and enabled? define_method(:"#{name}?") { !!public_send(name) } if as == :boolean end |
Instance Method Details
#==(other) ⇒ Boolean
Two resources are equal if they represent the same Adobe resource —same id and same type, regardless of which attributes were loaded.
158 159 160 161 162 |
# File 'lib/reactor_sdk/resources/base_resource.rb', line 158 def ==(other) other.is_a?(BaseResource) && other.id == id && other.type == type end |
#[](key) ⇒ Object?
Provides fallback access to any attribute by name. Use this for attributes not declared with the attribute macro, or for dynamic access patterns.
104 105 106 |
# File 'lib/reactor_sdk/resources/base_resource.rb', line 104 def [](key) @attributes[key.to_s] end |
#inspect ⇒ String
Returns a readable string representation for debugging. Subclasses override this to include their most useful fields.
147 148 149 |
# File 'lib/reactor_sdk/resources/base_resource.rb', line 147 def inspect "#<#{self.class.name} id=#{@id.inspect} type=#{@type.inspect}>" end |
#relationship_data(name) ⇒ Hash?
Returns the raw relationship object for a given relationship name.
114 115 116 |
# File 'lib/reactor_sdk/resources/base_resource.rb', line 114 def relationship_data(name) @relationships[name.to_s] end |
#relationship_id(name) ⇒ String?
Returns the related resource ID when the relationship contains a single linkage.
124 125 126 |
# File 'lib/reactor_sdk/resources/base_resource.rb', line 124 def relationship_id(name) relationship_data(name)&.dig('data', 'id') end |
#relationship_ids(name) ⇒ Array<String>
Returns related resource IDs when the relationship contains collection linkage.
134 135 136 137 138 139 |
# File 'lib/reactor_sdk/resources/base_resource.rb', line 134 def relationship_ids(name) data = relationship_data(name)&.fetch('data', nil) return [] unless data.is_a?(Array) data.filter_map { |item| item['id'] } end |
#to_h ⇒ Hash
Returns a plain hash representation of the resource. Useful for serialisation, logging, and debugging.
170 171 172 173 174 175 176 177 178 |
# File 'lib/reactor_sdk/resources/base_resource.rb', line 170 def to_h { id: @id, type: @type, attributes: @attributes, meta: @meta, relationships: @relationships } end |