Skip to content

Limitations

Dima Lashkov edited this page Jan 23, 2017 · 14 revisions

Reserved names

Using reserved method names new and initialize for additional constructor declaration will raise an error:

  konstructor
  def initialize # raises Konstructor::ReservedNameError
  end

or

  konstructor
  def new # raises Konstructor::ReservedNameError
  end

Declaring in Modules

Modules can't have konstructors. Use ActiveSupport::Concern and declare konstructor in included block.

module SomeModule
  extend ActiveSupport::Concern
    
  included do      
    konstructor :create
  end
  
  # konstructor declaration here would raise Konstructor::IncludeInModuleError
  def create
  end
end        

Declaring inherited methods

Methods inherited from superclasses can't become konstructors in subclasses. To achieve the effect, define a new method, mark it as konstructor and call the inherited one.

class SomeClass
  def create(val)
    @val = val
  end
  
  attr_reader :val
end

class SomeSubclass < SomeClass
  konstructor :create # will not work

  konstructor
  def complex_create(val1, val2)
    super(val1 + val2)
  end
end

obj = SomeSubclass.complex_create(2, 3)
obj.val # 5
Clone this wiki locally