Sha256: 1b4b9f543070655bb54faa39813b3e73a8d283e2ecbeb9558c1b74cd6b3ffb22

Contents?: true

Size: 1.9 KB

Versions: 14

Compression:

Stored size: 1.9 KB

Contents

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

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

      alias from name
      alias render_nil? render_nil
      alias render_default? render_default
      alias attribute? attribute

      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

14 entries across 14 versions & 1 rubygems

Version Path
lutaml-model-0.3.28 lib/lutaml/model/mapping_rule.rb
lutaml-model-0.3.27 lib/lutaml/model/mapping_rule.rb
lutaml-model-0.3.26 lib/lutaml/model/mapping_rule.rb
lutaml-model-0.3.25 lib/lutaml/model/mapping_rule.rb
lutaml-model-0.3.24 lib/lutaml/model/mapping_rule.rb
lutaml-model-0.3.23 lib/lutaml/model/mapping_rule.rb
lutaml-model-0.3.22 lib/lutaml/model/mapping_rule.rb
lutaml-model-0.3.21 lib/lutaml/model/mapping_rule.rb
lutaml-model-0.3.20 lib/lutaml/model/mapping_rule.rb
lutaml-model-0.3.19 lib/lutaml/model/mapping_rule.rb
lutaml-model-0.3.18 lib/lutaml/model/mapping_rule.rb
lutaml-model-0.3.17 lib/lutaml/model/mapping_rule.rb
lutaml-model-0.3.16 lib/lutaml/model/mapping_rule.rb
lutaml-model-0.3.15 lib/lutaml/model/mapping_rule.rb