module Filigree::TypedClass::ClassMethods

Public Class Methods

new(*vals) click to toggle source
# File lib/filigree/types.rb, line 102
def initialize(*vals)
        if self.class.typed_ivars.length != vals.length
                raise ArgumentError, "#{self.class.typed_ivars.length} arguments expected, #{vals.length} given."
        end

        self.set_typed_ivars(vals)
end

Public Instance Methods

default_constructor(strict = false) click to toggle source

Define the default constructor for the including class.

@param [Boolean] strict To use strict checking or not

@return [void]

# File lib/filigree/types.rb, line 99
def default_constructor(strict = false)
        class_eval do
                if strict
                        def initialize(*vals)
                                if self.class.typed_ivars.length != vals.length
                                        raise ArgumentError, "#{self.class.typed_ivars.length} arguments expected, #{vals.length} given."
                                end

                                self.set_typed_ivars(vals)
                        end
                else
                        def initialize(*vals)
                                self.set_typed_ivars(vals)
                        end
                end
        end
end
typed_ivar(name, type, nillable: false, strict: false) click to toggle source

Define a typed instance variable.

@param [Symbol] name Name of the accessor @param [Class] type Type of the accessor @param [Boolean] nillable If the value can be nil or not @param [Boolean] strict To use strict checking or not

@return [void]

# File lib/filigree/types.rb, line 141
def typed_ivar(name, type, nillable: false, strict: false)
        typed_ivars << name

        define_typed_accessor(name, nillable, strict, *
                type.is_a?(Array) ? [type.first, method(:check_array_type)] : [type, method(:check_type)])

        attr_reader name
end
typed_ivars() click to toggle source

Return the typed instance variables for an object.

@return [Array<Symbol>] Array of defined typed instance variables

# File lib/filigree/types.rb, line 153
def typed_ivars
        @typed_ivars ||= Array.new
end

Private Instance Methods

define_typed_accessor(name, nillable, strict, type, checker) click to toggle source

Define a new typed accessor.

@param [Symbol] name Name of the accessor @param [Boolean] nillable If the value can be nil or not @param [Boolean] strict To use strict checking or not @param [Class] type Type of the accessor @param [Proc] checker Method used to check the type

@return [void]

# File lib/filigree/types.rb, line 126
def define_typed_accessor(name, nillable, strict, type, checker)
        define_method "#{name}=" do |obj|
                self.instance_variable_set("@#{name}", checker.call(obj, type, blame: name, nillable: nillable, strict: strict))
        end
end