Sha256: 5f292310316d6c5208af494c9380991b92103b3a35fe1893e116df6d6ac5175c

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

module Lutaml
  module Model
    class MappingHash < Hash
      attr_accessor :ordered

      def initialize
        @ordered = false
        @item_order = []

        super
      end

      def item_order
        @item_order&.map { |key| normalize(key) } || keys
      end

      def fetch(key)
        self[key.to_s] || self[key.to_sym]
      end

      def key_exist?(key)
        key?(key.to_s) || key?(key.to_sym)
      end

      def item_order=(order)
        raise "`item order` must be an array" unless order.is_a?(Array)

        @item_order = order
      end

      def ordered?
        @ordered
      end

      def method_missing(method_name, *args)
        value = self[method_name] || self[method_name.to_s]
        return value if value

        super
      end

      def respond_to_missing?(method_name, include_private = false)
        key_present = key?(method_name) || key?(method_name.to_s)
        return true if key_present

        super
      end

      private

      def normalize(key)
        if self[key.to_s]
          key.to_s
        elsif self[key.to_sym]
          key.to_sym
        else
          key
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lutaml-model-0.3.17 lib/lutaml/model/mapping_hash.rb
lutaml-model-0.3.16 lib/lutaml/model/mapping_hash.rb
lutaml-model-0.3.15 lib/lutaml/model/mapping_hash.rb
lutaml-model-0.3.14 lib/lutaml/model/mapping_hash.rb