getto-initialize_with

rubygems: getto-initialize_with

Define initialize method that require specific parameters

  • purpose : for type annotation
require "getto/initialize_with"

class MyClass
  include Getto::InitializeWith

  initialize_with(
    :time,

    # assert expire is a Integer
    expire: Integer,

    # assert repository respond to methods
    repository: [
      :account_exists?,
    ],
  )
end

class Repository
  def 
    true
  end
end


my_class = MyClass.new(
  time: :now,
  expire: 10,
  repository: Repository.new,
)
my_class.time       # => :now
my_class.expire     # => 10
my_class.repository # => Repository instance
Table of Contents

Requirements

  • developed on ruby: 2.5.1

Usage

require "getto/initialize_with"

class MyClass
  include Getto::InitializeWith

  initialize_with(
    :time,

    expire: Integer,

    repository: [
      :account_exists?,
    ],
  )
end

Errors

  • missing argument
# raise "argument missing" becouse missing `repository`
my_class = MyClass.new(time: :now, expire: 10)
  • unknown argument
# raise "unknown argument" because including `unknown`
my_class = MyClass.new(time: :now, expire: 10, repository: Repository.new, unknown: :argument)
  • type check failed
# raise "argument type error" because `expire` is not a Integer
my_class = MyClass.new(time: :now, expire: "10", repository: Repository.new)
  • object not satisfied signature
# raise "argument type error" because `repository` is not respond to [:account_exists?]
my_class = MyClass.new(time: :now, expire: 10, repository: Object.new)

Install

Add this line to your application's Gemfile:

gem 'getto-initialize_with'

And then execute:

$ bundle

Or install it yourself as:

$ gem install getto-initialize_with

License

getto/initialize_with is licensed under the MIT license.

Copyright © since 2018 [email protected]