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

Returns a new instance of DeepStruct.



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/api-config.rb', line 88

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, &block) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/api-config.rb', line 117

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

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

Instance Method Details

#each(&block) ⇒ Object



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

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

#map(&block) ⇒ Object



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

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

#path_for(key) ⇒ Object



113
114
115
# File 'lib/api-config.rb', line 113

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

#to_hObject



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

def to_h
  @hash_table
end