Class: Grape::Router::AttributeTranslator
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- Grape::Router::AttributeTranslator
- Defined in:
- lib/grape/router/attribute_translator.rb
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ AttributeTranslator
constructor
A new instance of AttributeTranslator.
- #to_h ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ AttributeTranslator
Returns a new instance of AttributeTranslator.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/grape/router/attribute_translator.rb', line 15 def initialize(attributes = {}) ostruct = OpenStruct.new(attributes) super ostruct @attributes = attributes self.class.supported_attributes.each do |name| ostruct.send(:"#{name}=", nil) unless ostruct.respond_to?(name) self.class.instance_eval do define_method(name) { instance_variable_get(:"@#{name}") } end if name == :format end end |
Class Method Details
.register(*attributes) ⇒ Object
7 8 9 |
# File 'lib/grape/router/attribute_translator.rb', line 7 def self.register(*attributes) AttributeTranslator.supported_attributes.concat(attributes) end |
.supported_attributes ⇒ Object
11 12 13 |
# File 'lib/grape/router/attribute_translator.rb', line 11 def self.supported_attributes @supported_attributes ||= [] end |
Instance Method Details
#to_h ⇒ Object
27 28 29 30 31 |
# File 'lib/grape/router/attribute_translator.rb', line 27 def to_h @attributes.each_with_object({}) do |(key, _), attributes| attributes[key.to_sym] = send(:"#{key}") end end |