Class: Lutaml::Hal::Resource

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

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.



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

def link_definitions
  @link_definitions
end

Instance Attribute Details

#_global_register_idObject

Returns the value of attribute _global_register_id.



8
9
10
# File 'lib/lutaml/hal/resource.rb', line 8

def _global_register_id
  @_global_register_id
end

#embedded_dataObject

Returns the value of attribute embedded_data.



8
9
10
# File 'lib/lutaml/hal/resource.rb', line 8

def embedded_data
  @embedded_data
end

Class Method Details



101
102
103
# File 'lib/lutaml/hal/resource.rb', line 101

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


93
94
95
# File 'lib/lutaml/hal/resource.rb', line 93

def create_link_set_class
  LinkSetClassFactory.create_for(self)
end

.from_embedded(json_data, register_name = nil) ⇒ Object



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

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


89
90
91
# File 'lib/lutaml/hal/resource.rb', line 89

def get_link_set_class
  create_link_set_class
end

Raises:

  • (ArgumentError)


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

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

  attribute_name = attr_key.to_sym

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

  realize_class_name = case realize_class
                       when Class
                         realize_class.name.split('::').last
                       when String
                         realize_class
                       else
                         raise ArgumentError,
                               "realize_class must be a Class or String, got #{realize_class.class}"
                       end

  link_set_klass = link_set_class || create_link_set_class

  raise 'Failed to create LinkSet class' if link_set_klass.nil?

  link_klass = link_class || create_link_class(realize_class_name)

  unless link_set_class
    link_set_klass.class_eval do
      if collection
        attribute attribute_name, link_klass, collection: true
      else
        attribute attribute_name, link_klass
      end

      key_value do
        map key, to: attribute_name
      end
    end
  end

  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



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

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


97
98
99
# File 'lib/lutaml/hal/resource.rb', line 97

def init_links_definition
  @link_definitions = {}
end

Instance Method Details

#get_embedded(key) ⇒ Object



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

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

#has_embedded?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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