Class: Daru::IO::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/daru/io/base.rb

Overview

Base IO Class that contains generic helper methods, to be used by other Importers::Base and Exporters::Base via inheritence

Instance Method Summary collapse

Instance Method Details

#optional_gem(dependency, version = nil, requires: nil, callback: self.class.name) ⇒ Object

Specifies and requires a gem, if the gem is present in the application environment. Else, raises LoadError with meaningful message of which dependency to install for which Daru-IO module.

Examples:

Requires with dependency

optional_gem 'avro'
#=> true

Requires with version and requires

optional_gem 'activerecord', '~> 4.0', requires: 'active_record'
#=> true

Raises error with meaningful message

df = Daru::DataFrame.from_json('path/to/file.json')
#=> LoadError: Please install the jsonpath gem, or add it to the
#   Gemfile to use the Daru::IO::Importers::JSON module.

Parameters:

  • dependency (String)

    A dependency to specify with gem command

  • version (String) (defaults to: nil)

    A version range to specify with gem command

  • requires (String) (defaults to: nil)

    The gem name to be required, in case it's different from the dependency name. For example, activerecord dependency has to be required as require 'active_record'

  • callback (Class) (defaults to: self.class.name)

    The Daru-IO module which is being used currently. Useful for throwing meaningful LoadError message.



33
34
35
36
37
38
39
40
41
42
# File 'lib/daru/io/base.rb', line 33

def optional_gem(dependency, version=nil, requires: nil,
  callback: self.class.name)
  gem dependency, version
  require requires || dependency
rescue LoadError
  version = version.nil? ? '' : " #{version} version"
  raise LoadError,
    "Please install the #{dependency} gem#{version}, "\
    "or add it to the Gemfile to use the #{callback} module."
end