Class: RailsAppGenerator::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_app_generator/context.rb

Overview

Context stores the template folder and options

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rails_template_path, rails_override_template_path, addon_template_path, options = {}) ⇒ Context

Build a context object from the given arguments to be passed around within Rails Generator and AddOns.

Parameters:

  • rails_template_path (String)

    The path to the Rails template.

  • rails_override_template_path (String)

    This is the path to the templates that will override the default Rails templates.

  • addon_template_path (String)

    The path to the addon’s templates.

  • options (Hash) (defaults to: {})

    A hash of options that can be used to customize the template. Defaults to {}



18
19
20
21
22
23
24
# File 'lib/rails_app_generator/context.rb', line 18

def initialize(rails_template_path, rails_override_template_path, addon_template_path, options = {})
  @rails_template_path = rails_template_path
  @rails_override_template_path = rails_override_template_path
  @addon_template_path = addon_template_path

  @options = options
end

Instance Attribute Details

#addon_template_pathObject (readonly)

Returns the value of attribute addon_template_path.



8
9
10
# File 'lib/rails_app_generator/context.rb', line 8

def addon_template_path
  @addon_template_path
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/rails_app_generator/context.rb', line 10

def options
  @options
end

#rails_override_template_pathObject (readonly)

Returns the value of attribute rails_override_template_path.



7
8
9
# File 'lib/rails_app_generator/context.rb', line 7

def rails_override_template_path
  @rails_override_template_path
end

#rails_template_pathObject (readonly)

Returns the value of attribute rails_template_path.



6
7
8
# File 'lib/rails_app_generator/context.rb', line 6

def rails_template_path
  @rails_template_path
end

Instance Method Details

#addonsObject



26
27
28
29
30
# File 'lib/rails_app_generator/context.rb', line 26

def addons
  AddOns.constants.select { |klass| AddOns.const_get(klass).is_a?(Class) }.map { |klass| AddOns.const_get(klass) }
        .map(&:to_s) # .map(&:name) only works in Ruby 3.0+
        .map(&:underscore)
end

#debugObject



39
40
41
42
43
44
45
46
47
# File 'lib/rails_app_generator/context.rb', line 39

def debug
  kv('rails_template_path', rails_template_path)
  kv('rails_override_template_path', rails_override_template_path)
  kv('addon_template_path', addon_template_path)

  debug_default_addons
  debug_addons
  debug_options
end

#default_addonsObject

Skippable parts of the default Rails generator, e.g. active_record, active_job…



33
34
35
36
37
# File 'lib/rails_app_generator/context.rb', line 33

def default_addons
  addon_options = addons.map { |option| "skip_#{option}".to_sym }
  skip_options = RailsAppGenerator::AppGenerator.class_options.keys.grep(/skip_/)
  (skip_options - addon_options).map { |option| option.to_s.delete_prefix('skip_').to_sym }
end