Module: Lutaml::Model::Liquefiable::ClassMethods

Included in:
Serialize::ClassMethods
Defined in:
lib/lutaml/model/liquefiable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#mappingsObject



166
167
168
# File 'lib/lutaml/model/liquefiable.rb', line 166

def mappings
  @mappings ||= {}
end

Instance Method Details

#base_drop_classObject



98
99
100
101
102
# File 'lib/lutaml/model/liquefiable.rb', line 98

def base_drop_class
  const_get(drop_class_name)
rescue StandardError
  nil
end

#custom_liquid_class_nameObject



53
54
55
# File 'lib/lutaml/model/liquefiable.rb', line 53

def custom_liquid_class_name
  @custom_liquid_class_name
end

#drop_classObject



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lutaml/model/liquefiable.rb', line 104

def drop_class
  if custom_liquid_class_name
    begin
      return Object.const_get(custom_liquid_class_name)
    rescue NameError
      raise Lutaml::Model::LiquidClassNotFoundError,
            custom_liquid_class_name
    end
  end

  base_drop_class
end

#drop_class_nameObject



90
91
92
93
94
95
96
# File 'lib/lutaml/model/liquefiable.rb', line 90

def drop_class_name
  @drop_class_name ||= if name
                         "#{name.split('::').last}Drop"
                       else
                         "Drop"
                       end
end

#ensure_liquid_registered!Object

lutaml-model#800: models defined before require "liquid" keep their liquid do ... end mappings; the drop class materializes on first use. Consumers that require Liquid first keep the eager path unchanged.



32
33
34
35
36
37
38
# File 'lib/lutaml/model/liquefiable.rb', line 32

def ensure_liquid_registered!
  if !Object.const_defined?(:Liquid) && !liquid_loadable?
    raise Lutaml::Model::LiquidNotEnabledError
  end

  register_class_if_liquid_defined
end

#generate_mapping_methodsObject



146
147
148
149
150
151
152
153
154
# File 'lib/lutaml/model/liquefiable.rb', line 146

def generate_mapping_methods
  return unless liquid_mappings&.mappings

  liquid_mappings.mappings.each do |key, method_name|
    base_drop_class.define_method(key) do
      liquefy_value(@object.public_send(method_name))
    end
  end
end

#inherited(child) ⇒ Object



14
15
16
17
18
# File 'lib/lutaml/model/liquefiable.rb', line 14

def inherited(child)
  super
  child.register_class_if_liquid_defined
  child.mappings = Utils.deep_dup(mappings) || {}
end

#liquid(&block) ⇒ Object



156
157
158
159
160
# File 'lib/lutaml/model/liquefiable.rb', line 156

def liquid(&block)
  mappings[:liquid] ||= ::Lutaml::Model::Liquid::Mapping.new
  mappings[:liquid].instance_eval(&block) if block
  mappings[:liquid]
end

#liquid_class(class_name) ⇒ Object



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

def liquid_class(class_name)
  @custom_liquid_class_name = class_name
end

#liquid_loadable?Boolean

Load the Liquid gem on first use; a missing gem surfaces as LiquidNotEnabledError at the point a drop is actually needed.

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/lutaml/model/liquefiable.rb', line 42

def liquid_loadable?
  require "liquid"
  true
rescue LoadError
  false
end

#liquid_mappingsObject



162
163
164
# File 'lib/lutaml/model/liquefiable.rb', line 162

def liquid_mappings
  mappings[:liquid]
end

#register_class_if_liquid_definedObject



20
21
22
23
24
25
26
# File 'lib/lutaml/model/liquefiable.rb', line 20

def register_class_if_liquid_defined
  return unless is_a?(Class)
  return unless Object.const_defined?(:Liquid)
  return if base_drop_class

  register_liquid_drop_class
end

#register_drop_method(method_name) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/lutaml/model/liquefiable.rb', line 138

def register_drop_method(method_name)
  return if base_drop_class.method_defined?(method_name)

  base_drop_class.define_method(method_name) do
    liquefy_value(@object.public_send(method_name))
  end
end

#register_liquid_drop_classObject



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
88
# File 'lib/lutaml/model/liquefiable.rb', line 57

def register_liquid_drop_class
  validate_liquid!
  if base_drop_class
    raise Lutaml::Model::LiquidDropAlreadyRegisteredError,
          drop_class_name
  end

  const_set(drop_class_name,
            Class.new(::Liquid::Drop) do
              def initialize(object)
                super()
                @object = object
              end

              def liquefy_value(value)
                if value.is_a?(Array)
                  value.map(&:to_liquid)
                else
                  value.to_liquid
                end
              end

              def liquid_method_missing(method)
                if @object.is_a?(::Lutaml::Model::Liquid::IndexedAccess)
                  result = @object.liquid_fetch(method)
                  result.nil? ? super : liquefy_value(result)
                else
                  super
                end
              end
            end)
end

#register_methodsObject



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/lutaml/model/liquefiable.rb', line 124

def register_methods
  @methods_generated = true

  if self <= Serializable
    raise Lutaml::Model::NoAttributesDefinedLiquidError.new(name) if attributes.empty?

    register_drop_method(:element_order) if method_defined?(:element_order)

    attributes&.each_key { |attr_name| register_drop_method(attr_name) }
  end

  generate_mapping_methods
end

#to_liquid_classObject



117
118
119
120
121
122
# File 'lib/lutaml/model/liquefiable.rb', line 117

def to_liquid_class
  ensure_liquid_registered!
  register_methods unless @methods_generated

  base_drop_class
end

#validate_liquid!Object



170
171
172
173
174
# File 'lib/lutaml/model/liquefiable.rb', line 170

def validate_liquid!
  return if Object.const_defined?(:Liquid)

  raise Lutaml::Model::LiquidNotEnabledError
end