Class: Rokaki::FilterModel::DeepAssignStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/rokaki/filter_model/deep_assign_struct.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keys:, value:, struct: nil) ⇒ DeepAssignStruct

Returns a new instance of DeepAssignStruct.



6
7
8
9
10
# File 'lib/rokaki/filter_model/deep_assign_struct.rb', line 6

def initialize(keys:, value:, struct: nil)
  @keys = keys
  @value = value
  @struct = struct
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



11
12
13
# File 'lib/rokaki/filter_model/deep_assign_struct.rb', line 11

def keys
  @keys
end

#structObject

Returns the value of attribute struct.



12
13
14
# File 'lib/rokaki/filter_model/deep_assign_struct.rb', line 12

def struct
  @struct
end

#valueObject (readonly)

Returns the value of attribute value.



11
12
13
# File 'lib/rokaki/filter_model/deep_assign_struct.rb', line 11

def value
  @value
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rokaki/filter_model/deep_assign_struct.rb', line 14

def call
  base_keys = keys
  i = base_keys.length - 1

  base_keys.reverse_each.reduce (value) do |struc,key|
    i -= 1
    cur_keys = base_keys[0..i]

    if struct
      val = struct.dig(*cur_keys)
      val[key] = struc
      p val
      return val
    else
      if key.is_a?(Integer)
        struct = [struc]
      else
        { key=>struc }
      end
    end
  end
end