Ruby:如何在哈希中为命名空间添加前缀

发布于 2024-11-27 22:47:04 字数 1167 浏览 0 评论 0原文

我所处的情况是,我需要找出哈希的级别,并为该级别中的所有元素应用命名空间。

这是这样的场景:

  1. 我有一个用我的数据填充的对象。

  2. 接下来我将对象转换为哈希。

    #将对象转换为哈希
    def my_hash
      哈希[instance_variables.map { |var| [var[1..-1].to_sym,instance_variable_get(var)] }]
    结尾
    
  3. 最后,我想循环遍历哈希并将不同的命名空间应用于我的嵌套哈希。

    • 不幸的是,我无法找到一个好的解决方案来直接使用 savon gem 来执行此操作:

      soap.body = request_object.my_hash
      
    • 我将检查每个元素并尝试递归地找到分类方法中的嵌套级别:(这需要更多魔法)

      def 分类(o)
        案例o
          当哈希
            #需要将其重构为嵌套哈希的前缀:data NS,覆盖默认的:mes NS。 
            h = {}
            o.each {|k,v| h[k] = 分类(v)}
            小时                 
          别的
            o级
        结尾 
      结尾
      soap.body = 分类(request_object.my_hash)
      

它应该看起来像这样,源哈希:

{:UserTicket=>'123',:ImpersonationUsername=>'dave',:TicketSettings=>{:ResourceId=>'abcd',:ClientIp=>'0',:Username=>'bobby'}}

输出(其中 mes 和 data 是两个命名空间):

{'mes:UserTicket'=>'123','mes:ImpersonationUsername'=>'dave','mes:TicketSettings'=>{'data:ResourceId'=>'abcd','data:ClientIp'=>'0','data:Username'=>'bobby'}}

I am in a situation where I need to find out the level of the hash and apply a namespace for all elements in that level.

This is the scenario:

  1. I have an object which is populated with my data.

  2. Next I convert the object to hash.

    #convert Object to Hash
    def my_hash
      Hash[instance_variables.map { |var| [var[1..-1].to_sym, instance_variable_get(var)] }]
    end
    
  3. Finally I would like to loop thru the hash and apply a different Namespace to my nested hash.

    • Unfortunately I wasn't able to find a good solution to do this with savon gem directly:

      soap.body = request_object.my_hash
      
    • I will inspect each element and try to find the nested level in classify method recursively: (this requires some more magic)

      def classify(o)
        case o
          when Hash
            #Need to refactor this to prefix :data NS for nested hash, overwriting the default :mes NS. 
            h = {}
            o.each {|k,v| h[k] = classify(v)}
            h                 
          else
            o.class
        end 
      end
      soap.body = classify(request_object.my_hash)
      

It should look like this, The source hash:

{:UserTicket=>'123',:ImpersonationUsername=>'dave',:TicketSettings=>{:ResourceId=>'abcd',:ClientIp=>'0',:Username=>'bobby'}}

Output (where mes and data are two Namespaces):

{'mes:UserTicket'=>'123','mes:ImpersonationUsername'=>'dave','mes:TicketSettings'=>{'data:ResourceId'=>'abcd','data:ClientIp'=>'0','data:Username'=>'bobby'}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不语却知心 2024-12-04 22:47:04

这是一种传递与每个嵌套级别关联的标识符列表的方法:

def classify(o, with)
  case o
    when Hash
      h = {}
      o.each {|k,v| h[:"#{with[0]}:#{k}"] = classify(v, with[1, with.length])}
      h                 
    else
      o.class
  end 
end

hash = {:UserTicket=>'123',:ImpersonationUsername=>'dave',:TicketSettings=>{:ResourceId=>'abcd',:ClientIp=>'0',:Username=>'bobby'}}

classify(hash, [ :mes, :data ])
# => {"mes:UserTicket"=>String, "mes:ImpersonationUsername"=>String, "mes:TicketSettings"=>{"data:ResourceId"=>String, "data:ClientIp"=>String, "data:Username"=>String}}

如果您使用递归算法,您就有机会修改您向下挖掘的每个级别所应用的范围。

Here's an approach where you pass in a list of identifiers associated with each level of nesting:

def classify(o, with)
  case o
    when Hash
      h = {}
      o.each {|k,v| h[:"#{with[0]}:#{k}"] = classify(v, with[1, with.length])}
      h                 
    else
      o.class
  end 
end

hash = {:UserTicket=>'123',:ImpersonationUsername=>'dave',:TicketSettings=>{:ResourceId=>'abcd',:ClientIp=>'0',:Username=>'bobby'}}

classify(hash, [ :mes, :data ])
# => {"mes:UserTicket"=>String, "mes:ImpersonationUsername"=>String, "mes:TicketSettings"=>{"data:ResourceId"=>String, "data:ClientIp"=>String, "data:Username"=>String}}

If you're using a recursive algorithm you have the opportunity to modify the scope of what's being applied with each level you dig down.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文