Sha256: 59078be31e9b9597415569ece866c1f66623d3e8225ef4244056fb00b6c513ce

Contents?: true

Size: 890 Bytes

Versions: 7

Compression:

Stored size: 890 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(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

7 entries across 7 versions & 1 rubygems

Version Path
depth-0.4.3 lib/depth/route_element.rb
depth-0.4.2 lib/depth/route_element.rb
depth-0.4.1 lib/depth/route_element.rb
depth-0.4.0 lib/depth/route_element.rb
depth-0.3.0 lib/depth/route_element.rb
depth-0.1.0 lib/depth/route_element.rb
depth-0.0.2 lib/depth/route_element.rb