Class: Outbox::Generators::ItemGenerator

Inherits:
NamedBase
  • Object
show all
Defined in:
lib/generators/outbox/item/item_generator.rb

Constant Summary

Constants included from Helpers::Values

Helpers::Values::VALUES_PATH

Constants included from Helpers::Paas

Helpers::Paas::APP_MANIFEST_PATH

Constants included from Helpers::Initializer

Helpers::Initializer::OUTBOX_INITIALIZER_PATH

Constants included from Helpers::Config

Helpers::Config::CONFIG_PATH

Instance Method Summary collapse

Methods included from Helpers::Items

#item_path, #namespaced_item_class_name

Instance Method Details

#check!Object



23
24
25
# File 'lib/generators/outbox/item/item_generator.rb', line 23

def check!
  check_config!
end

#check_kind!Object

Raises:

  • (Rails::Generators::Error)


16
17
18
19
20
21
# File 'lib/generators/outbox/item/item_generator.rb', line 16

def check_kind!
  return if options[:kind].in?(%w[inbox outbox])

  raise Rails::Generators::Error, "Unknown item kind. " \
                                  "Please specify `--kind inbox` or `--kind outbox`"
end

#create_migrationObject



39
40
41
42
43
# File 'lib/generators/outbox/item/item_generator.rb', line 39

def create_migration
  return if options[:skip_migration]

  create_migration_file(migration_class_name, migration_table_name)
end

#create_modelObject



45
46
47
48
49
# File 'lib/generators/outbox/item/item_generator.rb', line 45

def create_model
  return if options[:skip_model]

  template "#{options[:kind]}_item.rb", File.join("app/models", "#{item_path}.rb")
end

#patch_configObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/generators/outbox/item/item_generator.rb', line 51

def patch_config
  return if options[:skip_config]

  item_template_data = if options[:kind] == "inbox"
    <<~RUBY
      #{item_path}:
        partition_size: 1
        partition_strategy: hash
        retention: P1W
        max_retries: 7
        retry_strategies:
          - exponential_backoff
      #  # see README to learn more about transport configuration
      #  transports: {}
    RUBY
  else
    <<~RUBY
      #{item_path}:
        partition_size: 1
        partition_strategy: number
        retention: P3D
        max_retries: 7
        retry_strategies:
          - exponential_backoff
      #  # see README to learn more about transport configuration
      #  transports: {}
    RUBY
  end

  add_item_to_config("#{options[:kind]}_items", item_template_data)
end

#patch_valuesObject



83
84
85
86
87
88
89
90
91
# File 'lib/generators/outbox/item/item_generator.rb', line 83

def patch_values
  return if options[:skip_values]
  return unless paas_app?

  # e.g. order/inbox_item => inbox-order-inbox-items
  deployment_name = "#{options[:kind]}-" + dasherize_item(item_path)

  add_item_to_values(deployment_name.pluralize, item_path)
end

#validate_item_nameObject

Raises:

  • (Rails::Generators::Error)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/generators/outbox/item/item_generator.rb', line 27

def validate_item_name
  return if file_name.underscore.match?(%r{#{options[:kind]}_item$})

  continue = yes?(
    "Warning: your item's name doesn't match the conventional" \
    " name format (i.e. Some#{options[:kind].camelize}Item). Continue?"
  )
  return if continue

  raise Rails::Generators::Error, "Aborting"
end