Module: Earth

Defined in:
lib/earth.rb,
lib/earth/eia.rb,
lib/earth/model.rb,
lib/earth/tasks.rb,
lib/earth/utils.rb,
lib/earth/loader.rb,
lib/earth/version.rb,
lib/earth/warnings.rb,
lib/earth/irradiance_scopes.rb

Overview

The earth module is an interface for loading data models

Defined Under Namespace

Modules: EIA, IrradianceScopes, Loader, Model, Utils, Warnings Classes: Tasks

Constant Summary collapse

VENDOR_DIR =
::File.expand_path '../../vendor', __FILE__
LIB_DIR =
::File.expand_path '../earth', __FILE__
DATA_DIR =
::File.expand_path '../../data', __FILE__
ERRATA_DIR =
::File.expand_path '../../errata', __FILE__
FACTORY_DIR =
::File.expand_path '../../spec/factories', __FILE__
VERSION =
"1.2.1"

Class Method Summary collapse

Class Method Details

.connectObject

Connect to the database using ActiveRecord’s default behavior



72
73
74
75
# File 'lib/earth.rb', line 72

def Earth.connect
  ActiveRecord::Base.establish_connection
  ActiveRecord::Base.connection
end

.envObject

The current environment. Earth detects the following environment variables:

  • EARTH_ENV (for CLI apps and daemons)

  • RAILS_ENV

  • RACK_ENV

Default is ‘development`



84
85
86
# File 'lib/earth.rb', line 84

def Earth.env
  @env ||= ActiveSupport::StringInquirer.new(ENV['EARTH_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development')
end

.init(*args) ⇒ Object

Earth.init is the gateway to using Earth. It can load all models at once, connect to the database using Rails conventions, and set up the models to pull data from original sources instead of Brighter Planet’s pre-processed data service.

  • :mine_original_sources, if true, will load files necessary to data mine from scratch rather than downloading from data.brighterplanet.com. Note that you must run Earth.init before requiring models in order for this option to work properly.

  • :connect will connect to the database for you

Parameters:

  • load_directive (Symbol)

    use ‘:all` to load all models at once (optional)

  • options (Hash)

    load options



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/earth.rb', line 37

def Earth.init(*args)
  options = args.extract_options!

  if options[:connect]
    connect 
    Warnings.check_mysql_ansi_mode
  end

  Earth.mine_original_sources = options[:load_data_miner] || options[:mine_original_sources]
  
  if args.include? :all
    require 'earth/all'
  elsif args.length > 0
    Kernel.warn "Deprecation Warning: `Earth.init :domain` will be removed. Use `require 'earth/domain'` instead"
    args.each do |argh|
      require "earth/#{argh}"
    end
  end
end

.reset_schemas!Object

Drop and recreate tables for all currently loaded data models.



90
91
92
# File 'lib/earth.rb', line 90

def Earth.reset_schemas!
  Earth.resource_models.each(&:create_table!)
end

.resource_modelsArray

List the currently loaded data model classes

Returns:

  • (Array)

    a list of resource classes



67
68
69
# File 'lib/earth.rb', line 67

def Earth.resource_models
  Earth::Model.registry
end

.resourcesArray

List the currently loaded data model class names.

Returns:

  • (Array)

    a list of camelized resource names



60
61
62
# File 'lib/earth.rb', line 60

def Earth.resources
  @resources ||= Earth.resource_models.map(&:to_s).sort
end

.run_data_miner!Object

Note:

By default, data is mined from data.brighterplanet.com

Run data miner on all currently loaded data models.

via taps. In order to mine from scratch, call Earth.init with the :mine_original_sources option.



99
100
101
# File 'lib/earth.rb', line 99

def Earth.run_data_miner!
  DataMiner.run(Earth.resources)
end