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



146
147
148
# File 'lib/lutaml/model/liquefiable.rb', line 146

def mappings
  @mappings ||= {}
end

Instance Method Details

#base_drop_classObject



77
78
79
80
81
# File 'lib/lutaml/model/liquefiable.rb', line 77

def base_drop_class
  const_get(drop_class_name)
rescue StandardError
  nil
end

#custom_liquid_class_nameObject



32
33
34
# File 'lib/lutaml/model/liquefiable.rb', line 32

def custom_liquid_class_name
  @custom_liquid_class_name
end

#drop_classObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/lutaml/model/liquefiable.rb', line 83

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



69
70
71
72
73
74
75
# File 'lib/lutaml/model/liquefiable.rb', line 69

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

#generate_mapping_methodsObject



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

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



134
135
136
137
138
139
140
# File 'lib/lutaml/model/liquefiable.rb', line 134

def liquid(&block)
  return unless Object.const_defined?(:Liquid)

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

#liquid_class(class_name) ⇒ Object



28
29
30
# File 'lib/lutaml/model/liquefiable.rb', line 28

def liquid_class(class_name)
  @custom_liquid_class_name = class_name
end

#liquid_mappingsObject



142
143
144
# File 'lib/lutaml/model/liquefiable.rb', line 142

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



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

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



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

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



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

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



96
97
98
99
100
# File 'lib/lutaml/model/liquefiable.rb', line 96

def to_liquid_class
  register_methods unless @methods_generated

  base_drop_class
end

#validate_liquid!Object



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

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

  raise Lutaml::Model::LiquidNotEnabledError
end