Class: CORL::Configuration::File

Inherits:
Object
  • Object
show all
Defined in:
lib/CORL/configuration/file.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.finalize(file_name) ⇒ Object




18
19
20
21
22
23
# File 'lib/CORL/configuration/file.rb', line 18

def self.finalize(file_name)
  proc do
    logger.debug("Finalizing file: #{file_name}")
    Util::Disk.close(file_name)
  end
end

Instance Method Details

#attach(type, name, data, options = {}) ⇒ Object




228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/CORL/configuration/file.rb', line 228

def attach(type, name, data, options = {})
  super do |method_config|
    attach_path = Util::Disk.filename([ project.directory, type.to_s ])
    success     = true
    
    begin
      FileUtils.mkdir_p(attach_path) unless Dir.exists?(attach_path)
    
    rescue Exception => error
      alert(error.message)
      success = false
    end
    
    if success
      case method_config.get(:type, :source)
      when :source
        new_file = project.local_path(Util::Disk.filename([ attach_path, name ]))
        
        logger.debug("Attaching source data (length: #{data.length}) to configuration at #{attach_path}")        
        success = Util::Disk.write(new_file, data)
        
      when :file  
        file       = ::File.expand_path(data)
        attach_ext = ::File.basename(file)
        new_file   = project.local_path(Util::Disk.filename([ attach_path, "#{name}-#{attach_ext}" ])) 
            
        logger.debug("Attaching file #{file} to configuration at #{attach_path}")       
    
        begin
          FileUtils.mkdir_p(attach_path) unless Dir.exists?(attach_path)
          FileUtils.cp(file, new_file)
        
        rescue Exception => error
          alert(error.message)
          success = false
        end  
      end
    end
    if success && autosave
      logger.debug("Attaching data to project as #{new_file}")
      success = update_project(new_file, method_config)
    end
    success ? new_file : nil
  end
end

#delete_attachments(ids, options = {}) ⇒ Object




276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/CORL/configuration/file.rb', line 276

def delete_attachments(ids, options = {})
  super do |method_config|
    success = true
    files   = []
    
    array(ids).each do |id|
      file = ::File.join(project.directory, id.to_s)
      
      if Util::Disk.delete(file)
        files << file
      else
        success = false
      end
    end
    
    if success && autosave
      logger.debug("Removing attached data from project as #{files.join(', ')}")
      success = update_project(files, method_config)
    end
    success ? files : nil
  end
end

#load(options = {}) ⇒ Object


Configuration loading / saving



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/CORL/configuration/file.rb', line 48

def load(options = {})
  super do |method_config, properties|
    
    generate_routes = lambda do |config_name, file_properties, parents = []|
      file_properties.each do |name, value|
        keys = [ parents, name ].flatten
        
        if value.is_a?(Hash) && ! value.empty?
          generate_routes.call(config_name, value, keys)
        else
          router.set(keys, config_name)  
        end
      end
    end
    
    if fetch_project(method_config)
      search.export.each do |config_name, info|
        provider = info[:provider]
        file     = info[:file]
      
        logger.info("Loading #{provider} translated source configuration from #{file}")
        
        parser = CORL.translator(method_config, provider)
        raw    = Util::Disk.read(file)    
      
        if parser && raw && ! raw.empty?
          logger.debug("Source configuration file contents: #{raw}")            
          parse_properties = parser.parse(raw)
          
          generate_routes.call(config_name, parse_properties)
          properties.import(parse_properties)
        end         
      end
    end
  end
end

#normalize(reload) ⇒ Object


Configuration plugin interface



9
10
11
12
13
14
# File 'lib/CORL/configuration/file.rb', line 9

def normalize(reload)
  super do        
    _set(:search, Config.new)
    _set(:router, Config.new)
  end
end

#remove(options = {}) ⇒ Object




213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/CORL/configuration/file.rb', line 213

def remove(options = {})
  super do |method_config|
    success      = true
    config_files = [] 
    search.each do |config_name, info|
      config_files << info[:file]
      success = false unless Util::Disk.delete(info[:file])
    end
    success = update_project(config_files, method_config) if success
    success
  end
end

#routerObject




34
35
36
# File 'lib/CORL/configuration/file.rb', line 34

def router
  _get(:router)
end

#save(options = {}) ⇒ Object




169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/CORL/configuration/file.rb', line 169

def save(options = {})
  super do |method_config|
    config_files = []
    success      = true      
    search_data  = Config.new(search.export)
    
    separate.export.each do |config_name, router_data|
      info     = search_data.delete(config_name)
      provider = info[:provider]
      file     = info[:file]
      
      if renderer = CORL.translator(method_config, provider)
        rendering = renderer.generate(router_data)
        
        if Util::Disk.write(file, rendering)
          config_files << config_name
        else
          success = false
        end
      else
        success = false
      end
      break unless success        
    end
    if success
      # Check for removals
      search_data.export.each do |config_name, info|
        FileUtils.rm_f(info[:file])
        config_files << config_name
      end
    end
    if success && ! config_files.empty?
      # Commit changes
      commit_files = [ config_files, method_config.get_array(:files) ].flatten
      
      logger.debug("Source configuration rendering: #{rendering}")        
      success = update_project(commit_files, method_config)
    end
    success
  end
end

#searchObject


Property accessors / modifiers



28
29
30
# File 'lib/CORL/configuration/file.rb', line 28

def search
  _get(:search)
end

#set_location(directory) ⇒ Object




40
41
42
43
# File 'lib/CORL/configuration/file.rb', line 40

def set_location(directory)
  super
  search_files if directory
end