Class: Lutaml::Model::Services::DefaultValueResolver

Inherits:
Base
  • Object
show all
Defined in:
lib/lutaml/model/services/default_value_resolver.rb

Instance Method Summary collapse

Methods inherited from Base

call

Constructor Details

#initialize(attribute, register, instance_object) ⇒ DefaultValueResolver

Returns a new instance of DefaultValueResolver.



7
8
9
10
11
12
13
# File 'lib/lutaml/model/services/default_value_resolver.rb', line 7

def initialize(attribute, register, instance_object)
  super()

  @attribute = attribute
  @register = register
  @instance_object = instance_object
end

Instance Method Details

#defaultObject

Get the default value with proper instance context and casting Returns the evaluated and casted default value



17
18
19
# File 'lib/lutaml/model/services/default_value_resolver.rb', line 17

def default
  attribute.cast_value(default_value, register)
end

#default_set?Boolean

Check if a default value is set (not uninitialized)

Returns:

  • (Boolean)


50
51
52
# File 'lib/lutaml/model/services/default_value_resolver.rb', line 50

def default_set?
  !Utils.uninitialized?(raw_default_value)
end

#default_valueObject

Get the default value with proper instance context Returns the evaluated default value (procs are executed)



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lutaml/model/services/default_value_resolver.rb', line 23

def default_value
  raw_value = raw_default_value
  return raw_value unless raw_value.is_a?(Proc)

  # Execute proc in instance context if available, otherwise call it directly
  if instance_object
    instance_object.instance_exec(&raw_value)
  else
    raw_value.call
  end
end

#raw_default_valueObject

Get the raw default value without executing procs



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lutaml/model/services/default_value_resolver.rb', line 36

def raw_default_value
  if attribute.delegate
    # For delegated attributes, recursively resolve through the service
    delegated_attr = attribute.type(register).attributes[attribute.to]
    self.class.new(delegated_attr, register,
                   instance_object).raw_default_value
  elsif attribute.options.key?(:default)
    attribute.options[:default]
  else
    Lutaml::Model::UninitializedClass.instance
  end
end