Class: Spree::PromotionCode::CodeBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/promotion_code/code_builder.rb

Overview

A class responsible for building PromotionCodes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(promotion, base_code, num_codes) ⇒ CodeBuilder

Requres a promotion, base_code and num_codes

promotion Must be a Spree::Promotion. base_code Must be a String. num_codes Must be a positive integer greater than zero.



13
14
15
16
17
# File 'app/models/spree/promotion_code/code_builder.rb', line 13

def initialize promotion, base_code, num_codes
  @base_code = base_code
  @num_codes = num_codes
  @promotion = promotion
end

Instance Attribute Details

#base_codeObject (readonly)

Returns the value of attribute base_code.



3
4
5
# File 'app/models/spree/promotion_code/code_builder.rb', line 3

def base_code
  @base_code
end

#num_codesObject (readonly)

Returns the value of attribute num_codes.



3
4
5
# File 'app/models/spree/promotion_code/code_builder.rb', line 3

def num_codes
  @num_codes
end

#promotionObject (readonly)

Returns the value of attribute promotion.



3
4
5
# File 'app/models/spree/promotion_code/code_builder.rb', line 3

def promotion
  @promotion
end

Instance Method Details

#build_promotion_codesObject

Builds and returns an array of Spree::PromotionCode’s



20
21
22
23
24
# File 'app/models/spree/promotion_code/code_builder.rb', line 20

def build_promotion_codes
  codes.map do |code|
    promotion.codes.build(value: code)
  end
end