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

Class Method Summary collapse

Class Attribute Details

Returns the value of attribute link_definitions.



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

def link_definitions
  @link_definitions
end

Class Method Details

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



121
122
123
# File 'lib/lutaml/hal/resource.rb', line 121

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



111
112
113
# File 'lib/lutaml/hal/resource.rb', line 111

def create_link_set_class
  LinkSetClassFactory.create_for(self)
end

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



104
105
106
# File 'lib/lutaml/hal/resource.rb', line 104

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)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
# File 'lib/lutaml/hal/resource.rb', line 33

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



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

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


115
116
117
# File 'lib/lutaml/hal/resource.rb', line 115

def init_links_definition
  @link_definitions = {}
end