Skip to content

Commit

Permalink
Faker::Types minor cleanup (#1362)
Browse files Browse the repository at this point in the history
* Minor code reuse cleanup

* added examples to docs
  • Loading branch information
stephengroat authored and vbrazo committed Sep 14, 2018
1 parent 2ae4fc6 commit a57fe15
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
4 changes: 4 additions & 0 deletions doc/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ Faker::Types.rb_integer #=> 1

# Random Hash (with random keys and values)
Faker::Types.rb_hash #=> {name: "bob"}
Faker::Types.rb_hash(1) #=> {name: "bob"}
Faker::Types.rb_hash(2) #=> {name: "bob", last: "marley"}

# Random Complex Hash (values include other hashes and arrays)
Faker::Types.complex_rb_hash #=> {user: {first: "bob", last: "marley"}}
Faker::Types.complex_rb_hash(1) #=> {user: {first: "bob", last: "marley"}}
Faker::Types.complex_rb_hash(2) #=> {user: {first: "bob", last: "marley"}, son: ["damien", "marley"]}

# Random Array
Faker::Types.rb_array #=> ["a", 1, 2, "bob"]
Expand Down
10 changes: 3 additions & 7 deletions lib/faker/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,16 @@ def rb_integer(from = 0, to = 100)
rand(from..to).to_i
end

def rb_hash(key_count = 1)
def rb_hash(key_count = 1, type = random_type)
{}.tap do |hsh|
Lorem.words(key_count * 2).uniq.first(key_count).each do |s|
hsh.merge!(s.to_sym => random_type)
hsh.merge!(s.to_sym => type)
end
end
end

def complex_rb_hash(key_count = 1)
{}.tap do |hsh|
Lorem.words(key_count * 2).uniq.first(key_count).each do |s|
hsh.merge!(s.to_sym => random_complex_type)
end
end
rb_hash(key_count, random_complex_type)
end

def rb_array(len = 1)
Expand Down

0 comments on commit a57fe15

Please sign in to comment.