README.md in nxt_init-0.1.4 vs README.md in nxt_init-0.1.5

- old
+ new

@@ -1,5 +1,7 @@ +[![CircleCI](https://circleci.com/gh/nxt-insurance/nxt_init.svg?style=svg)](https://circleci.com/gh/nxt-insurance/nxt_init) + # NxtInit Create an initializer that accepts option arguments and define private readers for your arguments at the same time. @@ -19,42 +21,75 @@ $ gem install nxt_init ## Usage +NxtInit removes some boilerplate. Instead of writing your initializer and (private) attribute readers each and every time like so: + ```ruby -class MyService - include NxtInit - attr_init :one, - two: 'has a default', - three: nil, # makes the attribute optional - four: -> { "This is set on initialize: #{Time.now} - means it will not be evaluated multiple times" } - - def call - { - one: one, - two: two, - three: three, - four: four - } - end +class GetSafe + def initialize(frontend:, backend:) + @frontend = frontend + @backend = backend + end + + private + + attr_reader :frontend, :backend end +``` -my_service = MyService.new(one: 'this is required') -my_service.call +You can instead do the following: -# Will output the following: -{ +```ruby +class GetSafe + include NxtInit + attr_init :frontend, :backend +end - one: "this is required", - two: "has a default", - three: nil, - four: "This is evaluated on initialize: 2019-02-04 18:10:56 +0100 - means it will not be evaluated multiple times" -} +GetSafe.new # KeyError (NxtInit attr_init key :frontend was missing at initialization! +GetSafe.new(frontend: 'React', backend: 'Ruby on Rails') #<GetSafe:0x00007f81fb8506b8 @frontend="React", @backend="Ruby on Rails"> ``` -The attribute readers are private. If you need public accessors you have to add them yourself. That's all there is. -Check out the specs for examples how we handle inheritance. +### Optional arguments and defaults + +In order to provide default values you can simply use the hash syntax to define your defaults. +If you want to make an attribute optional, just pass nil as the default argument. +If there is no default value and you did not provide one when initializing your class, you will get a KeyError. + +```ruby +class GetSafe + include NxtInit + attr_init frontend: 'React', + backend: -> { 'Ruby on Rails' }, + middleware: nil +end + +GetSafe.new #<GetSafe:0x00007fab608e1918 @frontend="React", @backend="Ruby on Rails", @middleware=nil> +``` + +### Preprocessors + +If you want to preprocess your attribute somehow, you can define a preprocessor block to which the original attribute will be yielded. +Note that you can also call methods in your block if you have some heavier lifting to do. + +```ruby +class GetSafe + include NxtInit + attr_init date: -> (date) { date && (date.is_a?(Date) ? date : Date.parse(date)) } +end + +GetSafe.new(date: '2020/12/12').send(:date) # will give you the date +GetSafe.new(date: nil).send(:date) # would give you nil +GetSafe.new # would raise KeyError (NxtInit attr_init key :date was missing at initialization!) +``` + +Also you can still pass in nil if your block can handle it. If the attribute is not provided on initialization again a KeyError will be raised. + +### Inheritance + +When you inherit from a class that already includes NxtInit you can add further attributes to your subclass and overwrite existing options +simply by using attr_init for the same attributes. Check out the specs for more examples. ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.