Class: Tachyon
- Inherits:
-
Object
- Object
- Tachyon
- Defined in:
- lib/tachyon.rb,
lib/tachyon/version.rb
Constant Summary collapse
- VERSION =
"0.2.1"
- @@connection_cache =
{}
Class Method Summary collapse
- .connection_for(klass) ⇒ Object
- .dump(record) ⇒ Object
- .dump_attribute(attribute) ⇒ Object
- .insert(klass, data) ⇒ Object
- .quote_data(data) ⇒ Object
- .quote_value(value) ⇒ Object
- .sql_for(klass, data) ⇒ Object
Class Method Details
.connection_for(klass) ⇒ Object
14 15 16 17 |
# File 'lib/tachyon.rb', line 14 def connection_for(klass) return @@connection_cache[klass] if @@connection_cache.has_key?(klass) @@connection_cache[klass] = klass.connection end |
.dump(record) ⇒ Object
38 39 40 41 42 |
# File 'lib/tachyon.rb', line 38 def dump(record) record.attributes_before_type_cast.map do |key, value| [key.to_sym, dump_attribute(value)] end.to_h end |
.dump_attribute(attribute) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/tachyon.rb', line 44 def dump_attribute(attribute) case attribute when Time then attribute.to_s(:db) when Date then attribute.to_s(:db) when TrueClass then 1 when FalseClass then 0 else attribute end end |
.insert(klass, data) ⇒ Object
8 9 10 11 12 |
# File 'lib/tachyon.rb', line 8 def insert(klass, data) connection_for(klass).execute(sql_for(klass, data)) rescue ActiveRecord::RecordNotUnique # NO OP end |
.quote_data(data) ⇒ Object
26 27 28 |
# File 'lib/tachyon.rb', line 26 def quote_data(data) data.map {|value| quote_value(value) } end |
.quote_value(value) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/tachyon.rb', line 30 def quote_value(value) case value when String then "'#{value.gsub("'", "''")}'" when NilClass then "NULL" else value end end |
.sql_for(klass, data) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/tachyon.rb', line 19 def sql_for(klass, data) columns = "`" + data.keys.join("`, `") + "`" values = quote_data(data.values).join(", ") "INSERT INTO `#{klass.table_name}` (#{columns}) VALUES (#{values})" end |