Created
October 13, 2017 23:23
-
-
Save aren55555/4eca83025be26b41ccc52a9fbf100165 to your computer and use it in GitHub Desktop.
Dima Example of Optional Args
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pp' | |
require 'securerandom' | |
require 'faker' | |
def generate_car(opts={}) | |
type = opts[:car] || "car" | |
id = opts[:id] || SecureRandom.uuid | |
attrs = opts[:json_attrs] || {} | |
attributes = { | |
make: Faker::Company.name, | |
model: Faker::Commerce.product_name, | |
year: (1980...2017).to_a.sample, | |
}.merge(attrs) | |
{ | |
"data": { | |
"id": id, | |
"type": type, | |
"attributes": attributes.merge(attrs), | |
} | |
} | |
end | |
c1 = generate_car() | |
c2 = generate_car(json_attrs: {make: "Ford"}) | |
pp c1 | |
pp c2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment