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 bugs.ruby-lang.org/issues/12251

Constant Summary collapse

ROUTE_ATTRIBUTES =
%i[
  prefix
  version
  settings
  format
  description
  http_codes
  headers
  entity
  details
  requirements
  request_method
  namespace
].freeze
ROUTER_ATTRIBUTES =
%i[pattern index].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ AttributeTranslator

Returns a new instance of AttributeTranslator.


26
27
28
# File 'lib/grape/router/attribute_translator.rb', line 26

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


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

def method_missing(method_name, *args)
  if setter?(method_name[-1])
    attributes[method_name[0..-1]] = *args
  else
    attributes[method_name]
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes


7
8
9
# File 'lib/grape/router/attribute_translator.rb', line 7

def attributes
  @attributes
end

#request_methodObject (readonly)

Returns the value of attribute request_method


7
8
9
# File 'lib/grape/router/attribute_translator.rb', line 7

def request_method
  @request_method
end

#requirementsObject (readonly)

Returns the value of attribute requirements


7
8
9
# File 'lib/grape/router/attribute_translator.rb', line 7

def requirements
  @requirements
end

Instance Method Details

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

Returns:

  • (Boolean)

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

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

#to_hObject


36
37
38
# File 'lib/grape/router/attribute_translator.rb', line 36

def to_h
  attributes
end