Sha256: 51398d4d9e3cfbd8a404768af8a93341ca1d0fdaad74ba511b0dcbcb0d28bc25

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

class Usher
  class Route
    class Path

      attr_accessor :route
      attr_reader :parts

      def initialize(route, parts)
        self.route = route
        self.parts = parts
      end

      def convert_params_array(ary)
        ary.empty? ? ary : dynamic_keys.zip(ary)
      end

      def dynamic_indicies
        unless dynamic? && @dynamic_indicies
          @dynamic_indicies = []
          parts.each_index{|i| @dynamic_indicies << i if parts[i].is_a?(Variable)}
        end
        @dynamic_indicies
      end

      def dynamic_parts
        @dynamic_parts ||= parts.values_at(*dynamic_indicies) if dynamic?
      end

      def dynamic_map
        unless dynamic? && @dynamic_map
          @dynamic_map = {}
          dynamic_parts.each{|p| @dynamic_map[p.name] = p }
        end
        @dynamic_map
      end

      def dynamic_keys
        @dynamic_keys ||= dynamic_parts.map{|dp| dp.name} if dynamic?
      end

      def dynamic_required_keys
        @dynamic_required_keys ||= dynamic_parts.select{|dp| !dp.default_value}.map{|dp| dp.name} if dynamic?
      end

      def dynamic?
        @dynamic
      end

      def can_generate_from_keys?(keys)
        if dynamic?
          (dynamic_required_keys - keys).size.zero? ? keys : nil
        end
      end

      def can_generate_from_params?(params)
        if route.router.consider_destination_keys?
          (route.destination.to_a - params.to_a).size.zero?
        end
      end

      # Merges paths for use in generation
      def merge(other_path)
        new_parts = parts + other_path.parts
        Path.new(route, new_parts)
      end

      private
      def parts=(parts)
        @parts = parts
        @dynamic = @parts && @parts.any?{|p| p.is_a?(Variable)}
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
usher-0.7.2 lib/usher/route/path.rb
usher-0.7.1 lib/usher/route/path.rb
usher-0.7.0 lib/usher/route/path.rb