All Projects → nxt-insurance → nxt_init

nxt-insurance / nxt_init

Licence: MIT license
Define initializers and attribute readers for your arguments with a single line of code

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to nxt init

Rails Devise Graphql
A Rails 6 boilerplate to create your next Saas product. Preloaded with graphQL, devise, JWT, CanCanCan, RailsAdmin, Rubocop, Rspec, i18n and more.
Stars: ✭ 199 (+847.62%)
Mutual labels:  ruby-on-rails
Stream Rails
Rails Client - Build Activity Feeds & Streams with GetStream.io
Stars: ✭ 250 (+1090.48%)
Mutual labels:  ruby-on-rails
Idatav
大屏数据可视化 Big screen data visualization demo
Stars: ✭ 3,913 (+18533.33%)
Mutual labels:  sugar
Still life
Rails upgrade's best friend
Stars: ✭ 213 (+914.29%)
Mutual labels:  ruby-on-rails
Octopus
Database Sharding for ActiveRecord
Stars: ✭ 2,496 (+11785.71%)
Mutual labels:  ruby-on-rails
SuperPuperDuperLayout
Super puper duper mega easy awesome wrapper over auto layout!!111!!1!!!1!!!11111!!!1!!
Stars: ✭ 14 (-33.33%)
Mutual labels:  sugar
Enhanced Rails Architecture
A set of good architectural patterns beyond the pure Ruby on Rails architecture.
Stars: ✭ 185 (+780.95%)
Mutual labels:  ruby-on-rails
handbook
📙 ErgoServ Developer's Handbook - a collection of guides, recipes, and scripts for helping you get things done, better.
Stars: ✭ 46 (+119.05%)
Mutual labels:  ruby-on-rails
Vueonrails
💎 Rails gem with the power of Vue.js components
Stars: ✭ 250 (+1090.48%)
Mutual labels:  ruby-on-rails
EZAnchor
An easier and faster way to code Autolayout
Stars: ✭ 25 (+19.05%)
Mutual labels:  sugar
Inject Some Sql
Have fun injecting SQL into a Ruby on Rails application!
Stars: ✭ 211 (+904.76%)
Mutual labels:  ruby-on-rails
Filestack Rails
Official Ruby on Rails plugin for Filestack File Picker that makes it easy to add powerful file uploading and transformation capabilities to any web or mobile application.
Stars: ✭ 220 (+947.62%)
Mutual labels:  ruby-on-rails
SugarDockerized
Sugar Dockerized (Sugar application not included. Download at www.sugarcrm.com)
Stars: ✭ 63 (+200%)
Mutual labels:  sugar
Acts as indexed
Acts As Indexed is a plugin which provides a pain-free way to add fulltext search to your Ruby on Rails app
Stars: ✭ 211 (+904.76%)
Mutual labels:  ruby-on-rails
Tinyconstraints
Nothing but sugar.
Stars: ✭ 3,721 (+17619.05%)
Mutual labels:  sugar
Email Dashboard
📪 An interactive emailing management service with scheduling, templating, tracking and A/B testing.
Stars: ✭ 194 (+823.81%)
Mutual labels:  ruby-on-rails
sugar
Declarative HTTP client for Golang
Stars: ✭ 25 (+19.05%)
Mutual labels:  sugar
projects
Türk geliştiricelerin açık kaynak Ruby projeleri
Stars: ✭ 13 (-38.1%)
Mutual labels:  ruby-on-rails
Sugar
A Javascript library for working with native objects.
Stars: ✭ 4,457 (+21123.81%)
Mutual labels:  sugar
sugaroid
The not-that intelligent, but cute Artificially Intelligent bot, created when I was bored.
Stars: ✭ 12 (-42.86%)
Mutual labels:  sugar

CircleCI

NxtInit

Create an initializer that accepts option arguments and define private readers for your arguments at the same time.

Installation

Add this line to your application's Gemfile:

gem 'nxt_init'

And then execute:

$ bundle

Or install it yourself as:

$ gem install nxt_init

Usage

NxtInit removes some boilerplate. Instead of writing your initializer and (private) attribute readers each and every time like so:

class GetSafe
  def initialize(frontend:, backend:)
    @frontend = frontend
    @backend = backend
  end
  
  private 
  
  attr_reader :frontend, :backend
end

You can instead do the following:

class GetSafe
  include NxtInit
  attr_init :frontend, :backend
end

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">

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.

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>

BEWARE of Global Defaults

class GetSafe
  include NxtInit
  attr_init frontend: [], # this is global and shared between all classes
            backend: -> { [] } # this is local to an instance of the class
end

GetSafe.new.send(:frontend).object_id => 70236117045360
GetSafe.new.send(:frontend).object_id => 70236117045360

GetSafe.new.send(:backend).object_id => 70236143937240
GetSafe.new.send(:backend).object_id => 70236121600680

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.

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.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/nxt-insurance/nxt_init.

License

The gem is available as open source under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].