class CORL::Configuration::File

Public Instance Methods

attach(type, name, data, options = {}) click to toggle source
Calls superclass method
    # File lib/CORL/configuration/file.rb
237 def attach(type, name, data, options = {})
238   super do |method_config|
239     attach_path = Util::Disk.filename([ project.directory, type.to_s ])
240     success     = true
241 
242     begin
243       FileUtils.mkdir_p(attach_path) unless Dir.exists?(attach_path)
244 
245     rescue => error
246       warn(error.message, { :i18n => false })
247       success = false
248     end
249 
250     if success
251       case method_config.get(:type, :source)
252       when :source
253         new_file = project.local_path(Util::Disk.filename([ attach_path, name ]))
254 
255         logger.debug("Attaching source data (length: #{data.length}) to configuration at #{attach_path}")
256         success = Util::Disk.write(new_file, data)
257 
258       when :file
259         file       = ::File.expand_path(data)
260         attach_ext = ::File.basename(file)
261         new_file   = project.local_path(Util::Disk.filename([ attach_path, "#{name}-#{attach_ext}" ]))
262 
263         logger.debug("Attaching file #{file} to configuration at #{attach_path}")
264 
265         begin
266           FileUtils.mkdir_p(attach_path) unless Dir.exists?(attach_path)
267           FileUtils.cp(file, new_file)
268 
269         rescue => error
270           warn(error.message, { :i18n => false })
271           success = false
272         end
273       end
274     end
275     if success && autosave
276       logger.debug("Attaching data to project as #{new_file}")
277       success = update_project(new_file, method_config)
278     end
279     success ? new_file : nil
280   end
281 end
delete_attachments(ids, options = {}) click to toggle source
Calls superclass method
    # File lib/CORL/configuration/file.rb
285 def delete_attachments(ids, options = {})
286   super do |method_config|
287     success = true
288     files   = []
289 
290     array(ids).each do |id|
291       file = ::File.join(project.directory, id.to_s)
292 
293       if Util::Disk.delete(file)
294         files << file
295       else
296         success = false
297       end
298     end
299 
300     if success && autosave
301       logger.debug("Removing attached data from project as #{files.join(', ')}")
302       success = update_project(files, method_config)
303     end
304     success ? files : nil
305   end
306 end
load(options = {}) click to toggle source
Calls superclass method
   # File lib/CORL/configuration/file.rb
39 def load(options = {})
40   super do |method_config, properties|
41     success = true
42 
43     myself.status = code.success
44 
45     generate_routes = lambda do |config_name, file_properties, parents = []|
46       file_properties.each do |name, value|
47         keys = [ parents, name ].flatten
48 
49         if value.is_a?(Hash) && ! value.empty?
50           generate_routes.call(config_name, value, keys)
51         else
52           router.set(keys, config_name)
53         end
54       end
55     end
56 
57     if fetch_project(method_config)
58       search.export.each do |config_name, info|
59         provider = info[:provider]
60         file     = info[:file]
61 
62         logger.info("Loading #{provider} translated source configuration from #{file}")
63 
64         parser = CORL.translator(method_config, provider)
65         raw    = Util::Disk.read(file)
66 
67         if parser && raw && ! raw.empty?
68           logger.debug("Source configuration file contents: #{raw}")
69 
70           begin
71             parse_properties = parser.parse(raw)
72 
73             generate_routes.call(config_name, parse_properties)
74             properties.import(parse_properties)
75 
76           rescue => error
77             ui_group(file) do
78               error(error.message.sub(/^[^\:]+\:\s+/, ''), { :i18n => false })
79             end
80             myself.status = 255
81             success       = false
82           end
83         end
84         CORL.remove_plugin(parser) if parser
85       end
86     end
87     success
88   end
89 end
normalize(reload) click to toggle source
Calls superclass method
   # File lib/CORL/configuration/file.rb
 9 def normalize(reload)
10   super do
11     _set(:search, Config.new({}, {}, true, false))
12     _set(:router, Config.new({}, {}, true, false))
13   end
14 end
remove(options = {}) click to toggle source
Calls superclass method
    # File lib/CORL/configuration/file.rb
222 def remove(options = {})
223   super do |method_config|
224     success      = true
225     config_files = []
226     search.each do |config_name, info|
227       config_files << info[:file]
228       success = false unless Util::Disk.delete(info[:file])
229     end
230     success = update_project(config_files, method_config) if success
231     success
232   end
233 end
router() click to toggle source
   # File lib/CORL/configuration/file.rb
25 def router
26   _get(:router)
27 end
save(options = {}) click to toggle source
Calls superclass method
    # File lib/CORL/configuration/file.rb
169 def save(options = {})
170   super do |method_config|
171     config_files = []
172     success      = true
173     deleted_keys = search.export.keys
174 
175     separate.export.each do |config_name, router_data|
176       info         = search[config_name]
177       provider     = info[:provider]
178       file         = info[:file]
179       file_dir     = ::File.dirname(file)
180       deleted_keys = deleted_keys - [ config_name ]
181 
182       FileUtils.mkdir_p(file_dir) unless Dir.exists?(file_dir)
183 
184       if renderer = CORL.translator(method_config, provider)
185         rendering = renderer.generate(router_data)
186 
187         if Util::Disk.write(file, rendering)
188           config_files << config_name
189         else
190           success = false
191         end
192         CORL.remove_plugin(renderer)
193       else
194         success = false
195       end
196       break unless success
197     end
198     if success
199       # Check for removals
200       deleted_keys.each do |config_name|
201         info = search[config_name]
202 
203         search.delete(config_name)
204         FileUtils.rm_f(info[:file])
205 
206         config_files << config_name
207       end
208     end
209     if success && ! config_files.empty?
210       # Commit changes
211       commit_files = [ config_files, method_config.get_array(:files) ].flatten
212 
213       logger.debug("Source configuration rendering: #{rendering}")
214       success = update_project(commit_files, method_config)
215     end
216     success
217   end
218 end
set_location(directory) click to toggle source
Calls superclass method
   # File lib/CORL/configuration/file.rb
31 def set_location(directory)
32   super
33   search_files if directory
34 end

Protected Instance Methods

fetch_project(options = {}) click to toggle source
    # File lib/CORL/configuration/file.rb
359 def fetch_project(options = {})
360   config  = Config.ensure(options)
361   success = true
362   if remote = config.get(:remote, nil)
363     logger.info("Pulling configuration updates from remote #{remote}")
364     success = project.pull(remote, config) if config.get(:pull, true)
365   end
366   success
367 end
search_files() click to toggle source
    # File lib/CORL/configuration/file.rb
311 def search_files
312 
313   add_search_file = lambda do |config_name, file, provider, info|
314     if Util::Disk.exists?(file)
315       search[config_name] = {
316         :provider => provider,
317         :info     => info,
318         :file     => file
319       }
320     else
321       logger.info("Configuration file #{file} does not exist")
322     end
323   end
324 
325   if Util::Data.empty?(project.directory)
326     logger.debug("Clearing configuration file information")
327     search.clear
328   else
329     translators = CORL.loaded_plugins(:nucleon, :translator)
330     file_bases  = [ :corl, extension_set(:base, []) ].flatten.uniq
331 
332     project.localize do
333       translators.each do |provider, info|
334         Dir.glob(::File.join('nodes', '**', "*.#{provider}")).each do |file|
335           config_name = file
336           file        = ::File.join(project.directory, file)
337 
338           add_search_file.call(config_name, file, provider, info)
339         end
340       end
341     end
342 
343     translators.each do |provider, info|
344       file_bases.each do |file_base|
345         config_name = "#{file_base}.#{provider}"
346         file        = Util::Disk.filename([ project.directory, config_name ])
347 
348         add_search_file.call(config_name, file, provider, info)
349       end
350     end
351     logger.debug("Setting configuration file information to #{search.inspect}")
352   end
353   load(_export) if autoload
354 end
select_largest(router) click to toggle source
    # File lib/CORL/configuration/file.rb
393 def select_largest(router)
394   return router unless router.is_a?(Hash)
395 
396   config_map = {}
397 
398   count_config_names = lambda do |data|
399     data = data.export if data.is_a?(CORL::Config)
400     data.each do |name, value|
401       if value.is_a?(Hash)
402         count_config_names.call(value)
403       else
404         config_map[value] = 0 unless config_map.has_key?(value)
405         config_map[value] = config_map[value] + 1
406       end
407     end
408   end
409 
410   config_name   = nil
411   config_weight = nil
412 
413   count_config_names.call(router)
414   config_map.each do |name, weight|
415     if config_name.nil? || weight > config_weight
416       config_name   = name
417       config_weight = weight
418     end
419   end
420   config_name
421 end
separate() click to toggle source

properties = values

to

file_data[key…] = config

    # File lib/CORL/configuration/file.rb
 99 def separate
100   file_data        = Config.new
101   default_provider = CORL.type_default(:nucleon, :translator)
102 
103   split_config = lambda do |properties, local_router, parents = []|
104     properties.each do |name, value|
105       next if name.to_sym == :nodes
106 
107       keys = [ parents, name ].flatten
108 
109       if value.is_a?(Hash) && ! value.empty?
110         # Nested configurations
111         if local_router.is_a?(Hash) && local_router.has_key?(name)
112           # Router and configuration values are nested
113           split_config.call(value, local_router[name], keys)
114         else
115           # Just configuration values are nested
116           if local_router.is_a?(String)
117             # We are down to a config_name router.  Inherit on down the line
118             split_config.call(value, local_router, keys)
119           else
120             # Never encountered before
121             config_name = nil
122 
123             config_name = select_largest(router.get(parents)) unless parents.empty?
124             split_config.call(value, config_name, keys)
125           end
126         end
127       else
128         if local_router.is_a?(String)
129           # Router is a config_name string
130           file_data.set([ local_router, keys ].flatten, value)
131 
132         elsif local_router.is_a?(Hash)
133           # Router is a hash with sub options we have to pick from
134           config_name = select_largest(local_router)
135           file_data.set([ config_name, keys ].flatten, value)
136         else
137           # Router is non existent.  Resort to easily found default
138           config_name = "corl.#{default_provider}"
139           file_data.set([ config_name, keys ].flatten, value)
140         end
141       end
142     end
143   end
144 
145   if config.get(:nodes, false)
146     config[:nodes].each do |provider, data|
147       data.each do |name, info|
148         config_name = ::File.join('nodes', provider.to_s, "#{name}.#{default_provider}")
149         file_data.set([ config_name, :nodes, provider, name ], info)
150 
151         unless search[config_name]
152           search[config_name] = {
153             :provider => default_provider,
154             :file     => ::File.join(project.directory, config_name)
155           }
156         end
157       end
158     end
159   end
160 
161   # Whew!  Glad that's over...
162   split_config.call(Util::Data.subset(config.export, config.keys - [ :nodes ]), router.export)
163   file_data
164 end
update_project(files = [], options = {}) click to toggle source
    # File lib/CORL/configuration/file.rb
372 def update_project(files = [], options = {})
373   config  = Config.ensure(options)
374   success = true
375 
376   commit_files = '.'
377   commit_files = array(files).flatten unless files.empty?
378 
379   logger.info("Committing changes to configuration files")
380   success = project.commit(commit_files, config)
381 
382   if success && remote = config.get(:remote, nil)
383     logger.info("Pushing configuration updates to remote #{remote}")
384     success = project.pull(remote, config) if config.get(:pull, true) && ! config.get(:push, false)
385     success = project.push(remote, config) if success && config.get(:push, true)
386   end
387   success
388 end