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

Direct Known Subclasses

Any

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ AttributeTranslator

Returns a new instance of AttributeTranslator.



9
10
11
12
13
# File 'lib/grape/router/attribute_translator.rb', line 9

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

rubocop:disable Style/MethodMissing



19
20
21
22
23
24
25
# File 'lib/grape/router/attribute_translator.rb', line 19

def method_missing(method_name, *args) # rubocop:disable Style/MethodMissing
  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)


27
28
29
30
31
32
33
# File 'lib/grape/router/attribute_translator.rb', line 27

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

#to_hObject



15
16
17
# File 'lib/grape/router/attribute_translator.rb', line 15

def to_h
  attributes
end