Class: APIConfig::DeepStruct

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/api-config.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil, file = nil, parent = nil, branch = nil) ⇒ DeepStruct



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/api-config.rb', line 84

def initialize(hash = nil, file = nil, parent = nil, branch = nil)
  @table      = {}
  @hash_table = {}
  @file       = file
  @parent     = parent
  @branch     = branch

  (hash || []).each do |k, v|
    @table[k.to_sym]      = (v.is_a?(Hash) ? self.class.new(v, file, self, k) : v)
    @hash_table[k.to_sym] = v
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/api-config.rb', line 113

def method_missing(meth, *args, &)
  if meth.to_s =~ /\A(.+)!\Z/
    setting = ::Regexp.last_match(1).intern

    @table.fetch(setting) { raise Error, "API Setting `#{path_for(setting)}' not found in #{@file}" }
  else
    super
  end
end

Instance Method Details

#eachObject



97
98
99
# File 'lib/api-config.rb', line 97

def each(&)
  @table.each(&)
end

#mapObject



101
102
103
# File 'lib/api-config.rb', line 101

def map(&)
  @table.map(&)
end

#path_for(key) ⇒ Object



109
110
111
# File 'lib/api-config.rb', line 109

def path_for(key)
  [(@parent ? @parent.path_for(@branch) : APIConfig.env), key].compact.join('.')
end

#to_hObject



105
106
107
# File 'lib/api-config.rb', line 105

def to_h
  @hash_table
end