Class: SbrfMerchant::Api::Response::BodyDecorator

Inherits:
Object
  • Object
show all
Defined in:
lib/sbrf_merchant/api/response/body_decorator.rb

Overview

Решение - декоратор для хэша на котором будут вызываться методы в snake_case,

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, to_camel_case = ::SBRF::Utils::String::ToCamelCase.new) ⇒ BodyDecorator



15
16
17
18
# File 'lib/sbrf_merchant/api/response/body_decorator.rb', line 15

def initialize(hash, to_camel_case = ::SBRF::Utils::String::ToCamelCase.new)
  @hash = hash
  @to_camel_case = to_camel_case
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sbrf_merchant/api/response/body_decorator.rb', line 20

def method_missing(meth, *args)
  value = hash[key(meth)]

  # Если значение не найдено то поведение по умолчанию
  return super if value.nil?

  # Если значение - хэш то возвращаем его, предварительно обернув в декоратор
  return self.class.new(value) if value.is_a?(Hash)

  # В остальных случаях возвращаем самое значение
  value
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



13
14
15
# File 'lib/sbrf_merchant/api/response/body_decorator.rb', line 13

def hash
  @hash
end

#to_camel_caseObject (readonly)

Returns the value of attribute to_camel_case.



13
14
15
# File 'lib/sbrf_merchant/api/response/body_decorator.rb', line 13

def to_camel_case
  @to_camel_case
end

Instance Method Details

#respond_to?(meth) ⇒ Boolean



33
34
35
# File 'lib/sbrf_merchant/api/response/body_decorator.rb', line 33

def respond_to?(meth)
  hash.key?(key(meth))
end

#respond_to_missing?(meth) ⇒ Boolean



37
38
39
# File 'lib/sbrf_merchant/api/response/body_decorator.rb', line 37

def respond_to_missing?(meth)
  hash.key?(key(meth))
end