Class: Lutaml::Model::Transform

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

Direct Known Subclasses

KeyValue::Transform, Xml::ModelTransform

Constant Summary collapse

MAX_CACHE_SIZE =

Maximum number of cached Transform instances before eviction.

256

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.



51
52
53
54
# File 'lib/lutaml/model/transform.rb', line 51

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.



49
50
51
# File 'lib/lutaml/model/transform.rb', line 49

def context
  @context
end

#lutaml_registerObject (readonly)

Returns the value of attribute lutaml_register.



49
50
51
# File 'lib/lutaml/model/transform.rb', line 49

def lutaml_register
  @lutaml_register
end

Class Method Details

.cache_sizeObject



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

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

.cached_transform(context, register) ⇒ Object



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

def self.cached_transform(context, register)
  @transform_cache ||= {}
  cache_key = [context.object_id, 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



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

def self.clear_cache!
  @transform_cache&.clear
end

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



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

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

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



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

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

Instance Method Details

#attributesObject



56
57
58
# File 'lib/lutaml/model/transform.rb', line 56

def attributes
  context.attributes(lutaml_register)
end

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

Raises:

  • (NotImplementedError)


64
65
66
67
# File 'lib/lutaml/model/transform.rb', line 64

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

#model_classObject



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

def model_class
  @context.model
end

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

Raises:

  • (NotImplementedError)


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

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