Class: Monobank::Resources::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/monobank/resources/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Base

Returns a new instance of Base.



10
11
12
# File 'lib/monobank/resources/base.rb', line 10

def initialize(attributes)
  @attributes = deep_snake_case(attributes)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



32
33
34
# File 'lib/monobank/resources/base.rb', line 32

def attributes
  @attributes
end

Class Method Details

.define_fields(attributes) ⇒ Object



4
5
6
7
8
# File 'lib/monobank/resources/base.rb', line 4

def self.define_fields(attributes)
  attributes.each do |attribute|
    define_method(attribute) { instance_variable_get("@attributes")[attribute] }
  end
end

Instance Method Details

#deep_snake_case(object) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/monobank/resources/base.rb', line 18

def deep_snake_case(object)
  if object.is_a?(Hash)
    object.map do |key, value|
      [method_name(key), deep_snake_case(value)]
    end.to_h
  elsif object.is_a?(Array)
    object.map do |value|
      deep_snake_case(value)
    end
  else
    object
  end
end

#method_name(key) ⇒ Object



14
15
16
# File 'lib/monobank/resources/base.rb', line 14

def method_name(key)
  key.gsub(/(.)([A-Z])/,'\1_\2').downcase
end