Class: DeepPluck::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/deep_pluck/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



3
4
5
6
# File 'lib/deep_pluck/config.rb', line 3

def initialize()
  @attrs = []
  @children_map = {}
end

Class Method Details

.attribute_names_to_args(attribute_names) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/deep_pluck/config.rb', line 8

def self.attribute_names_to_args(attribute_names)
  attribute_names = [attribute_names] if not attribute_names.is_a?(Array)

  config = self.new
  attribute_names.each do |attribute_name|
    attr_array = attribute_name.to_s.split('.')
    config.add(attr_array)
  end
  config.to_args
end

Instance Method Details

#add(attr_array) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/deep_pluck/config.rb', line 20

def add(attr_array)
  attr_array = [attr_array] if not attr_array.is_a?(Array)
  if attr_array.size > 1
    child_key = attr_array.slice!(0).to_sym

    @children_map[child_key] ||= self.class.new
    @children_map[child_key].add(attr_array)
  else
    @attrs << attr_array.first
  end
end

#to_argsObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/deep_pluck/config.rb', line 32

def to_args
  if @children_map.size > 0
    children_args = @children_map.reduce({}) do |h, (child_key, child_config)|
      h[child_key] = child_config.to_args
      h
    end
    @attrs.append(children_args)
  else
    @attrs
  end
end