Class: DeepCount::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/deep_count/adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Adapter

Returns a new instance of Adapter.



5
6
7
8
# File 'lib/deep_count/adapter.rb', line 5

def initialize(input)
  @input = input
  @output = {}
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



3
4
5
# File 'lib/deep_count/adapter.rb', line 3

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



3
4
5
# File 'lib/deep_count/adapter.rb', line 3

def output
  @output
end

Instance Method Details

#add_to(object, array, value) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/deep_count/adapter.rb', line 23

def add_to(object, array, value)
  key = array.shift
  if array.any?
    add_to object[key] ||= {}, array, value
  else
    object[key] = value
  end
end

#callObject



10
11
12
13
14
15
16
17
# File 'lib/deep_count/adapter.rb', line 10

def call
  if input.is_a?(Hash) && input.keys.first.is_a?(Array)
    prepare_output
    output
  else
    input
  end
end

#prepare_outputObject



19
20
21
# File 'lib/deep_count/adapter.rb', line 19

def prepare_output
  input.each { |arr, value| add_to output, arr, value }
end