class Reflekt::StringRule

Attributes

max_length[RW]
min_length[RW]

Public Class Methods

new() click to toggle source
# File lib/rules/string_rule.rb, line 9
def initialize()
  @type = :string
  @min_length = nil
  @max_length = nil
end

Public Instance Methods

random() click to toggle source
# File lib/rules/string_rule.rb, line 53
def random()
  # Pour alphabet soup.
  alpha_numeric = Array('A'..'Z') + Array('a'..'z')
  10.times do
    alpha_numeric << ' '
  end

  # Dip ladle into alphabet soup.
  last_char = nil
  sentence = Array.new(rand(@min_length..@max_length)) do |index|
    char = alpha_numeric.sample
    # Put no character next to the same character twice.
    while char == last_char
      char = alpha_numeric.sample
    end
    last_char = char
  end

  return sentence.join
end
result() click to toggle source
# File lib/rules/string_rule.rb, line 45
def result()
  {
    :type => @type,
    :min_length => @min_length,
    :max_length => @max_length
  }
end
test(value) click to toggle source

@param value [String]

# File lib/rules/string_rule.rb, line 37
def test(value)
  length = value.length

  return false if length < @min_length
  return false if length > @max_length
  true
end
train(meta) click to toggle source

@param meta [StringMeta]

# File lib/rules/string_rule.rb, line 18
def train(meta)
  length = meta[:length]

  if @min_length.nil?
    @min_length = length
  else
    @min_length = length if length < @min_length
  end

  if @max_length.nil?
    @max_length = length
  else
    @max_length = length if length > @max_length
  end
end