Class: Transfer::Generators::Sequel

Inherits:
Base
  • Object
show all
Defined in:
lib/transfer/generators/sequel.rb

Instance Attribute Summary

Attributes inherited from Base

#klass

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Transfer::Generators::Base

Class Method Details

.supports?(klass) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/transfer/generators/sequel.rb', line 3

def self.supports? klass
  defined?(Sequel) && klass.ancestors.include?(Sequel::Model)
end

Instance Method Details

#afterObject



15
16
17
18
19
20
# File 'lib/transfer/generators/sequel.rb', line 15

def after
  klass.restrict_primary_key
  klass.strict_param_setting = @strict_param_setting
  klass.set_restricted_columns *@restricted_columns if @restricted_columns
  klass.set_allowed_columns *@allowed_columns if @allowed_columns
end

#beforeObject



7
8
9
10
11
12
13
# File 'lib/transfer/generators/sequel.rb', line 7

def before
  klass.unrestrict_primary_key
  @strict_param_setting = klass.strict_param_setting
  klass.strict_param_setting = false
  klass.set_restricted_columns if @restricted_columns = klass.restricted_columns
  klass.set_allowed_columns if @allowed_columns = klass.allowed_columns
end

#column_present?(name) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/transfer/generators/sequel.rb', line 22

def column_present? name
  super name || klass.columns.include?(name.to_sym)
end

#create(attributes, row, options = {}, callbacks = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/transfer/generators/sequel.rb', line 30

def create attributes, row, options={}, callbacks={}
  model = klass.new attributes
  model.instance_exec(row, &callbacks[:before_save]) if callbacks[:before_save]

  save_options = options.select{|key| key == :validate }
  save_options[:raise_on_failure] = true

  model.save save_options
  model.instance_exec(row, &callbacks[:after_save]) if callbacks[:after_save]
  options[:success].call(model, row) if options[:success]
  model
rescue Exception => e
  options[:failure].call(model, row, e) if options[:failure]
  raise Sequel::Rollback if options[:failure_strategy] == :rollback
  model
end

#transaction(&block) ⇒ Object



26
27
28
# File 'lib/transfer/generators/sequel.rb', line 26

def transaction &block
  klass.db.transaction :savepoint => true, &block
end