Sha256: d87a6a127b7bd47832922c6ac3451de06154a0b83256e324caa0c2d00dad9d02

Contents?: true

Size: 1.44 KB

Versions: 17

Compression:

Stored size: 1.44 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 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_map.keys 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)
        (dynamic_required_keys - keys).size.zero?
      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.any?{|p| p.is_a?(Variable)}
      end
      
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
joshbuddy-usher-0.5.2 lib/usher/route/path.rb
joshbuddy-usher-0.5.3 lib/usher/route/path.rb
joshbuddy-usher-0.5.4 lib/usher/route/path.rb
joshbuddy-usher-0.5.6 lib/usher/route/path.rb
joshbuddy-usher-0.5.7 lib/usher/route/path.rb
usher-0.5.13 lib/usher/route/path.rb
usher-0.5.12 lib/usher/route/path.rb
usher-0.5.11 lib/usher/route/path.rb
usher-0.5.10 lib/usher/route/path.rb
usher-0.5.8 lib/usher/route/path.rb
usher-0.5.7 lib/usher/route/path.rb
usher-0.5.6 lib/usher/route/path.rb
usher-0.5.5 lib/usher/route/path.rb
usher-0.5.4 lib/usher/route/path.rb
usher-0.5.3 lib/usher/route/path.rb
usher-0.5.2 lib/usher/route/path.rb
usher-0.5.1 lib/usher/route/path.rb