class FactoryHelper::MySQL

created for MySQL 5.0 dev.mysql.com/doc/refman/5.0/en/numeric-types.html

Constants

ArgumentError
INTEGERS

Public Class Methods

bigint(opt= {}) click to toggle source
# File lib/factory-helper/my_sql.rb, line 53
def bigint opt= {}
  [ integer(opt),
    int_with_edge_cases(int_limits_for :bigint, opt),
  ].sample(:random => FactoryHelper::Config.random)
end
int(opt= {})
Alias for: integer
integer(opt= {}) click to toggle source
# File lib/factory-helper/my_sql.rb, line 47
def integer opt= {}
  [ mediumint(opt),
    int_with_edge_cases(int_limits_for :integer, opt),
  ].sample(:random => FactoryHelper::Config.random)
end
Also aliased as: int
mediumint(opt= {}) click to toggle source
# File lib/factory-helper/my_sql.rb, line 41
def mediumint opt= {}
  [ smallint(opt),
    int_with_edge_cases(int_limits_for :mediumint, opt),
  ].sample(:random => FactoryHelper::Config.random)
end
smallint(opt= {}) click to toggle source
# File lib/factory-helper/my_sql.rb, line 35
def smallint opt= {}
  [ tinyint(opt),
    int_with_edge_cases(int_limits_for :smallint, opt),
  ].sample(:random => FactoryHelper::Config.random)
end
tinyint(opt= {}) click to toggle source
# File lib/factory-helper/my_sql.rb, line 30
def tinyint opt= {}
  int_sanitize opt
  int_with_edge_cases(int_limits_for :tinyint, opt)
end

Private Class Methods

int_edge_case_for(opt= {:min => -128, :max => 127}) click to toggle source
# File lib/factory-helper/my_sql.rb, line 74
def int_edge_case_for opt= {:min => -128, :max => 127}
  [-1, 1, 0, opt[:min], opt[:max]].keep_if do |element|
    (opt[:min].. opt[:max]).include?(element)
  end.sample(:random => FactoryHelper::Config.random)
end
int_limits_for(datatype= :integer, opt= {}) click to toggle source
# File lib/factory-helper/my_sql.rb, line 70
def int_limits_for datatype= :integer, opt= {}
  INTEGERS[datatype][!!opt[:unsigned]].merge(opt)
end
int_sanitize(opt) click to toggle source
# File lib/factory-helper/my_sql.rb, line 80
def int_sanitize opt
  raise ArgumentError, 'Argument list must be a hash.' unless opt.kind_of? Hash
  raise ArgumentError, 'Must specify a min and a max, or neither.' if !!opt[:min]^ !!opt[:max]
  raise ArgumentError, '`:min` must reference an Integer' if opt[:min]&& !opt[:min].kind_of?(Integer)
  raise ArgumentError, '`:max` must reference an Integer' if opt[:max]&& !opt[:max].kind_of?(Integer)
  raise ArgumentError, "Minimum(#{opt[:min]}) must not be less than zero when unsigned" if (opt[:unsigned]&& opt[:min]&& opt[:min]< 0)
  raise ArgumentError, "Minimum(#{opt[:min]}) must be less than Maximum(#{opt[:max]})" if opt[:min]&& opt[:max]&& (opt[:max]< opt[:min])
end
int_with_edge_cases(opt= {:min => -128, :max => 127}) click to toggle source
# File lib/factory-helper/my_sql.rb, line 63
def int_with_edge_cases opt= {:min => -128, :max => 127}
  [ int_edge_case_for(opt),
    FactoryHelper::Config.random.rand(opt[:min].. opt[:max]),
    FactoryHelper::Config.random.rand(opt[:min].. opt[:max]),
  ].sample(:random => FactoryHelper::Config.random)
end