Class: ActiveExchanger::Base

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

Direct Known Subclasses

AgroSystemes::SoilAnalysesExchanger, AgroSystemes::WaterAnalysesExchanger, Agroedi::DaplosExchanger, BovinsCroissance::CattlePerformanceControlsExchanger, Caj::IncomingDeliveriesExchanger, CharentesAlliance::IncomingDeliveriesExchanger, CharentesAlliance::OutgoingDeliveriesExchanger, EBP::EDIExchanger, Ekylibre::AccountsExchanger, Ekylibre::ActivitiesExchanger, Ekylibre::AnimalGroupsExchanger, Ekylibre::AnimalsExchanger, Ekylibre::BackupExchanger, Ekylibre::BankStatementsExchanger, Ekylibre::BudgetsExchanger, Ekylibre::BuildingsJsonExchanger, Ekylibre::CashTransfertsExchanger, 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::Isacompta::FixedAssetsExchanger, Isagri::Isacompta::JournalEntriesExchanger, Isagri::Isaculture::CsvImportExchanger, LaGraineInformatique::Vinifera::EntitiesExchanger, LaGraineInformatique::Vinifera::ProductsExchanger, LaGraineInformatique::Vinifera::SalesExchanger, Legrain::Epicea::AccountsExchanger, Legrain::Epicea::JournalsExchanger, LelyMilkRobot::IndividualProductionExchanger, Lilco::MilkAnalysesExchanger, Milklic::IndividualProductionExchanger, Synel::AnimalsExchanger, Synel::InventoryExchanger, Synest::InventoryExchanger, Synest::MilkControlExchanger, Telepac::V2014::CapLandParcelsExchanger, Telepac::V2014::LandParcelsExchanger, Telepac::V2015::LandParcelsExchanger, UPRA::ReproductorsExchanger, Unicoque::OutgoingDeliveriesExchanger, Unicoque::PlantZonesExchanger, Viniteca::InterventionsExchanger, Viniteca::PlantZonesExchanger, Vivescia::PurchaseExchanger

Constant Summary collapse

@@exchangers =
{}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, supervisor, options = {}) ⇒ Base

Returns a new instance of Base.


125
126
127
128
# File 'lib/active_exchanger/base.rb', line 125

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

Class Attribute Details

.deprecatedObject

Returns the value of attribute deprecated


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

def deprecated
  @deprecated
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file


123
124
125
# File 'lib/active_exchanger/base.rb', line 123

def file
  @file
end

#supervisorObject (readonly) Also known as: w

Returns the value of attribute supervisor


123
124
125
# File 'lib/active_exchanger/base.rb', line 123

def supervisor
  @supervisor
end

Class Method Details

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


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

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

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


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/active_exchanger/base.rb', line 76

def check(file, _options = {}, &block)
  supervisor = Supervisor.new(:check, &block)
  exchanger = 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.


116
117
118
119
120
# File 'lib/active_exchanger/base.rb', line 116

def check_by_default
  define_method :check do
    import
  end
end

.deprecated?Boolean

Returns:

  • (Boolean)

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

def deprecated?
  deprecated
end

.exchanger_nameObject


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

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

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


72
73
74
# File 'lib/active_exchanger/base.rb', line 72

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

.exportersObject


44
45
46
# File 'lib/active_exchanger/base.rb', line 44

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

.find(nature) ⇒ Object


100
101
102
103
104
105
106
# File 'lib/active_exchanger/base.rb', line 100

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

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


48
49
50
# File 'lib/active_exchanger/base.rb', line 48

def find_and_import(nature, file, options = {}, &block)
  find(nature).import(file, options, &block)
end

.find_by(nature) ⇒ Object


108
109
110
111
112
# File 'lib/active_exchanger/base.rb', line 108

def find_by(nature)
  find nature
rescue
  nil
end

.human_nameObject


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

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

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

Import file with check if possible


58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/active_exchanger/base.rb', line 58

def import(file, options = {}, &block)
  exchanger = build(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!(file, options = {}, &block) ⇒ Object

Import file without check


53
54
55
# File 'lib/active_exchanger/base.rb', line 53

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

.importersObject


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

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

.importers_selection(options = {}) ⇒ Object


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

def importers_selection(options = {})
  list = importers
  list = list.reject { |_, v| v.deprecated? } unless options[:with_deprecated]
  list.collect { |i, e| [e.human_name, i] }.sort_by { |a| a.first.lower_ascii }
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


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

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