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



140
141
142
# File 'lib/lutaml/model/liquefiable.rb', line 140

def mappings
  @mappings ||= {}
end

Instance Method Details

#base_drop_classObject



59
60
61
62
63
# File 'lib/lutaml/model/liquefiable.rb', line 59

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



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/lutaml/model/liquefiable.rb', line 65

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



51
52
53
54
55
56
57
# File 'lib/lutaml/model/liquefiable.rb', line 51

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

#generate_mapping_methodsObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/lutaml/model/liquefiable.rb', line 112

def generate_mapping_methods
  return unless liquid_mappings&.mappings

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

      if value.is_a?(Array)
        value.map(&:to_liquid)
      else
        value.to_liquid
      end
    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



128
129
130
131
132
133
134
# File 'lib/lutaml/model/liquefiable.rb', line 128

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



136
137
138
# File 'lib/lutaml/model/liquefiable.rb', line 136

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



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/lutaml/model/liquefiable.rb', line 98

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

  base_drop_class.define_method(method_name) do
    value = @object.public_send(method_name)

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

#register_liquid_drop_classObject



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

def register_liquid_drop_class
  validate_liquid!
  if base_drop_class
    raise "#{drop_class_name} Already exists!"
  end

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

#register_methodsObject



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

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



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

def to_liquid_class
  register_methods unless @methods_generated

  base_drop_class
end

#validate_liquid!Object



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

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

  raise Lutaml::Model::LiquidNotEnabledError
end