Module: WrapAttributes

Defined in:
lib/wrap_attributes.rb,
lib/wrap_attributes/railtie.rb,
lib/wrap_attributes/version.rb,
lib/wrap_attributes/converter.rb,
lib/wrap_attributes/attributes_wrapper.rb

Defined Under Namespace

Modules: AttributesWrapper Classes: Railtie

Constant Summary collapse

VERSION =
ENV['VERSION']&.match(/^refs\/tags\/v(\d\.\d\.\d)$/)&.[](0) || '0.1.2'

Class Method Summary collapse

Class Method Details

.convert(parameters, attributes) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wrap_attributes/converter.rb', line 2

def self.convert(parameters, attributes)
  if parameters.is_a?(Symbol) || parameters.is_a?(String)
    parameters
  elsif parameters.is_a?(Hash)
    if attributes.is_a?(Symbol) || attributes.is_a?(String)
      parameters.map { |key, value| key.to_sym == attributes ? [attribute_key(key), value] : [key, value] }.to_h
    elsif attributes.is_a?(Hash)
      parameters.map do |key, params|
        attr = attributes&.[](key.to_sym)
        attr.present? ? [attribute_key(key), convert(params, attr)] : [key, params]
      end.to_h
    elsif attributes.is_a?(Array)
      parameters.map do |key, params|
        attributes.reduce([key, params]) do |pre, cur|
          if cur.is_a?(Symbol) && cur == key.to_sym
            [attribute_key(key), params]
          elsif cur.is_a?(Hash) && cur.keys.include?(key.to_sym)
            [attribute_key(key), convert(params, cur[key.to_sym])]
          else
            pre
          end
        end
      end.to_h
    end
  elsif parameters.is_a?(Array)
    parameters.map do |params|
      convert(params, attributes)
    end
  end
end