Module: Earth

Defined in:
lib/earth.rb,
lib/earth/eia.rb,
lib/earth/utils.rb,
lib/earth/version.rb,
lib/earth/active_record_base_class_methods.rb

Overview

The earth module is an interface for loading data models from various domains.

Defined Under Namespace

Modules: ActiveRecordBaseClassMethods, EIA, Utils

Constant Summary collapse

VERSION =
"0.12.4"
TAPS_SOURCE =
'http://carbon:[email protected]:5000'
TAPS_DESCRIPTION =
"Brighter Planet's reference data web service"
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__

Class Method Summary collapse

Class Method Details

.domainsObject



28
29
30
31
32
33
34
# File 'lib/earth.rb', line 28

def Earth.domains
  @domains ||= ::Dir[::File.join(LIB_DIR, '*')].map do |path|
    if ::File.directory? path
      ::File.basename path
    end
  end.compact.uniq.sort
end

.init(*args) ⇒ Object

Earth.init will load any specified domains, any needed ActiveRecord plugins, and will apply each domain model’s schema to the database if the :apply_schemas option is given. See Earth.domains for the list of allowable domains.

Earth.init should be performed after a connection is made to the database and before any domain models are referenced.



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
90
91
# File 'lib/earth.rb', line 57

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

  warn_unless_mysql_ansi_mode
  load_plugins
  
  if domains.include?(:none)
    # don't load anything
  elsif domains.include?(:all) or domains.empty?
    require_all options
  else
    domains.each do |domain|
      require_domain domain, options
    end
  end
  
  # be sure to look at both explicitly and implicitly loaded resources
  resources.select do |resource|
    ::Object.const_defined?(resource)
  end.each do |resource|
    resource_model = resource.constantize
    unless options[:skip_parent_associations]
      resource_model.data_miner_script.append_once :process, :run_data_miner_on_parent_associations!
    end
    if options[:load_data_miner]
      resource_model.data_miner_script.prepend_once :process, :auto_upgrade!
    else
      resource_model.data_miner_script.prepend_once :tap, TAPS_DESCRIPTION, TAPS_SOURCE
    end
    if options[:apply_schemas]
      resource_model.auto_upgrade!
    end
  end
end

.require_all(options = {}) ⇒ Object

internal use



102
103
104
# File 'lib/earth.rb', line 102

def Earth.require_all(options = {})
  require_glob ::File.join(LIB_DIR, '**', '*.rb'), options
end

internal use

Raises:

  • (::ArgumentError)


94
95
96
97
98
99
# File 'lib/earth.rb', line 94

def Earth.require_related(path)
  path = ::File.expand_path path
  raise ::ArgumentError, %{[earth gem] #{path} is not in #{LIB_DIR}} unless path.start_with?(LIB_DIR)
  domain = %r{#{LIB_DIR}/([^\./]+)}.match(path).captures.first
  require_domain domain, :load_data_miner => path.include?('data_miner')
end

.resources(*search_domains) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/earth.rb', line 36

def Earth.resources(*search_domains)
  search_domains = search_domains.flatten.compact.map(&:to_s)
  if search_domains.empty?
    search_domains = domains
  end
  search_domains.map do |domain|
    ::Dir[::File.join(LIB_DIR, domain, '**', '*.rb')].map do |possible_resource|
      unless possible_resource.include?('data_miner')
        ::File.basename(possible_resource, '.rb').camelcase
      end
    end
  end.flatten.compact.sort
end