Skip to content

Commit

Permalink
AttributeTranslator optimization (#1944)
Browse files Browse the repository at this point in the history
Added request_method and requirements has methods instead using method_missing since it's defined by defaults. Added regexp and index in Any class instead of falling back on method_missing
  • Loading branch information
ericproulx authored and dblock committed Dec 15, 2019
1 parent 1d35559 commit afcca35
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Features

* Your contribution here.
* [#1944](https://github.com/ruby-grape/grape/pull/1944): Reduced attribute_translator string allocations - [@ericproulx](https://github.com/ericproulx).
* [#1942](https://github.com/ruby-grape/grape/pull/1942): Optimized retained memory methods - [@ericproulx](https://github.com/ericproulx).
* [#1941](https://github.com/ruby-grape/grape/pull/1941): Frozen string literal - [@ericproulx](https://github.com/ericproulx).
* [#1940](https://github.com/ruby-grape/grape/pull/1940): Get rid of a needless step in HashWithIndifferentAccess - [@dnesteryuk](https://github.com/dnesteryuk).
Expand Down
7 changes: 5 additions & 2 deletions lib/grape/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ class Router
attr_reader :map, :compiled

class Any < AttributeTranslator
def initialize(pattern, **attributes)
attr_reader :pattern, :regexp, :index
def initialize(pattern, regexp, index, **attributes)
@pattern = pattern
@regexp = regexp
@index = index
super(attributes)
end
end
Expand Down Expand Up @@ -51,7 +54,7 @@ def append(route)

def associate_routes(pattern, **options)
regexp = /(?<_#{@neutral_map.length}>)#{pattern.to_regexp}/
@neutral_map << Any.new(pattern, regexp: regexp, index: @neutral_map.length, **options)
@neutral_map << Any.new(pattern, regexp, @neutral_map.length, **options)
end

def call(env)
Expand Down
24 changes: 16 additions & 8 deletions lib/grape/router/attribute_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,39 @@ module Grape
class Router
# this could be an OpenStruct, but doesn't work in Ruby 2.3.0, see https://bugs.ruby-lang.org/issues/12251
class AttributeTranslator
attr_reader :attributes, :request_method, :requirements

def initialize(attributes = {})
@attributes = attributes
@request_method = attributes[:request_method]
@requirements = attributes[:requirements]
end

def to_h
@attributes
attributes
end

def method_missing(m, *args)
if m[-1] == '='
@attributes[m[0..-1]] = *args
elsif m[-1] != '='
@attributes[m]
def method_missing(method_name, *args) # rubocop:disable Style/MethodMissing
if setter?(method_name[-1])
attributes[method_name[0..-1]] = *args
else
super
attributes[method_name]
end
end

def respond_to_missing?(method_name, _include_private = false)
if method_name[-1] == '='
if setter?(method_name[-1])
true
else
@attributes.key?(method_name)
end
end

private

def setter?(method_name)
method_name[-1] == '='
end
end
end
end

0 comments on commit afcca35

Please sign in to comment.