Class: ReactorSDK::Resources::BaseResource

Inherits:
Object
  • Object
show all
Defined in:
lib/reactor_sdk/resources/base_resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, type:, attributes: {}, meta: {}, relationships: {}) ⇒ BaseResource

Returns a new instance of BaseResource.

Parameters:

  • id (String)

    Adobe resource ID

  • type (String)

    JSON:API resource type

  • attributes (Hash) (defaults to: {})

    Resource attribute values from the API response

  • meta (Hash) (defaults to: {})

    Optional metadata from the API response

  • relationships (Hash) (defaults to: {})

    Optional relationships from the API response



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       = meta
  @relationships = relationships
end

Instance Attribute Details

#attributesHash (readonly)

Returns Full raw attributes hash from the JSON:API response Available for accessing fields not declared with the attribute macro.

Returns:

  • (Hash)

    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

#idString (readonly)

Returns Adobe resource ID (e.g. “PR1234abcd…”).

Returns:

  • (String)

    Adobe resource ID (e.g. “PR1234abcd…”)



35
36
37
# File 'lib/reactor_sdk/resources/base_resource.rb', line 35

def id
  @id
end

#metaHash (readonly)

Returns Meta hash from the JSON:API response.

Returns:

  • (Hash)

    Meta hash from the JSON:API response



45
46
47
# File 'lib/reactor_sdk/resources/base_resource.rb', line 45

def meta
  @meta
end

#relationshipsHash (readonly)

Returns Relationships hash from the JSON:API response.

Returns:

  • (Hash)

    Relationships hash from the JSON:API response



48
49
50
# File 'lib/reactor_sdk/resources/base_resource.rb', line 48

def relationships
  @relationships
end

#typeString (readonly)

Returns JSON:API resource type (e.g. “rules”, “properties”).

Returns:

  • (String)

    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.

Examples:

Declare a plain string attribute

attribute :name

Declare a boolean attribute (adds enabled? alias)

attribute :enabled, as: :boolean

Declare an array attribute with a default

attribute :domains, default: []

Parameters:

  • name (Symbol)

    Attribute name — must match the JSON:API field name

  • as (Symbol) (defaults to: nil)

    Type cast — :boolean adds a ? method alias (optional)

  • default (Object) (defaults to: nil)

    Default value when the attribute is nil (optional)



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.

Parameters:

  • other (Object)

    Object to compare with

Returns:

  • (Boolean)


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.

Parameters:

  • key (String, Symbol)

    Attribute name

Returns:

  • (Object, nil)

    Attribute value or nil if not present



104
105
106
# File 'lib/reactor_sdk/resources/base_resource.rb', line 104

def [](key)
  @attributes[key.to_s]
end

#inspectString

Returns a readable string representation for debugging. Subclasses override this to include their most useful fields.

Returns:

  • (String)


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.

Parameters:

  • name (String, Symbol)

Returns:

  • (Hash, nil)


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.

Parameters:

  • name (String, Symbol)

Returns:

  • (String, nil)


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.

Parameters:

  • name (String, Symbol)

Returns:

  • (Array<String>)


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_hHash

Returns a plain hash representation of the resource. Useful for serialisation, logging, and debugging.

Returns:

  • (Hash)


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