Class: Keybox::SymbolSetGenerator

Inherits:
StringGenerator show all
Includes:
SymbolSet
Defined in:
lib/keybox/string_generator.rb

Overview

SymbolSetGenerator uses symbol sets and emits a single character from the aggregated symobl sets that the instance is wrapping.

That is When a SymbolSetGenerator is instantiated, it can take a list of symbol sets (a-z, A-Z, 0-9) etc as parameters. Those sets are merged and when ‘emit’ is called a string of length 1 is returned from the aggregated symbols

Constant Summary

Constants included from SymbolSet

Keybox::SymbolSet::ALL, Keybox::SymbolSet::LOWER_ASCII, Keybox::SymbolSet::MAPPING, Keybox::SymbolSet::NUMERAL_ASCII, Keybox::SymbolSet::SPECIAL_ASCII, Keybox::SymbolSet::UPPER_ASCII

Instance Attribute Summary collapse

Attributes inherited from StringGenerator

#autoclear, #chunks, #max_length, #min_length

Instance Method Summary collapse

Methods inherited from StringGenerator

#clear, #generate, #to_s

Constructor Details

#initialize(set = ALL) ⇒ SymbolSetGenerator

Returns a new instance of SymbolSetGenerator.



127
128
129
130
131
# File 'lib/keybox/string_generator.rb', line 127

def initialize(set = ALL)
    super()
    @symbols = set.flatten.uniq
    @required_sets = []
end

Instance Attribute Details

#required_setsObject

Returns the value of attribute required_sets.



125
126
127
# File 'lib/keybox/string_generator.rb', line 125

def required_sets
  @required_sets
end

Instance Method Details

#generate_chunkObject

we force generation of the required sets at the beginning the first time we are called.



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/keybox/string_generator.rb', line 164

def generate_chunk
    chunk = ""
    if required_generated? then
        @chunks << @randomizer.pick_one_from(symbols)
        chunk = @chunks.last
    else
        req = generate_required
        @chunks.concat(req)
        chunk = req.join('')
    end
    return chunk
end

#required_generated?Boolean

Returns:

  • (Boolean)


147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/keybox/string_generator.rb', line 147

def required_generated?
    result = true
    @required_sets.each do |set|
        set_found = false
        @chunks.each do |chunk|
            if set.include?(chunk) then
                set_found = true
                break
            end
        end
        result = (result and set_found)
    end
    return result
end

#symbolsObject

every time we access the symbols set we need to make sure that the required symbols are a part of it, and if they aren’t then make sure they are.



136
137
138
139
140
141
142
143
144
145
# File 'lib/keybox/string_generator.rb', line 136

def symbols
    if @required_sets.size > 0 then
        if not @symbols.include?(@required_sets.first[0]) then
            @symbols << @required_sets
            @symbols.flatten!
            @symbols.uniq!
        end
    end
    @symbols 
end

#valid?Boolean

valid for the symbol set generator means the parent classes validity and that we have in the

Returns:

  • (Boolean)


179
180
181
# File 'lib/keybox/string_generator.rb', line 179

def valid?
    super and required_generated?
end