Class: Peacekeeper::Loader
- Inherits:
-
Object
- Object
- Peacekeeper::Loader
- Defined in:
- lib/peacekeeper/loader.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #active_record_config ⇒ Object
-
#initialize(config) ⇒ Loader
constructor
A new instance of Loader.
- #load_source ⇒ Object
- #paramize(options) ⇒ Object
-
#sequel_db_uri ⇒ Object
Construct uri to connect to database Sequel: sequel.rubyforge.org/rdoc/files/doc/opening_databases_rdoc.html.
Constructor Details
#initialize(config) ⇒ Loader
Returns a new instance of Loader.
5 6 7 8 |
# File 'lib/peacekeeper/loader.rb', line 5 def initialize(config) @config = config @source = config[:source] end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
3 4 5 |
# File 'lib/peacekeeper/loader.rb', line 3 def config @config end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
3 4 5 |
# File 'lib/peacekeeper/loader.rb', line 3 def source @source end |
Instance Method Details
#active_record_config ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/peacekeeper/loader.rb', line 65 def active_record_config protocol = config['protocol'] || config['adapter'] || 'sqlite3' # Set the adapter (DB engine; i.e. mysql, sqlite3, postgres, etc.) database = config['database'] ar_config = { adapter: protocol, database: database } ar_config['host'] = config['host'] if config['host'] ar_config['username'] = config['username'] if config['username'] ar_config['password'] = config['password'] if config['password'] ar_config['driver'] = config['driver'] if config['driver'] ar_config end |
#load_source ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/peacekeeper/loader.rb', line 10 def load_source case source when :sequel require 'sequel' Sequel::Model.db = Sequel::DATABASES.find { |db| db.uri == sequel_db_uri } || Sequel.connect(sequel_db_uri) when :active_record require 'active_record' ActiveRecord::Base.establish_connection(active_record_config) when :api require 'nasreddin' when :mock require config[:mock_library] if config[:mock_library] #data_class # Trigger mock data_class creation end end |
#paramize(options) ⇒ Object
81 82 83 84 |
# File 'lib/peacekeeper/loader.rb', line 81 def paramize() params = .map { |k, v| "#{k}=#{v}" } "#{params.join('&')}" end |
#sequel_db_uri ⇒ Object
Construct uri to connect to database Sequel: sequel.rubyforge.org/rdoc/files/doc/opening_databases_rdoc.html
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/peacekeeper/loader.rb', line 29 def sequel_db_uri # Set the protocol (DB engine; i.e. mysql, sqlite3, postgres, etc.) protocol = config['protocol'] || config['adapter'] || 'sqlite' if RUBY_ENGINE == 'jruby' protocol = "jdbc:#{protocol}" unless protocol.start_with? "jdbc:" end # Set the path (hostname & database name) path = "#{config['host'] || config['path']}/#{config['database']}" path = '' if path == '/' # Clear path if 'host', 'path', and 'database' are all unset # Set the user and password if RUBY_ENGINE == 'jruby' && protocol == 'jdbc:mysql' # Special case for JRuby and MySQL user_pass = "?user=#{config['username']}&password=#{config['password']}" server_path = "#{path}#{user_pass}" else user_pass = "#{config['username']}:#{config['password']}@" user_pass = '' if user_pass == ':@' # Clear user_pass if both 'username' and 'password' are unset server_path = "#{user_pass}#{path}" end # Finally, put the protocol and path components together server_path = "/#{server_path}" unless server_path.empty? uri = "#{protocol}:/#{server_path}" uri = 'jdbc:sqlite::memory:' if uri == 'jdbc:sqlite:/' && RUBY_ENGINE == 'jruby' if config['options'] if uri =~ /\?/ uri += "&#{paramize(config['options'])}" else uri += "?#{paramize(config['options'])}" end end uri end |