File: dig.rb

package info (click to toggle)
ruby-recursive-open-struct 1.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 188 kB
  • sloc: ruby: 912; makefile: 6
file content (22 lines) | stat: -rw-r--r-- 535 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class RecursiveOpenStruct < OpenStruct
  module Dig

    # Replaces +OpenStruct#dig+ to properly support treating nested values as
    # RecursiveOpenStructs instead of returning the nested Hashes.
    def dig(name, *names)
      begin
        name = name.to_sym
      rescue NoMethodError
        raise TypeError, "#{name} is not a symbol nor a string"
      end

      name_val = self[name]

      if names.length > 0 && name_val.respond_to?(:dig)
        name_val.dig(*names)
      else
        name_val
      end
    end
  end
end