Class: Grape::Router::AttributeTranslator

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/router/attribute_translator.rb

Overview

this could be an OpenStruct, but doesn't work in Ruby 2.3.0, see https://bugs.ruby-lang.org/issues/12251 fixed >= 3.0

Constant Summary collapse

ROUTE_ATTRIBUTES =
(%i[
  allow_header
  anchor
  endpoint
  format
  forward_match
  namespace
  not_allowed_method
  prefix
  request_method
  requirements
  settings
  suffix
  version
] | Grape::DSL::Desc::ROUTE_ATTRIBUTES).freeze

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ AttributeTranslator

Returns a new instance of AttributeTranslator.



24
25
26
# File 'lib/grape/router/attribute_translator.rb', line 24

def initialize(**attributes)
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/grape/router/attribute_translator.rb', line 42

def method_missing(method_name, *args)
  if setter?(method_name)
    @attributes[method_name.to_s.chomp('=').to_sym] = args.first
  else
    @attributes[method_name]
  end
end

Instance Method Details

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/grape/router/attribute_translator.rb', line 50

def respond_to_missing?(method_name, _include_private = false)
  return true if setter?(method_name)

  @attributes.key?(method_name)
end

#to_hObject



38
39
40
# File 'lib/grape/router/attribute_translator.rb', line 38

def to_h
  @attributes
end