Class: ActiveExchanger::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_exchanger/base.rb

Direct Known Subclasses

AgroSystemes::SoilAnalysesExchanger, AgroSystemes::WaterAnalysesExchanger, BovinsCroissance::CattlePerformanceControlsExchanger, CharentesAlliance::IncomingDeliveriesExchanger, CharentesAlliance::OutgoingDeliveriesExchanger, EBP::EDIExchanger, Ekylibre::ActivitiesExchanger, Ekylibre::AnimalGroupsExchanger, Ekylibre::AnimalsExchanger, Ekylibre::BackupExchanger, Ekylibre::BankStatementsExchanger, Ekylibre::BudgetsExchanger, Ekylibre::BuildingsJsonExchanger, Ekylibre::CultivableZonesExchanger, Ekylibre::CultivableZonesJsonExchanger, Ekylibre::EntitiesExchanger, Ekylibre::EquipmentsExchanger, Ekylibre::EquipmentsJsonExchanger, Ekylibre::GeoreadingsExchanger, Ekylibre::IncomingPaymentsExchanger, Ekylibre::InterventionsExchanger, Ekylibre::LandParcelsExchanger, Ekylibre::MattersExchanger, Ekylibre::OutgoingPaymentsExchanger, Ekylibre::PicturesExchanger, Ekylibre::PlantsExchanger, Ekylibre::PurchasesExchanger, Ekylibre::SalesExchanger, Ekylibre::SettingsExchanger, Ekylibre::VariantsExchanger, Ekylibre::VisualsExchanger, Ekylibre::WorkersExchanger, Ekylibre::ZonesExchanger, FIEA::GalacteaExchanger, Isagri::Isacompta::ExportExchanger, Isagri::Isaculture::CsvImportExchanger, LaGraineInformatique::Vinifera::EntitiesExchanger, LaGraineInformatique::Vinifera::ProductsExchanger, LaGraineInformatique::Vinifera::SalesExchanger, Legrain::Epicea::AccountsExchanger, Legrain::Epicea::JournalsExchanger, Lilco::MilkAnalysesExchanger, Milklic::IndividualProductionExchanger, Synel::AnimalsExchanger, Synel::InventoryExchanger, Telepac::V2014::CapLandParcelsExchanger, Telepac::V2014::LandParcelsExchanger, Telepac::V2015::LandParcelsExchanger, UPRA::ReproductorsExchanger, Unicoque::OutgoingDeliveriesExchanger, Unicoque::PlantZonesExchanger, Viniteca::InterventionsExchanger, Viniteca::PlantZonesExchanger

Constant Summary collapse

@@exchangers =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, supervisor) ⇒ Base

Returns a new instance of Base.


105
106
107
108
# File 'lib/active_exchanger/base.rb', line 105

def initialize(file, supervisor)
  @file = Pathname.new(file)
  @supervisor = supervisor
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file


103
104
105
# File 'lib/active_exchanger/base.rb', line 103

def file
  @file
end

#supervisorObject (readonly) Also known as: w

Returns the value of attribute supervisor


103
104
105
# File 'lib/active_exchanger/base.rb', line 103

def supervisor
  @supervisor
end

Class Method Details

.build(nature, file, _options = {}, &block) ⇒ Object


81
82
83
84
# File 'lib/active_exchanger/base.rb', line 81

def build(nature, file, _options = {}, &block)
  supervisor = Supervisor.new(&block)
  find(nature).new(file, supervisor)
end

.check(nature, file, _options = {}, &block) ⇒ Object


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/active_exchanger/base.rb', line 62

def check(nature, file, _options = {}, &block)
  supervisor = Supervisor.new(:check, &block)
  exchanger = find(nature).new(file, supervisor)
  valid = false
  ActiveRecord::Base.transaction do
    if exchanger.respond_to? :check
      exchanger.check
    else
      exchanger.import
    end
    valid = true
    raise ActiveRecord::Rollback
  end
  GC.start
  valid
rescue
  false
end

.check_by_defaultObject

This method check file by default by trying a run and and if no exception raise, it's fine so changes are rolled back.


96
97
98
99
100
# File 'lib/active_exchanger/base.rb', line 96

def check_by_default
  define_method :check do
    import
  end
end

.exchanger_nameObject


14
15
16
# File 'lib/active_exchanger/base.rb', line 14

def exchanger_name
  name.to_s.underscore.gsub(/_exchanger$/, '').tr('/', '_').to_sym
end

.export(nature, file, options = {}, &block) ⇒ Object


58
59
60
# File 'lib/active_exchanger/base.rb', line 58

def export(nature, file, options = {}, &block)
  build(nature, file, options, &block).export
end

.exportersObject


34
35
36
# File 'lib/active_exchanger/base.rb', line 34

def exporters
  @@exchangers.select { |_k, v| v.method_defined?(:export) }
end

.find(nature) ⇒ Object


86
87
88
89
90
91
92
# File 'lib/active_exchanger/base.rb', line 86

def find(nature)
  klass = @@exchangers[nature.to_sym]
  unless klass
    raise "Unable to find exchanger #{nature.inspect}. (#{@@exchangers.keys.to_sentence(locale: :eng)})"
  end
  klass
end

.human_nameObject


18
19
20
# File 'lib/active_exchanger/base.rb', line 18

def human_name
  "exchangers.#{exchanger_name}".t
end

.import(nature, file, options = {}, &block) ⇒ Object

Import file with check if possible


44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_exchanger/base.rb', line 44

def import(nature, file, options = {}, &block)
  exchanger = build(nature, file, options, &block)
  if exchanger.respond_to? :check
    if exchanger.check
      exchanger.import
    else
      Rails.logger.warn 'Cannot import file'
      return false
    end
  else
    exchanger.import
  end
end

.import!(nature, file, options = {}, &block) ⇒ Object

Import file without check


39
40
41
# File 'lib/active_exchanger/base.rb', line 39

def import!(nature, file, options = {}, &block)
  build(nature, file, options, &block).import
end

.importersObject


26
27
28
# File 'lib/active_exchanger/base.rb', line 26

def importers
  @@exchangers.select { |_k, v| v.method_defined?(:import) }
end

.importers_selectionObject


30
31
32
# File 'lib/active_exchanger/base.rb', line 30

def importers_selection
  importers.collect { |i, e| [e.human_name, i] }.sort { |a, b| a.first <=> b.first }
end

.inherited(subclass) ⇒ Object


8
9
10
11
12
# File 'lib/active_exchanger/base.rb', line 8

def inherited(subclass)
  name = subclass.exchanger_name
  ActiveExchanger::Base.register_exchanger(subclass)
  super
end

.register_exchanger(klass) ⇒ Object


22
23
24
# File 'lib/active_exchanger/base.rb', line 22

def register_exchanger(klass)
  @@exchangers[klass.exchanger_name] = klass
end