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

Instance Method Summary collapse

Class Attribute Details

Returns the value of attribute link_definitions.



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

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



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

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



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

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



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

def self.from_embedded(json_data, register_name = nil)
  instance = from_json(json_data.to_json)
  instance.instance_variable_set("@#{Hal::REGISTER_ID_ATTR_NAME}", register_name) if register_name
  instance
end

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



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

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)


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
122
# File 'lib/lutaml/hal/resource.rb', line 55

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



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

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


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

def init_links_definition
  @link_definitions = {}
end

Instance Method Details

#embedded_dataObject

Access embedded data if available



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

def embedded_data
  @_embedded
end

#get_embedded(key) ⇒ Object

Get embedded content for a specific key



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

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)


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

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