Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeTranslator optimization #1944

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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