Class: Dotcrypt::Flattener

Inherits:
Object
  • Object
show all
Defined in:
lib/dotcrypt/flattener.rb

Constant Summary collapse

SCALARS =
[String, Numeric, NilClass].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, prefixes: [], accumulator: {}, separator: "_") ⇒ Flattener

Returns a new instance of Flattener.



8
9
10
11
12
13
# File 'lib/dotcrypt/flattener.rb', line 8

def initialize(config, prefixes: [], accumulator: {}, separator: "_")
  @config = config
  @prefixes = prefixes
  @accumulator = accumulator
  @separator = separator
end

Class Method Details

.callObject



6
# File 'lib/dotcrypt/flattener.rb', line 6

def self.call(...) = new(...).call

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/dotcrypt/flattener.rb', line 15

def call
  case config
  when *SCALARS then save(@config)
  when Hash then config.each { |k, v| self.class.call(v, prefixes: prefixes + [k], accumulator:, separator:) }
  when Array then flatten_array
  else raise TypeError, "unknown type: #{config.class}"
  end

  accumulator
end