Class: Lutaml::Hal::Resource

Inherits:
Model::Serializable
  • Object
show all
Defined in:
lib/lutaml/hal/resource.rb

Overview

Resource class for all HAL resources

Direct Known Subclasses

Page

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

Returns the value of attribute link_definitions.



38
39
40
# File 'lib/lutaml/hal/resource.rb', line 38

def link_definitions
  @link_definitions
end

Instance Attribute Details

#_global_register_idObject

This is the model register that has fetched this resource, and will be used to resolve links unless overriden in resource#realize()



14
15
16
# File 'lib/lutaml/hal/resource.rb', line 14

def _global_register_id
  @_global_register_id
end

#embedded_dataObject

Embedded resources from the HAL ‘_embedded` section, retained so that links can be realized from already-fetched content.



18
19
20
# File 'lib/lutaml/hal/resource.rb', line 18

def embedded_data
  @embedded_data
end

Class Method Details

Creates a Link class that helps us realize the targeted class Delegates to LinkClassFactory for simplified implementation



142
143
144
# File 'lib/lutaml/hal/resource.rb', line 142

def create_link_class(realize_class_name)
  LinkClassFactory.create_for(self, realize_class_name)
end

The “links” class holds the ‘_links` object which contains the resource-linked Link classes Delegates to LinkSetClassFactory for simplified implementation



132
133
134
# File 'lib/lutaml/hal/resource.rb', line 132

def create_link_set_class
  LinkSetClassFactory.create_for(self)
end

.from_embedded(json_data, register_name = nil) ⇒ Object

Create a resource instance from embedded JSON data



31
32
33
34
35
# File 'lib/lutaml/hal/resource.rb', line 31

def self.from_embedded(json_data, register_name = nil)
  instance = from_json(json_data.to_json)
  instance._global_register_id = register_name if register_name
  instance
end

This method obtains the Links class that holds the Link classes Delegates to LinkSetClassFactory for simplified implementation



125
126
127
# File 'lib/lutaml/hal/resource.rb', line 125

def get_link_set_class
  create_link_set_class
end

The developer defines a link to another resource The “key” is the name of the attribute in the JSON The “realize_class” is the class to be realized The “collection” is a boolean indicating if the link is a collection of resources or a single resource The “type” is the type of the link (default is :link, can be :resource)

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/lutaml/hal/resource.rb', line 54

def hal_link(attr_key,
             key:,
             realize_class:,
             link_class: nil,
             link_set_class: nil,
             collection: false,
             type: :link)
  # Validate required parameters
  raise ArgumentError, 'realize_class parameter is required' if realize_class.nil?

  # Use the provided "key" as the attribute name
  attribute_name = attr_key.to_sym

  Hal.debug_log "Defining HAL link for `#{attr_key}` with realize class `#{realize_class}`"

  # Normalize realize_class to a string for consistent handling
  # Support both Class objects (when autoload is available) and strings (for delayed interpretation)
  realize_class_name = case realize_class
                       when Class
                         realize_class.name.split('::').last # Use simple name from actual class
                       when String
                         realize_class # Use string as-is for lazy resolution
                       else
                         raise ArgumentError,
                               "realize_class must be a Class or String, got #{realize_class.class}"
                       end

  # Create a dynamic LinkSet class if `link_set_class:` is not provided.
  # This must happen BEFORE creating the Link class to ensure proper order
  link_set_klass = link_set_class || create_link_set_class

  # Ensure it was actually created
  raise 'Failed to create LinkSet class' if link_set_klass.nil?

  # Create a dynamic Link subclass name based on "realize_class", the
  # class to realize for a Link object, if `link_class:` is not provided.
  link_klass = link_class || create_link_class(realize_class_name)

  # Now add the link to the LinkSet class
  unless link_set_class
    link_set_klass.class_eval do
      # Declare the corresponding lutaml-model attribute
      # Pass collection parameter correctly to the attribute definition
      if collection
        attribute attribute_name, link_klass, collection: true
      else
        attribute attribute_name, link_klass
      end

      # Define the mapping for the attribute
      key_value do
        map key, to: attribute_name
      end
    end
  end

  # Create a new link definition for future reference
  link_def = {
    attribute_name: attribute_name,
    key: attr_key,
    klass: link_klass,
    collection: collection,
    type: type
  }

  @link_definitions ||= {}
  @link_definitions[key] = link_def
end

.inherited(subclass) ⇒ Object

Callback for when a subclass is created



41
42
43
44
45
46
# File 'lib/lutaml/hal/resource.rb', line 41

def inherited(subclass)
  super
  subclass.class_eval do
    init_links_definition
  end
end


136
137
138
# File 'lib/lutaml/hal/resource.rb', line 136

def init_links_definition
  @link_definitions = {}
end

Instance Method Details

#get_embedded(key) ⇒ Object

Get embedded content for a specific key



26
27
28
# File 'lib/lutaml/hal/resource.rb', line 26

def get_embedded(key)
  embedded_data&.[](key.to_s)
end

#has_embedded?(key) ⇒ Boolean

Check if embedded data exists for a given key

Returns:

  • (Boolean)


21
22
23
# File 'lib/lutaml/hal/resource.rb', line 21

def has_embedded?(key)
  embedded_data&.key?(key.to_s)
end