Class: Cdigits::Luhn::Placeholder

Inherits:
Object
  • Object
show all
Defined in:
lib/cdigits/luhn/placeholder.rb

Constant Summary collapse

NON_ZERO_SYMBOL =
'+'
NUMERIC_SYMBOL =
'#'
CHECK_DIGIT_SYMBOL =
Note:

any good idia?

'?'

Instance Method Summary collapse

Constructor Details

#initialize(characters) ⇒ Placeholder

Returns a new instance of Placeholder.

Parameters:

  • characters (Array<String>)


14
15
16
# File 'lib/cdigits/luhn/placeholder.rb', line 14

def initialize(characters)
  @characters = characters
end

Instance Method Details

#fill(placeholder) ⇒ String

Generate code

Parameters:

  • placeholder (String)

Returns:

  • (String)


21
22
23
24
25
26
27
28
29
# File 'lib/cdigits/luhn/placeholder.rb', line 21

def fill(placeholder)
  codes = []
  store = build_store(placeholder) do |char, digit|
    codes << (digit.nil? ? char : nil)
  end

  store.fill_check_digit
  generate_code(codes, store.digits)
end

#valid?(placeholder) ⇒ Boolean

Validate code

Parameters:

  • placeholder (String)

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/cdigits/luhn/placeholder.rb', line 34

def valid?(placeholder)
  store = build_store(placeholder)
  (store.sum % modulus).zero?
end