Repository is archived
No commit activity in last 3 years
No release in over 3 years
Define initialize method that require specific parameters
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.16
~> 5.0
~> 10.0
~> 0.16
 Project Readme

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 account_exists?
    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
  • Usage
  • License

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 shun@getto.systems