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.



11
12
13
# File 'lib/lutaml/hal/resource.rb', line 11

def link_definitions
  @link_definitions
end

Class Method Details

This method obtains the Links class that holds the Link classes



70
71
72
73
74
75
76
77
78
# File 'lib/lutaml/hal/resource.rb', line 70

def get_links_class
  parent_klass_name = name.split('::')[0..-2].join('::')
  child_klass_name = "#{name.split('::').last}LinkSet"
  klass_name = "#{parent_klass_name}::#{child_klass_name}"

  raise unless Object.const_defined?(klass_name)

  Object.const_get(klass_name)
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)



28
29
30
31
32
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
# File 'lib/lutaml/hal/resource.rb', line 28

def hal_link(attr_key,
             key:,
             realize_class:,
             link_class: nil,
             link_set_class: nil,
             collection: false,
             type: :link)
  # Use the provided "key" as the attribute name
  attribute_name = attr_key.to_sym

  # 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)

  # Create a dynamic LinkSet class if `link_set_class:` is not provided.
  unless link_set_class
    link_set_klass = link_set_class || get_links_class
    link_set_klass.class_eval do
      # Declare the corresponding lutaml-model attribute
      attribute attribute_name, link_klass, collection: collection

      # Define the mapping for the attribute
      key_value do
        map attr_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



14
15
16
17
18
19
20
# File 'lib/lutaml/hal/resource.rb', line 14

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