Module: Earth

Extended by:
Earth
Included in:
Earth
Defined in:
lib/earth.rb,
lib/earth/base.rb,
lib/earth/version.rb

Overview

The earth module is an interface for establishing a taps server (used to fetch data) and for loading data models from various domains.

Defined Under Namespace

Modules: Base

Constant Summary collapse

VERSION =
'0.4.0'

Instance Method Summary collapse

Instance Method Details

#database_optionsObject



89
90
91
92
93
94
95
# File 'lib/earth.rb', line 89

def database_options
  if ActiveRecord::Base.connection.adapter_name.downcase == 'sqlite'
    {}
  else
    { :options => 'ENGINE=InnoDB default charset=utf8' }
  end
end

#domainsObject



46
47
48
# File 'lib/earth.rb', line 46

def domains
  @domains ||= resources.map { |(name, data)| data[:domain] }.uniq.sort
end

#gem_rootObject



42
43
44
# File 'lib/earth.rb', line 42

def gem_root 
  File.expand_path File.join(File.dirname(__FILE__), '..')
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.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/earth.rb', line 72

def init(*args)
  load_plugins

  domains = []
  options = {}
  args.each do |arg|
    if arg.is_a?(Hash)
      options = arg
    else
      domains << arg
    end
  end

  load_domains(domains, options)
  load_schemas(options) if options[:apply_schemas]
end

#resource_names(search_domains = nil) ⇒ Object

Takes argument like Earth.resource_names() Default is search all domains For example, [ 'Aircraft', 'Airline' ]



31
32
33
34
35
36
37
38
39
40
# File 'lib/earth.rb', line 31

def resource_names(search_domains = nil)
  if search_domains.nil?
    resources.keys
  else
    resources.inject([]) do |list, (name, data)|
      list << name if search_domains.include? data[:domain]
      list
    end
  end
end

#resourcesObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/earth.rb', line 50

def resources
  return @resources unless @resources.nil?
  earth_files = Dir[File.join(Earth.gem_root, 'lib', 'earth', '*')]
  domain_dirs = earth_files.find_all { |f| File.directory?(f) }
  @resources = domain_dirs.inject({}) do |hsh, domain_dir|
    Dir[File.join(domain_dir, '*.rb')].each do |resource_file|
      resource_camel = File.basename(resource_file, '.rb').camelcase
      unless resource_camel == 'DataMiner'
        hsh[resource_camel] = { :domain => File.basename(domain_dir) }
      end
    end
    hsh
  end
end

#taps_serverObject



19
20
21
# File 'lib/earth.rb', line 19

def taps_server
  @taps_server || 'http://carbon:[email protected]:5000'
end

#taps_server=(val) ⇒ Object

taps_server is a URL. See the data_miner gem docs



24
25
26
# File 'lib/earth.rb', line 24

def taps_server=(val)
  @taps_server = val
end