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



60
61
62
63
64
65
66
67
68
# File 'lib/lutaml/hal/resource.rb', line 60

def get_links_class
  parent_klass_name = name.split('::')[0..-2].join('::')
  child_klass_name = "#{name.split('::').last}Links"
  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
# File 'lib/lutaml/hal/resource.rb', line 28

def hal_link(attr_key, key:, realize_class:, 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
  link_klass = create_link_class(realize_class)
  links_klass = get_links_class
  links_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

  # 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_links_class
    init_links_definition
  end
end