Sha256: 866119a1b5f9633f3dcef5fb443bcfc1905aa3683a6957398315c6df4cd0f5d2

Contents?: true

Size: 893 Bytes

Versions: 1

Compression:

Stored size: 893 Bytes

Contents

module Depth
  class RouteElement
    attr_reader :key_or_index, :type
    alias_method :key, :key_or_index
    alias_method :index, :key_or_index
    def initialize(key_or_index, type: :hash)
      @key_or_index = key_or_index
      @type = type.to_sym
    end

    def create
      { hash: {}, array: [], leaf: nil }.fetch(type, nil)
    end

    class << self
      def convert_route(route_array)
        Array(route_array).map { |el| convert(el) }
      end

      def convert(el)
        return el if el.is_a?(RouteElement)
        case el
        when Array
          type = el.count > 1 ? el[1] : :hash
          RouteElement.new(route_el[0], type: type)
        when Hash
          key_or_index = el.fetch(:key, el.fetch(:index))
          RouteElement.new(key_or_index, type: el.fetch(:type, :hash))
        else
          RouteElement.new(el)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
depth-0.0.1 lib/depth/route_element.rb