Class: GrapePathHelpers::DecoratedRoute
- Inherits:
-
Object
- Object
- GrapePathHelpers::DecoratedRoute
- Defined in:
- lib/grape-path-helpers/decorated_route.rb
Overview
wrapper around Grape::Route that adds a helper method
Constant Summary collapse
- PATH_SEGMENTS_REGEXP =
%r{\(/?\.:?\w+\)|/(?!\))|(?<=\))|\??\*}
- PATH_SEGMENTS_WITH_WILDCARDS_REGEXP =
%r{\(/?\.:?\w+\)|/(?!\))|(?<=\))|\?}
Instance Attribute Summary collapse
-
#extension ⇒ Object
readonly
Returns the value of attribute extension.
-
#helper_arguments ⇒ Object
readonly
Returns the value of attribute helper_arguments.
-
#helper_names ⇒ Object
readonly
Returns the value of attribute helper_names.
-
#route ⇒ Object
readonly
Returns the value of attribute route.
-
#route_options ⇒ Object
readonly
Returns the value of attribute route_options.
Class Method Summary collapse
Instance Method Summary collapse
- #default_extension ⇒ Object
- #define_path_helper(method_name, route_attributes) ⇒ Object
- #define_path_helpers ⇒ Object
- #dynamic_path_segments ⇒ Object
- #dynamic_segment?(segment) ⇒ Boolean
-
#initialize(route) ⇒ DecoratedRoute
constructor
A new instance of DecoratedRoute.
- #optional_segment?(segment) ⇒ Boolean
- #path_helper_name(opts = {}) ⇒ Object
- #path_segments(include_wildcard_segments = false) ⇒ Object
- #path_segments_with_values(opts, include_wildcard_segments = false) ⇒ Object
- #query_string(params) ⇒ Object
- #required_helper_segments ⇒ Object
- #route_method ⇒ Object
- #route_namespace ⇒ Object
- #route_path ⇒ Object
- #route_version ⇒ Object
- #route_versions ⇒ Object
- #segment_to_value(segment, opts = {}) ⇒ Object
- #special_keys ⇒ Object
- #uses_segments_in_path_helper?(segments) ⇒ Boolean
Constructor Details
#initialize(route) ⇒ DecoratedRoute
Returns a new instance of DecoratedRoute.
14 15 16 17 18 19 20 21 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 14 def initialize(route) @route = route @route_options = route. @helper_names = [] @helper_arguments = required_helper_segments @extension = default_extension define_path_helpers end |
Instance Attribute Details
#extension ⇒ Object (readonly)
Returns the value of attribute extension.
4 5 6 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 4 def extension @extension end |
#helper_arguments ⇒ Object (readonly)
Returns the value of attribute helper_arguments.
4 5 6 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 4 def helper_arguments @helper_arguments end |
#helper_names ⇒ Object (readonly)
Returns the value of attribute helper_names.
4 5 6 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 4 def helper_names @helper_names end |
#route ⇒ Object (readonly)
Returns the value of attribute route.
4 5 6 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 4 def route @route end |
#route_options ⇒ Object (readonly)
Returns the value of attribute route_options.
4 5 6 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 4 def @route_options end |
Class Method Details
.sanitize_method_name(string) ⇒ Object
10 11 12 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 10 def self.sanitize_method_name(string) string.gsub(/\W|^[0-9]/, '_') end |
Instance Method Details
#default_extension ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 23 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) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 44 def define_path_helper(method_name, route_attributes) method_body = <<-RUBY def #{method_name}(attributes = {}, include_wildcard_segments = false) attrs = #{route_attributes}.merge(attributes) query_params = attrs.delete(:params) content_type = attrs.delete(:format) path = '/' + path_segments_with_values(attrs, include_wildcard_segments).join('/') path + content_type + query_string(query_params) end RUBY instance_eval method_body end |
#define_path_helpers ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 35 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 ⇒ Object
121 122 123 124 125 126 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 121 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) ⇒ Boolean
128 129 130 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 128 def dynamic_segment?(segment) segment.start_with?(':', '*') end |
#optional_segment?(segment) ⇒ Boolean
132 133 134 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 132 def optional_segment?(segment) segment.start_with?('(') end |
#path_helper_name(opts = {}) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 78 def path_helper_name(opts = {}) if [:as] name = [: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(include_wildcard_segments = false) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 112 def path_segments(include_wildcard_segments = false) pattern = if include_wildcard_segments PATH_SEGMENTS_WITH_WILDCARDS_REGEXP else PATH_SEGMENTS_REGEXP end route_path.split(pattern).reject(&:blank?) end |
#path_segments_with_values(opts, include_wildcard_segments = false) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 105 def path_segments_with_values(opts, include_wildcard_segments = false) segments = path_segments(include_wildcard_segments).map do |s| segment_to_value(s, opts) end segments.reject(&:blank?) end |
#query_string(params) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 59 def query_string(params) if params.nil? '' else '?' + params.to_param end end |
#required_helper_segments ⇒ Object
136 137 138 139 140 141 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 136 def required_helper_segments = dynamic_path_segments.select do |segment| route.[segment.to_sym] end dynamic_path_segments - end |
#route_method ⇒ Object
169 170 171 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 169 def route_method route.request_method end |
#route_namespace ⇒ Object
165 166 167 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 165 def route_namespace route.namespace end |
#route_path ⇒ Object
157 158 159 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 157 def route_path route.path end |
#route_version ⇒ Object
161 162 163 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 161 def route_version route.version end |
#route_versions ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 67 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 = {}) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 95 def segment_to_value(segment, opts = {}) if dynamic_segment?(segment) = route..merge(stringify_keys(opts)) key = segment.slice(1..-1).to_sym [key] else segment end end |
#special_keys ⇒ Object
143 144 145 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 143 def special_keys %w[format params] end |
#uses_segments_in_path_helper?(segments) ⇒ Boolean
147 148 149 150 151 152 153 154 155 |
# File 'lib/grape-path-helpers/decorated_route.rb', line 147 def uses_segments_in_path_helper?(segments) segments = segments.reject { |x| special_keys.include?(x) } if helper_arguments.empty? && segments.any? false else helper_arguments.all? { |x| segments.include?(x) } end end |