Class: Rigrate::DataSource
- Inherits:
-
Object
- Object
- Rigrate::DataSource
- Defined in:
- lib/rigrate/data_source.rb
Instance Attribute Summary collapse
-
#dbh ⇒ Object
Returns the value of attribute dbh.
Instance Method Summary collapse
-
#initialize(conn_uri) ⇒ DataSource
constructor
A new instance of DataSource.
- #method_missing(mth, *args, &block) ⇒ Object
- #sql(str, *args) ⇒ Object
Constructor Details
#initialize(conn_uri) ⇒ DataSource
Returns a new instance of DataSource.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/rigrate/data_source.rb', line 9 def initialize(conn_uri) uri = URI.parse(conn_uri) klazz = uri.scheme.capitalize begin Module.const_get(klazz) rescue NameError DataSource.load_driver(klazz.downcase) end @dbh = eval(klazz).new conn_uri end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(mth, *args, &block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rigrate/data_source.rb', line 29 def method_missing(mth, *args, &block) begin table_name = mth columns = args str_sql = build_sql(table_name, *columns) @dbh.select(str_sql, &block) rescue Exception => e raise e end end |
Instance Attribute Details
#dbh ⇒ Object
Returns the value of attribute dbh.
7 8 9 |
# File 'lib/rigrate/data_source.rb', line 7 def dbh @dbh end |
Instance Method Details
#sql(str, *args) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/rigrate/data_source.rb', line 20 def sql(str, *args) begin @dbh.select(str, *args) rescue SQLite3::SQLException => e puts "DB: #{@dbh.inspect} SQL: #{str}" raise e end end |