Class: VoucherCode::Config
- Inherits:
-
Object
- Object
- VoucherCode::Config
- Defined in:
- lib/voucher_code/config.rb
Overview
Voucher code configuration
Instance Attribute Summary collapse
-
#charset ⇒ Object
readonly
Returns the value of attribute charset.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
-
#postfix ⇒ Object
readonly
Returns the value of attribute postfix.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Instance Method Summary collapse
- #generate ⇒ Object
-
#initialize(config = {}) ⇒ Config
constructor
A new instance of Config.
Constructor Details
#initialize(config = {}) ⇒ Config
Returns a new instance of Config.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/voucher_code/config.rb', line 15 def initialize(config = {}) @config = config || {} @count = config[:count] || 1 @length = config[:length] || 8 @charset = config[:charset] || select_charset('alphanumeric') @prefix = config[:prefix] || '' @postfix = config[:postfix] || '' @pattern = if config[:pattern].nil? || config[:pattern].empty? looping('#', @length) else config[:pattern] end end |
Instance Attribute Details
#charset ⇒ Object (readonly)
Returns the value of attribute charset.
7 8 9 |
# File 'lib/voucher_code/config.rb', line 7 def charset @charset end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/voucher_code/config.rb', line 7 def config @config end |
#count ⇒ Object (readonly)
Returns the value of attribute count.
7 8 9 |
# File 'lib/voucher_code/config.rb', line 7 def count @count end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
7 8 9 |
# File 'lib/voucher_code/config.rb', line 7 def length @length end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
7 8 9 |
# File 'lib/voucher_code/config.rb', line 7 def pattern @pattern end |
#postfix ⇒ Object (readonly)
Returns the value of attribute postfix.
7 8 9 |
# File 'lib/voucher_code/config.rb', line 7 def postfix @postfix end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
7 8 9 |
# File 'lib/voucher_code/config.rb', line 7 def prefix @prefix end |
Instance Method Details
#generate ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/voucher_code/config.rb', line 30 def generate raise 'Not possible to generate some requested codes.' unless feasible? codes = {} while @count.positive? code = generate_single_data unless codes[code] codes[code] = true @count -= 1 end end codes.keys end |