Class: CORL::Configuration::File

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

Instance Method Summary collapse

Instance Method Details

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




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
273
274
275
276
277
278
279
280
281
# File 'lib/CORL/configuration/file.rb', line 237

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 => error
      warn(error.message, { :i18n => false })
      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 => error
          warn(error.message, { :i18n => false })
          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




285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/CORL/configuration/file.rb', line 285

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



39
40
41
42
43
44
45
46
47
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
84
85
86
87
88
89
# File 'lib/CORL/configuration/file.rb', line 39

def load(options = {})
  super do |method_config, properties|
    success = true

    myself.status = code.success

    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}")

          begin
            parse_properties = parser.parse(raw)

            generate_routes.call(config_name, parse_properties)
            properties.import(parse_properties)

          rescue => error
            ui_group(file) do
              error(error.message.sub(/^[^\:]+\:\s+/, ''), { :i18n => false })
            end
            myself.status = 255
            success       = false
          end
        end
        CORL.remove_plugin(parser) if parser
      end
    end
    success
  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({}, {}, true, false))
    _set(:router, Config.new({}, {}, true, false))
  end
end

#remove(options = {}) ⇒ Object




222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/CORL/configuration/file.rb', line 222

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




25
26
27
# File 'lib/CORL/configuration/file.rb', line 25

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
210
211
212
213
214
215
216
217
218
# File 'lib/CORL/configuration/file.rb', line 169

def save(options = {})
  super do |method_config|
    config_files = []
    success      = true
    deleted_keys = search.export.keys

    separate.export.each do |config_name, router_data|
      info         = search[config_name]
      provider     = info[:provider]
      file         = info[:file]
      file_dir     = ::File.dirname(file)
      deleted_keys = deleted_keys - [ config_name ]

      FileUtils.mkdir_p(file_dir) unless Dir.exists?(file_dir)

      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
        CORL.remove_plugin(renderer)
      else
        success = false
      end
      break unless success
    end
    if success
      # Check for removals
      deleted_keys.each do |config_name|
        info = search[config_name]

        search.delete(config_name)
        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



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

def search
  _get(:search)
end

#set_location(directory) ⇒ Object




31
32
33
34
# File 'lib/CORL/configuration/file.rb', line 31

def set_location(directory)
  super
  search_files if directory
end