README.md in faker_maker-1.1.2 vs README.md in faker_maker-1.1.3

- old
+ new

@@ -138,12 +138,37 @@ Faker::Commerce.product_name end end ``` +### Organising dependencies + +Factory definition files are Plain Ol' Ruby. If you depend on another factory because you either extend from it or use it just add a `require` or (depending on your load path) `require_relative` to the top of your file. + ### JSON field names +JavaScript likes to use camelCase, Ruby's idiom is to use snake_case. This can make make manipulating factory-built objects in ruby ugly. To avoid this, you can call your fields one thing and ask the JSON outputter to rename the field when generating JSON. + +```ruby +FakerMaker.factory :vehicle do + wheels { 4 } + colour { Faker::Color.color_name } + engine_capacity(json: 'engineCapacity') { rand( 600..2500 ) } +end + +v = FM[:vehicle].build +v.engine_capacity = 125 +``` + +and calls to `as_json` and `to_json` will report the fieldname as `engineCapacity`. + +```ruby +v.to_json + +=> "{\"wheels\":4,\"colour\":\"blue\",\"engineCapacity\":125}" +``` + ### Building instances Instances are Plain Ol' Ruby Objects and the attributes are attached with getters and setters with their values assigned to the value return from their block at build time. To build an object: @@ -164,10 +189,18 @@ ```ruby result = FakerMaker[:item].build{ |i| i.name = 'Electric Sheep' } ``` +this is particularly useful for overriding nested values, since all the getters and setters of the embedded objects are already constructed: + +```ruby +result = FakerMaker[:basket].build do |b| + b.items.first.name = 'Neon Badger' +end +``` + if you're crazy enough to want to do both styles during creation, the values in the block will be preserved, e.g. ```ruby result = FakerMaker[:item].build( name: 'Electric Blanket' ) do |i| i.name = 'Electric Sheep' @@ -188,9 +221,45 @@ As another convenience, `FakerMaker` is also assigned to the variable `FM` to it is possible to write just: ```ruby result = FM[:basket].build +``` + +### Omitting fields + +Sometimes you want a field present, other times you don't. This is often the case when you want to skip fields which have null or empty values. + +```ruby +FakerMaker.factory :user do + name {'Patsy Stone'} + email(omit: :nil) {'patsy@fabulous.co.uk'} + admin {false} +end + +FM[:user].build.as_json +=> {:name=>"Patsy Stone", :email=>"patsy@fabulous.co.uk", :admin=>false} + +FM[:user].build(email: nil).as_json +=> {:name=>"Patsy Stone", :admin=>false} +``` + +The `omit` modifier can take a single value or an array. If it is passed a value and the attribute equals this value, it will not be included in the output from `as_json` (which returns a Ruby Hash) or in `to_json` methods. + +There are three special modifiers: + +* `:nil` (symbol) to omit output when the attribute is set to nil +* `:empty` to omit output when the value is an empty string, an empty array or an empty hash +* `:always` to never output this attibute. + +These can be mixed with real values, e.g. + +```ruby +FakerMaker.factory :user do + name {'Patsy Stone'} + email(omit: [:nil, :empty, 'test@foobar.com']) {'patsy@fabulous.co.uk'} + admin {false} +end ``` ### Embedding factories To use factories with factories, the following pattern is recommended: