Module: Ekylibre::FirstRun

Defined in:
lib/ekylibre/first_run.rb,
lib/ekylibre/first_run/base.rb,
lib/ekylibre/first_run/faker.rb,
lib/ekylibre/first_run/folder.rb,
lib/ekylibre/first_run/booker.rb,
lib/ekylibre/first_run/counter.rb

Defined Under Namespace

Modules: Faker Classes: Base, Booker, Counter, Folder

Class Method Summary collapse

Class Method Details

.launch(options = {}) ⇒ Object

Only run first-run in current tenant Options are:

- path:   Full path of the first run folder
- folder: Name of the folder expected to be in db/first_runs
- name:   Name of the tenant to create. No tenant created if no name.
- max:    Max iteration per counter
- mode:   (hard|nil)
- verbose

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ekylibre/first_run.rb', line 34

def launch(options = {})
  if options[:path] && options[:folder]
    raise ArgumentError, ':path and :folder options are incompatible'
  end
  name = options.delete(:name)
  if path = options.delete(:path)
    path = Pathname.new(path)
  elsif options[:folder]
    path = Ekylibre::FirstRun.path.join(options.delete(:folder))
  else
    raise ArgumentError, 'Need at least :path or :folder option'
  end
  manifest = YAML.load_file(path.join('manifest.yml'))
  sentence = 'Launch first run'
  sentence << " in tenant #{name}" if name
  sentence << " from #{path.relative_path_from(Rails.root)}"
  max = options[:max].to_i
  sentence << (max > 0 ? " with max of #{max}" : ' without max')
  sentence << (options[:hard] ? ' without global transaction' : ' inside global transaction')
  sentence << '.'
  Rails.logger.info sentence
  puts sentence.yellow if options[:verbose]
  secure_tenant(name) do
    if manifest['version'] == 2
      Folder.new(path, options).run
    else
      Base.new(path, options).run
    end
  end
end

.launch!(options = {}) ⇒ Object

Launch a first-run with tenant creation Name of tenant is found with option :name or with path or folder options if missing


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ekylibre/first_run.rb', line 14

def launch!(options = {})
  if options[:path]
    options[:name] ||= File.basename(options[:path]).to_s
  else
    options[:folder] ||= options[:name]
    options[:folder] ||= 'demo' if Ekylibre::FirstRun.path.join('demo').exist?
    options[:folder] ||= 'default' if Ekylibre::FirstRun.path.join('default').exist?
    options[:name] ||= options[:folder]
  end
  launch(options)
end

.pathObject

Returns the default path where first_runs data are expected


66
67
68
# File 'lib/ekylibre/first_run.rb', line 66

def path
  Rails.root.join('db', 'first_runs')
end

.secure_tenant(name = nil, &_block) ⇒ Object

Wrap code in a tenant creation transaction if wanted


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ekylibre/first_run.rb', line 71

def secure_tenant(name = nil, &_block)
  if name
    begin
      Ekylibre::Tenant.check!(name)
      Ekylibre::Tenant.create(name) unless Ekylibre::Tenant.exist?(name)
      Ekylibre::Tenant.switch(name) do
        yield
      end
    ensure
      Ekylibre::Tenant.check!(name)
    end
  else
    yield
  end
end