class GrapePathHelpers::DecoratedRoute
wrapper around Grape::Route that adds a helper method
Attributes
extension[R]
helper_arguments[R]
helper_names[R]
route[R]
route_options[R]
Public Class Methods
new(route)
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 11 def initialize(route) @route = route @route_options = route.options @helper_names = [] @helper_arguments = required_helper_segments @extension = default_extension define_path_helpers end
sanitize_method_name(string)
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 7 def self.sanitize_method_name(string) string.gsub(/\W|^[0-9]/, '_') end
Public Instance Methods
default_extension()
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 20 def default_extension pattern = /\((\.\:?\w+)\)$/ match = route_path.match(pattern) return '' unless match ext = match.captures.first if ext == '.:format' '' else ext end end
define_path_helper(method_name, route_attributes)
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 41 def define_path_helper(method_name, route_attributes) method_body = <<-RUBY def #{method_name}(attributes = {}) attrs = #{route_attributes}.merge(attributes) query_params = attrs.delete(:params) content_type = attrs.delete(:format) path = '/' + path_segments_with_values(attrs).join('/') path + content_type + query_string(query_params) end RUBY instance_eval method_body end
define_path_helpers()
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 32 def define_path_helpers route_versions.each do |version| route_attributes = { version: version, format: extension } method_name = path_helper_name(route_attributes) @helper_names << method_name define_path_helper(method_name, route_attributes) end end
dynamic_path_segments()
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 112 def dynamic_path_segments segments = path_segments.select do |segment| dynamic_segment?(segment) end segments.map { |s| s.slice(1..-1) } end
dynamic_segment?(segment)
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 119 def dynamic_segment?(segment) segment.start_with?(':') end
path_helper_name(opts = {})
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 75 def path_helper_name(opts = {}) if route_options[:as] name = route_options[:as].to_s else segments = path_segments_with_values(opts) name = if segments.empty? 'root' else segments.join('_') end end sanitized_name = self.class.sanitize_method_name(name) sanitized_name + '_path' end
path_segments()
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 107 def path_segments pattern = %r{\(/?\.:?\w+\)|/|\??\*} route_path.split(pattern).reject(&:blank?) end
path_segments_with_values(opts)
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 102 def path_segments_with_values(opts) segments = path_segments.map { |s| segment_to_value(s, opts) } segments.reject(&:blank?) end
query_string(params)
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 56 def query_string(params) if params.nil? '' else '?' + params.to_param end end
required_helper_segments()
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 123 def required_helper_segments segments_in_options = dynamic_path_segments.select do |segment| route.options[segment.to_sym] end dynamic_path_segments - segments_in_options end
route_method()
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 156 def route_method route.request_method end
route_namespace()
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 152 def route_namespace route.namespace end
route_path()
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 144 def route_path route.path end
route_version()
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 148 def route_version route.version end
route_versions()
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 64 def route_versions return [nil] if route_version.nil? || route_version.empty? if route_version.is_a?(String) version_pattern = /[^\[",\]\s]+/ route_version.scan(version_pattern) else route_version end end
segment_to_value(segment, opts = {})
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 92 def segment_to_value(segment, opts = {}) if dynamic_segment?(segment) options = route.options.merge(stringify_keys(opts)) key = segment.slice(1..-1).to_sym options[key] else segment end end
special_keys()
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 130 def special_keys %w[format params] end
uses_segments_in_path_helper?(segments)
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 134 def uses_segments_in_path_helper?(segments) segments = segments.reject { |x| special_keys.include?(x) } if required_helper_segments.empty? && segments.any? false else required_helper_segments.all? { |x| segments.include?(x) } end end
Private Instance Methods
stringify_keys(original)
click to toggle source
# File lib/grape-path-helpers/decorated_route.rb, line 162 def stringify_keys(original) original.each_with_object({}) do |(key, value), hash| hash[key.to_sym] = value end end