Class: Ethel::Targets::Sequel
- Inherits:
-
Target
- Object
- Target
- Ethel::Targets::Sequel
- Defined in:
- lib/ethel/targets/sequel.rb
Instance Attribute Summary collapse
-
#force ⇒ Object
Returns the value of attribute force.
-
#import_limit ⇒ Object
Returns the value of attribute import_limit.
Instance Method Summary collapse
- #add_field(field) ⇒ Object
- #add_row(row) ⇒ Object
- #flush ⇒ Object
-
#initialize(table_name, *args) ⇒ Sequel
constructor
A new instance of Sequel.
- #prepare ⇒ Object
Constructor Details
#initialize(table_name, *args) ⇒ Sequel
Returns a new instance of Sequel.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ethel/targets/sequel.rb', line 6 def initialize(table_name, *args) super @database = if args.length == 1 && args[0].kind_of?(::Sequel::Database) args[0] else ::Sequel.connect(*args) end @table_name = table_name @fields = [] @rows = [] @force = false @import_limit = 10_000 end |
Instance Attribute Details
#force ⇒ Object
Returns the value of attribute force.
4 5 6 |
# File 'lib/ethel/targets/sequel.rb', line 4 def force @force end |
#import_limit ⇒ Object
Returns the value of attribute import_limit.
4 5 6 |
# File 'lib/ethel/targets/sequel.rb', line 4 def import_limit @import_limit end |
Instance Method Details
#add_field(field) ⇒ Object
24 25 26 |
# File 'lib/ethel/targets/sequel.rb', line 24 def add_field(field) @fields << field end |
#add_row(row) ⇒ Object
52 53 54 55 56 |
# File 'lib/ethel/targets/sequel.rb', line 52 def add_row(row) @rows << row flush if @rows.length >= @import_limit end |
#flush ⇒ Object
58 59 60 61 62 63 |
# File 'lib/ethel/targets/sequel.rb', line 58 def flush dataset = @database[@table_name] keys = @fields.collect { |field| field.name.to_sym } dataset.import(keys, @rows.collect { |r| r.values_at(*keys) }) @rows.clear end |
#prepare ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ethel/targets/sequel.rb', line 28 def prepare exists = @database.tables.include?(@table_name) if exists && !@force raise "Table #{@table_name} already exists" else generator = @database.create_table_generator @fields.each do |field| type = case field.type when :integer Integer when :string String end generator.column(field.name, type) end if exists @database.create_table!(@table_name, generator) else @database.create_table(@table_name, generator) end end end |