(Rails / Ruby) Неожиданная переменная массива, растущие значения массива - PullRequest
0 голосов
/ 14 июня 2019

У меня постоянно растут массивы, хотя я перезагружаю их в цикле.Я в основном перебираю хеш, загруженный из YAML для создания модулей и классов в пространстве имен.

Кажется, что scope_path_a изменяется, но я не знаю почему.

class Ah
  class << self
    private

    def dropdown_spec
      YAML.load_file(path.to_s).with_indifferent_access
    end

    def path
      File.join(Rails.root, 'config', 'dropdown.yml')
    end
  end


  def self.last_level?(myhash,level2 = false)
   return true if myhash.nil?
   last_level = true
    myhash.each do |k,v|
      check_key = v.is_a?(Hash) ? true : false
      if check_key && level2
        v.each do |k1,v1|
         last_level = false  if v1.is_a?(Hash)
        end
      else
        last_level = false  if v.is_a?(Hash)
      end
    end
    last_level
  end

  def self.build_module(object,receiver, scope_path_b )
    scope_path_c = scope_path_b
    p "SCOPE_PATH_B"
    p scope_path_b
    p "END SCOPE PaTH"
    p "self.build module build"
    receiver.each do |k, v|
      p "start receiver each"
      if v.present?
        if last_level?(v) 
          c = object.const_set(k.to_s.camelcase, Class.new(ActiveYaml::Base){          
          })
          scope_path_c = scope_path_b
          scope_path_c << k
        else
          c = object.const_set(k.to_s.camelcase, Module.new)
          scope_path_c = scope_path_b
          scope_path_c << k
          if v.present?
            build_module(c,v, scope_path_c)
          end
        end
      end
      p 'next KEY'
    end
  end

  def self.build
    a = Object.const_set("Dropdown", Module.new)  
    p "Start build"
    scope_path_a = []
    scope_path_a = [:dropdown]
    p "Scope path A"
    p scope_path_a

    dropdown_spec[:dropdown].each do |k, v|
      p "scope A in Each level 1"
      p scope_path_a

      scope_path_b = scope_path_a
      p "Key"
      p k
      scope_path_b << k
      p "Scope Path B level 1 after K"
      p scope_path_b

      b = a.const_set(k.to_s.camelcase.camelcase, Module.new)
      if v.present?
        p "start build"
       build_module(b,v, scope_path_b) 
      end
    end
  end
end

Вывод:

"Start build"
"Scope path A"
[:dropdown]
"scope A in Each level 1"
[:dropdown]
"Key"
"common"
"Scope Path B level 1 after K"
[:dropdown, "common"]
"scope A in Each level 1"
[:dropdown, "common"]
"Key"
"advisor_registration"
"Scope Path B level 1 after K"
[:dropdown, "common", "car_registration"]

Ожидаемый ВЫХОД:

"Start build"
"Scope path A"
[:dropdown]
"scope A in Each level 1"
[:dropdown]
"Key"
"common"
"Scope Path B level 1 after K"
[:dropdown, :common]
"scope A in Each level 1"
[:dropdown]
"Key"
"advisor_registration"
"Scope Path B level 1 after K"
[:dropdown, :car_registration]

dropdown.yml

dropdown:
  common:
  car_registration:
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...