Class: VoucherCode::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/voucher_code/config.rb

Overview

Voucher code configuration

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#charsetObject (readonly)

Returns the value of attribute charset.



7
8
9
# File 'lib/voucher_code/config.rb', line 7

def charset
  @charset
end

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/voucher_code/config.rb', line 7

def config
  @config
end

#countObject (readonly)

Returns the value of attribute count.



7
8
9
# File 'lib/voucher_code/config.rb', line 7

def count
  @count
end

#lengthObject (readonly)

Returns the value of attribute length.



7
8
9
# File 'lib/voucher_code/config.rb', line 7

def length
  @length
end

#patternObject (readonly)

Returns the value of attribute pattern.



7
8
9
# File 'lib/voucher_code/config.rb', line 7

def pattern
  @pattern
end

#postfixObject (readonly)

Returns the value of attribute postfix.



7
8
9
# File 'lib/voucher_code/config.rb', line 7

def postfix
  @postfix
end

#prefixObject (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

#generateObject



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