Class: SolidusFriendlyPromotions::PromotionCode::BatchBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/models/solidus_friendly_promotions/promotion_code/batch_builder.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  random_code_length: 6,
  batch_size: 1000,
  sample_characters: ("a".."z").to_a + (2..9).to_a.map(&:to_s)
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(promotion_code_batch, options = {}) ⇒ BatchBuilder

Returns a new instance of BatchBuilder.



16
17
18
19
20
# File 'app/models/solidus_friendly_promotions/promotion_code/batch_builder.rb', line 16

def initialize(promotion_code_batch, options = {})
  @promotion_code_batch = promotion_code_batch
  options.assert_valid_keys(*DEFAULT_OPTIONS.keys)
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'app/models/solidus_friendly_promotions/promotion_code/batch_builder.rb', line 6

def options
  @options
end

#promotion_code_batchObject (readonly)

Returns the value of attribute promotion_code_batch.



6
7
8
# File 'app/models/solidus_friendly_promotions/promotion_code/batch_builder.rb', line 6

def promotion_code_batch
  @promotion_code_batch
end

Instance Method Details

#build_promotion_codesObject



22
23
24
25
26
27
28
29
30
31
# File 'app/models/solidus_friendly_promotions/promotion_code/batch_builder.rb', line 22

def build_promotion_codes
  generate_random_codes
  promotion_code_batch.update!(state: "completed")
rescue => e
  promotion_code_batch.update!(
    error: e.inspect,
    state: "failed"
  )
  raise e
end