Class: Lutaml::Model::Transform

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/transform.rb

Constant Summary collapse

MAX_CACHE_SIZE =

Maximum number of cached Transform instances before eviction. Covers most OOXML/ISO schemas with ~1000+ classes and multiple registers.

2048

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, register = nil) ⇒ Transform

Returns a new instance of Transform.



62
63
64
65
# File 'lib/lutaml/model/transform.rb', line 62

def initialize(context, register = nil)
  @context = context
  @lutaml_register = register || Lutaml::Model::Config.default_register
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



60
61
62
# File 'lib/lutaml/model/transform.rb', line 60

def context
  @context
end

#lutaml_registerObject (readonly)

Returns the value of attribute lutaml_register.



60
61
62
# File 'lib/lutaml/model/transform.rb', line 60

def lutaml_register
  @lutaml_register
end

Class Method Details

.cache_sizeObject



39
40
41
# File 'lib/lutaml/model/transform.rb', line 39

def self.cache_size
  @transform_cache&.size || 0
end

.cached_transform(context, register) ⇒ Object



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

def self.cached_transform(context, register)
  @transform_cache ||= {}
  cache_key = [context, register]
  entry = @transform_cache[cache_key]
  return entry if entry

  evict_if_needed if @transform_cache.size >= MAX_CACHE_SIZE
  @transform_cache[cache_key] = new(context, register)
end

.clear_cache!Object



35
36
37
# File 'lib/lutaml/model/transform.rb', line 35

def self.clear_cache!
  @transform_cache&.clear
end

.data_to_model(context, data, format, options = {}) ⇒ Object



12
13
14
15
16
# File 'lib/lutaml/model/transform.rb', line 12

def self.data_to_model(context, data, format, options = {})
  register = options[:register] || Lutaml::Model::Config.default_register
  transform = cached_transform(context, register)
  transform.data_to_model(data, format, options)
end

.invalidate_for(context, register = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/lutaml/model/transform.rb', line 43

def self.invalidate_for(context, register = nil)
  return unless @transform_cache

  if register
    @transform_cache.delete([context, register])
  else
    @transform_cache.reject! { |(ctx, _reg)| ctx == context }
  end
end

.model_to_data(context, model, format, options = {}) ⇒ Object



18
19
20
21
22
23
# File 'lib/lutaml/model/transform.rb', line 18

def self.model_to_data(context, model, format, options = {})
  register = model.lutaml_register if model.is_a?(Lutaml::Model::Serialize)
  register ||= Lutaml::Model::Config.default_register
  transform = cached_transform(context, register)
  transform.model_to_data(model, format, options)
end

Instance Method Details

#attributesObject



67
68
69
# File 'lib/lutaml/model/transform.rb', line 67

def attributes
  context.attributes(lutaml_register)
end

#data_to_model(data, options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


75
76
77
78
# File 'lib/lutaml/model/transform.rb', line 75

def data_to_model(data, options = {})
  raise NotImplementedError,
        "#{self.class.name} must implement `data_to_model`."
end

#model_classObject



71
72
73
# File 'lib/lutaml/model/transform.rb', line 71

def model_class
  @context.model
end

#model_to_data(model, options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


80
81
82
83
# File 'lib/lutaml/model/transform.rb', line 80

def model_to_data(model, options = {})
  raise NotImplementedError,
        "#{self.class.name} must implement `model_to_data`."
end