Class: IDL::Options

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/ridl/options.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil, marked = nil) ⇒ Options

Returns a new instance of Options.



21
22
23
24
# File 'lib/ridl/options.rb', line 21

def initialize(hash = nil, marked = nil)
  super(hash)
  @marked = marked
end

Class Method Details

.load_config(opt) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ridl/options.rb', line 135

def self.load_config(opt)
  # first collect config from known (standard and configured) locations
  _rc_paths = [RIDLRC_GLOBAL]
  _loaded_rc_paths = []
  (ENV['RIDLRC'] || '').split(/:|;/).each do |p|
    _rc_paths << p unless _rc_paths.include?(p)
  end
  _rc_paths.collect { |path| File.expand_path(path) }.each do |rcp|
    IDL.log(3, "Testing rc path #{rcp}")
    if File.readable?(rcp) && !_loaded_rc_paths.include?(rcp)
      opt.load(rcp)
      _loaded_rc_paths << rcp
    else
      IDL.log(3, "Ignoring #{File.readable?(rcp) ? 'already loaded' : 'inaccessible'} rc path #{rcp}")
    end
  end
  # now scan working path for any rc files
  _cwd = File.expand_path(Dir.getwd)
  IDL.log(3, "scanning working path #{_cwd} for rc files")
  # first collect any rc files found
  _rc_paths = []
  begin
    _rcp = File.join(_cwd, RIDLRC)
    if File.readable?(_rcp) && !_loaded_rc_paths.include?(_rcp)
      _rc_paths << _rcp unless _rc_paths.include?(_rcp)
    else
      IDL.log(3, "Ignoring #{File.readable?(_rcp) ? 'already loaded' : 'inaccessible'} rc path #{_rcp}")
    end
    break if /\A(.:(\\|\/)|\.|\/)\Z/ =~ _cwd

    _cwd = File.dirname(_cwd)
  end while true
  # now load them in reverse order
  _rc_paths.reverse.each do |_rcp|
    opt.load(_rcp)
    _loaded_rc_paths << _rcp
  end
end

Instance Method Details

#copy(from, *keys) ⇒ Object



40
41
42
# File 'lib/ridl/options.rb', line 40

def copy(from, *keys)
  self.dup.copy!(from, *keys)
end

#copy!(from, *keys) ⇒ Object



35
36
37
38
# File 'lib/ridl/options.rb', line 35

def copy!(from, *keys)
  keys.flatten.each { |k| self[k] = from[k] }
  self
end

#delete(k) ⇒ Object



44
45
46
# File 'lib/ridl/options.rb', line 44

def delete(k)
  table.delete(k)
end

#dupObject



56
57
58
# File 'lib/ridl/options.rb', line 56

def dup
  self.class.new(_dup_elem(@table), @marked)
end

#has_key?(k) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ridl/options.rb', line 48

def has_key?(k)
  @table.has_key?(k)
end

#keysObject



52
53
54
# File 'lib/ridl/options.rb', line 52

def keys
  @table.keys
end

#load(rcpath) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ridl/options.rb', line 68

def load(rcpath)
  IDL.log(3, "Loading #{RIDLRC} from #{rcpath}")
  _cfg = JSON.parse(File.read(rcpath))
  IDL.log(4, "Read from #{rcpath}: [#{_cfg}]")
  _rcdir = File.dirname(rcpath)
  # handle automatic env var expansion in ridl be_paths
  _cfg['be_path'] = (_cfg['be_path'] || []).collect do |p|
    IDL.log(5, "Examining RIDL be path [#{p}]")
    # for paths coming from rc files environment vars are immediately expanded and
    p.gsub!(/\$([^\s\/]+)/) { |m| ENV[$1] }
    IDL.log(6, "Expanded RIDL be path [#{p}]")
    # resulting relative paths converted to absolute paths
    _fp = File.expand_path(p, _rcdir)
    if File.directory?(_fp) # relative to rc location?
      p = _fp
    end # or relative to working dir
    IDL.fatal("Cannot access RIDL backend search path #{p} configured in #{rcpath}") unless File.directory?(p)
    IDL.log(4, "Adding RIDL backend search path : #{p}")
    p
  end
  merge!(_cfg)
end

#markObject



60
61
62
# File 'lib/ridl/options.rb', line 60

def mark
  @marked = _dup_elem(@table)
end

#merge(from, *keys) ⇒ Object



31
32
33
# File 'lib/ridl/options.rb', line 31

def merge(from, *keys)
  self.dup.merge!(from, *keys)
end

#merge!(from, *keys) ⇒ Object



26
27
28
29
# File 'lib/ridl/options.rb', line 26

def merge!(from, *keys)
  _merge(@table, from, *keys)
  self
end

#restoreObject



64
65
66
# File 'lib/ridl/options.rb', line 64

def restore
  self.class.new(_dup_elem(@marked || @table), @marked)
end