Sha256: 665653278e7aef872658b03ff1a3e0e23415cdb6df8873e4df66b42a2d4471c6

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

module Lutaml
  module Model
    class MappingRule
      attr_reader :name,
                  :to,
                  :render_nil,
                  :custom_methods,
                  :delegate

      def initialize(
        name,
        to:,
        render_nil: false,
        with: {},
        attribute: false,
        delegate: nil
      )
        @name = name
        @to = to
        @render_nil = render_nil
        @custom_methods = with
        @attribute = attribute
        @delegate = delegate
      end

      alias from name
      alias render_nil? render_nil

      def serialize_attribute(model, element, doc)
        if custom_methods[:to]
          model.send(custom_methods[:to], model, element, doc)
        end
      end

      def to_value_for(model)
        if delegate
          model.public_send(delegate).public_send(to)
        else
          model.public_send(to)
        end
      end

      def serialize(model, parent = nil, doc = nil)
        if custom_methods[:to]
          model.send(custom_methods[:to], model, parent, doc)
        else
          to_value_for(model)
        end
      end

      def deserialize(model, value, attributes, mapper_class = nil)
        if custom_methods[:from]
          mapper_class.new.send(custom_methods[:from], model, value)
        elsif delegate
          if model.public_send(delegate).nil?
            model.public_send(:"#{delegate}=", attributes[delegate].type.new)
          end

          model.public_send(delegate).public_send(:"#{to}=", value)
        else
          model.public_send(:"#{to}=", value)
        end
      end

      def deep_dup
        raise NotImplementedError, "Subclasses must implement `deep_dup`."
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lutaml-model-0.3.14 lib/lutaml/model/mapping_rule.rb