Class: DeepCount::Adapter
- Inherits:
-
Object
- Object
- DeepCount::Adapter
- Defined in:
- lib/deep_count/adapter.rb
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
- #add_to(object, array, value) ⇒ Object
- #call ⇒ Object
-
#initialize(input) ⇒ Adapter
constructor
A new instance of Adapter.
- #prepare_output ⇒ Object
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
#input ⇒ Object (readonly)
Returns the value of attribute input.
3 4 5 |
# File 'lib/deep_count/adapter.rb', line 3 def input @input end |
#output ⇒ Object (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 |
#call ⇒ Object
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_output ⇒ Object
19 20 21 |
# File 'lib/deep_count/adapter.rb', line 19 def prepare_output input.each { |arr, value| add_to output, arr, value } end |