Module: Ronin::Model::Importable::ClassMethods

Defined in:
lib/ronin/model/importable.rb

Overview

Since:

  • 1.3.0

Instance Method Summary collapse

Instance Method Details

#import(path) {|resource| ... } ⇒ Array<Model>

Extracts and imports resources from the given file.

Parameters:

  • path (String)

    The path of the file.

Yields:

  • (resource)

    The given block will be passed every imported resource.

Yield Parameters:

  • resource (Model)

    A successfully imported resource.

Returns:

  • (Array<Model>)

    If no block is given, an Array of imported resources is returned.

Since:

  • 1.3.0



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ronin/model/importable.rb', line 51

def import(path)
  return enum_for(:import,path).to_a unless block_given?

  File.open(path) do |file|
    file.each_line do |line|
      extract(line) do |resource|
        yield(resource) if resource.save
      end
    end
  end
end