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

Direct Known Subclasses

Any

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ AttributeTranslator

Returns a new instance of AttributeTranslator


5
6
7
# File 'lib/grape/router/attribute_translator.rb', line 5

def initialize(attributes = {})
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object


13
14
15
16
17
18
19
# File 'lib/grape/router/attribute_translator.rb', line 13

def method_missing(m, *args)
  if m[-1] == '='
    @attributes[m[0..-1]] = *args
  elsif m[-1] != '='
    @attributes[m]
  end
end

Instance Method Details

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

Returns:

  • (Boolean)

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

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

#to_hObject


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

def to_h
  @attributes
end