1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
# frozen_string_literal: true
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
ROUTE_ATTRIBUTES = %i[
prefix
version
settings
format
description
http_codes
headers
entity
details
requirements
request_method
namespace
].freeze
ROUTER_ATTRIBUTES = %i[pattern index].freeze
def initialize(**attributes)
@attributes = attributes
end
(ROUTER_ATTRIBUTES + ROUTE_ATTRIBUTES).each do |attr|
define_method attr do
attributes[attr]
end
end
def to_h
attributes
end
def method_missing(method_name, *args)
if setter?(method_name[-1])
attributes[method_name[0..-1]] = *args
else
attributes[method_name]
end
end
def respond_to_missing?(method_name, _include_private = false)
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
|