Class: ZanTools::Seed

Inherits:
Object
  • Object
show all
Defined in:
lib/zan_tools/seed.rb

Constant Summary collapse

TEXTS =
%Q{12月6日,在结束对西班牙、阿根廷、巴拿马、葡萄牙国事访问并出席二十国集团领导人第十三次峰会后,国家主席习近平回到北京。}.gsub(/[,、。]/, '')

Class Method Summary collapse

Class Method Details

.generate(fields) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/zan_tools/seed.rb', line 9

def self.generate(fields)
  Hash[fields.map do |name, conf|
    sample = case conf['type'].upper_camel
      when 'Long' then rand(0..1000)
      when 'String' then guess_string(name)
      when 'Date' then DateTime.now.to_s
      when 'Integer' then guess_integer(name)
      when 'Double' then (10000*rand()).to_i/100.0
      else
        conf['type']
      end
    [name, sample]
  end]
end

.generate_json(fields) ⇒ Object



44
45
46
# File 'lib/zan_tools/seed.rb', line 44

def self.generate_json(fields)
  JSON.generate(generate(fields))
end

.guess_integer(name) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/zan_tools/seed.rb', line 35

def self.guess_integer(name)
  case name.to_s
  when /^(is|has)[_A-Z]/
    rand(0..1)
  else
    rand(0..10)
  end
end

.guess_string(name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/zan_tools/seed.rb', line 24

def self.guess_string(name)
  case name.to_s.downcase
  when /name|title|content|body|desc|comment/
    TEXTS[rand(0 ... TEXTS.size/2) .. rand(TEXTS.size/2 ... TEXTS.size)]
  when /url/i
    "http://www.qima-inc.com/"
  else
    (0.. rand(5..10)).map { (97 + rand(26)).chr }.join
  end
end