Sha256: b439256d66ec9d11eb2b5ac1d02f40f586ae4951efb52c2483d8cf4f5ffde796

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

require 'date'
require 'zan_tools/string'
require 'json'

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

    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

    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

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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
zan_tools-0.1.3 lib/zan_tools/seed.rb
zan_tools-0.1.2 lib/zan_tools/seed.rb
zan_tools-0.1.1 lib/zan_tools/seed.rb
zan_tools-0.1.0 lib/zan_tools/seed.rb