1
0
mirror of https://github.com/askn/faker.git synced 2023-08-10 21:13:01 +03:00

Update to Crystal 0.19.4

This commit is contained in:
Serdar Dogruyol 2016-10-20 14:29:47 +03:00
parent fb5c4708f7
commit ae84c91f94
8 changed files with 130 additions and 130 deletions

File diff suppressed because one or more lines are too long

View File

@ -3,7 +3,7 @@ require "./faker/*"
module Faker module Faker
def self.numerify(number_string) def self.numerify(number_string)
number_string = number_string as String number_string = number_string.as String
number_string.sub(/#/) { (rand(9) + 1).to_s }.gsub(/#/) { rand(10).to_s } number_string.sub(/#/) { (rand(9) + 1).to_s }.gsub(/#/) { rand(10).to_s }
end end
@ -18,25 +18,25 @@ module Faker
def self.regexify(re) def self.regexify(re)
re = re.source if re.is_a?(Regex) re = re.source if re.is_a?(Regex)
re.gsub(/^\/?\^?/, "").gsub(/\$?\/?$/, "") # Ditch the anchors re.gsub(/^\/?\^?/, "").gsub(/\$?\/?$/, "") # Ditch the anchors
.gsub(/\{(\d+)\}/) { "{#{$1},#{$1}}" }.gsub(/\?/, "{0,1}") # All {2} become {2,2} and ? become {0,1} .gsub(/\{(\d+)\}/) { "{#{$1},#{$1}}" }.gsub(/\?/, "{0,1}") # All {2} become {2,2} and ? become {0,1}
.gsub(/(\[[^\]]+\])\{(\d+),(\d+)\}/) { |match| $1 * (Range.new($2.to_i, $3.to_i)).to_a.sample } # [12]{1,2} becomes [12] or [12][12] .gsub(/(\[[^\]]+\])\{(\d+),(\d+)\}/) { |match| $1 * (Range.new($2.to_i, $3.to_i)).to_a.sample } # [12]{1,2} becomes [12] or [12][12]
.gsub(/(\([^\)]+\))\{(\d+),(\d+)\}/) { |match| $1 * ($2.to_i..$3.to_i).to_a.sample } # (12|34){1,2} becomes (12|34) or (12|34)(12|34) .gsub(/(\([^\)]+\))\{(\d+),(\d+)\}/) { |match| $1 * ($2.to_i..$3.to_i).to_a.sample } # (12|34){1,2} becomes (12|34) or (12|34)(12|34)
.gsub(/(\\?.)\{(\d+),(\d+)\}/) { |match| $1 * ($2.to_i..$3.to_i).to_a.sample } # A{1,2} becomes A or AA or \d{3} becomes \d\d\d .gsub(/(\\?.)\{(\d+),(\d+)\}/) { |match| $1 * ($2.to_i..$3.to_i).to_a.sample } # A{1,2} becomes A or AA or \d{3} becomes \d\d\d
.gsub(/\((.*?)\)/) { |match| match.gsub(/[\(\)]/, "").split("|").sample } # (this|that) becomes "this" or "that" .gsub(/\((.*?)\)/) { |match| match.gsub(/[\(\)]/, "").split("|").sample } # (this|that) becomes "this" or "that"
.gsub(/\[([^\]]+)\]/) { |match| match.gsub(/(\w\-\w)/) { |range| ((0...range.size).map { |i| range[i].to_s }).join("").split("-").to_a.sample } } # All A-Z inside of [] become C (or X, or whatever) .gsub(/\[([^\]]+)\]/) { |match| match.gsub(/(\w\-\w)/) { |range| ((0...range.size).map { |i| range[i].to_s }).join("").split("-").to_a.sample } } # All A-Z inside of [] become C (or X, or whatever)
.gsub(/\[([^\]]+)\]/) { |match| $1.split("").sample } # All [ABC] become B (or A or C) .gsub(/\[([^\]]+)\]/) { |match| $1.split("").sample } # All [ABC] become B (or A or C)
.gsub("\\d") { |match| (0..9).to_a.sample } .gsub("\\d") { |match| (0..9).to_a.sample }
.gsub("\\w") { |match| (("A".."Z").to_a + (0..9).to_a).sample } .gsub("\\w") { |match| (("A".."Z").to_a + (0..9).to_a).sample }
end end
def self.fetch(data) def self.fetch(data)
data = data as Array data = data.as Array
fetched = data.sample as String fetched = data.sample.as String
if fetched.match(/^\//) && fetched.match(/\/$/) # A regex if fetched.match(/^\//) && fetched.match(/\/$/) # A regex
fetched = Faker.regexify(fetched) fetched = Faker.regexify(fetched)
end end
Faker.parse(fetched) as String Faker.parse(fetched).as String
end end
def self.parse(st) def self.parse(st)

View File

@ -5,7 +5,7 @@ module Faker
end end
def self.credit_card_expiry_date def self.credit_card_expiry_date
credit_card_expiry_date = Faker.fetch(Data["business"]["credit_card_expiry_dates"]) as String credit_card_expiry_date = Faker.fetch(Data["business"]["credit_card_expiry_dates"]).as String
Time.parse(credit_card_expiry_date, "%Y-%m-%d") Time.parse(credit_card_expiry_date, "%Y-%m-%d")
end end

View File

@ -18,12 +18,12 @@ module Faker
end end
def self.material def self.material
product_name = Data["commerce"]["product_name"] as Hash product_name = Data["commerce"]["product_name"].as Hash
Faker.fetch(product_name["material"]) Faker.fetch(product_name["material"])
end end
def self.product_name def self.product_name
product_name = Data["commerce"]["product_name"] as Hash product_name = Data["commerce"]["product_name"].as Hash
Faker.fetch(product_name["adjective"]) + " " + Faker.fetch(product_name["material"]) + " " + Faker.fetch(product_name["product"]) Faker.fetch(product_name["adjective"]) + " " + Faker.fetch(product_name["material"]) + " " + Faker.fetch(product_name["product"])
end end

View File

@ -9,12 +9,12 @@ module Faker
end end
def self.catch_phrase def self.catch_phrase
data = Data["company"]["buzzwords"] as Array(Array(String)) data = Data["company"]["buzzwords"].as Array(Array(String))
Faker.fetch(data.flatten) Faker.fetch(data.flatten)
end end
def self.bs def self.bs
data = Data["company"]["bs"] as Array(Array(String)) data = Data["company"]["bs"].as Array(Array(String))
Faker.fetch(data.flatten) Faker.fetch(data.flatten)
end end

View File

@ -1,6 +1,6 @@
module Faker module Faker
class Finance class Finance
CREDIT_CARD_TYPES = (Data["credit_card"] as Hash).keys CREDIT_CARD_TYPES = (Data["credit_card"].as(Hash)).keys
def self.credit_card(types : Array = [] of Array(String)) def self.credit_card(types : Array = [] of Array(String))
types = CREDIT_CARD_TYPES if types.empty? types = CREDIT_CARD_TYPES if types.empty?

View File

@ -11,13 +11,13 @@ module Faker
end end
def self.word def self.word
words = Data["lorem"]["words"] as Array words = Data["lorem"]["words"].as Array
Faker.fetch(words) Faker.fetch(words)
end end
def self.words(num = 3, supplemental = false) def self.words(num = 3, supplemental = false)
words = Data["lorem"]["words"] as Array(String) words = Data["lorem"]["words"].as Array(String)
words += (Data["lorem"]["supplemental"] as Array(String)) if supplemental words += (Data["lorem"]["supplemental"].as Array(String)) if supplemental
words.shuffle[0, num] words.shuffle[0, num]
end end

View File

@ -7,7 +7,7 @@ module Faker
{% end %} {% end %}
def self.title def self.title
title = Data["name"]["title"] as Hash title = Data["name"]["title"].as Hash
Faker.fetch(title["descriptor"]) + " " + Faker.fetch(title["level"]) + " " + Faker.fetch(title["job"]) Faker.fetch(title["descriptor"]) + " " + Faker.fetch(title["level"]) + " " + Faker.fetch(title["job"])
end end
end end