Sha256: b842d114fa2d170a310529cd85c15e8c67d4025557cb5b584d317914e1ce3341

Contents?: true

Size: 1.35 KB

Versions: 13

Compression:

Stored size: 1.35 KB

Contents

require_relative "key_value_mapping_rule"

module Lutaml
  module Model
    class KeyValueMapping
      attr_reader :mappings

      def initialize
        @mappings = []
      end

      def map(
        name,
        to: nil,
        render_nil: false,
        render_default: false,
        with: {},
        delegate: nil,
        child_mappings: nil
      )
        validate!(name, to, with)

        @mappings << KeyValueMappingRule.new(
          name,
          to: to,
          render_nil: render_nil,
          render_default: render_default,
          with: with,
          delegate: delegate,
          child_mappings: child_mappings,
        )
      end

      alias map_element map

      def validate!(key, to, with)
        if to.nil? && with.empty?
          msg = ":to or :with argument is required for mapping '#{key}'"
          raise IncorrectMappingArgumentsError.new(msg)
        end

        if !with.empty? && (with[:from].nil? || with[:to].nil?)
          msg = ":with argument for mapping '#{key}' requires :to and :from keys"
          raise IncorrectMappingArgumentsError.new(msg)
        end
      end

      def deep_dup
        self.class.new.tap do |new_mapping|
          new_mapping.instance_variable_set(:@mappings, duplicate_mappings)
        end
      end

      def duplicate_mappings
        @mappings.map(&:deep_dup)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

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