Module: Fried::Schema::Struct::ClassMethods

Defined in:
lib/fried/schema/struct.rb

Constant Summary collapse

Noop =
-> { nil }

Instance Method Summary collapse

Instance Method Details

#attribute(name, type, default: Noop) ⇒ Symbol

Defines an attr_accessor with given name, type checking and optional default value

Parameters:

  • name (Symbol, String)

    name for attribute (attr_accessor)

  • type (::Fried::Typings::Type, Class, Module)

    either a ‘Type` from `Fried::Typings` (even a custom one) or any `Class` or `Module`

  • default (Object, Proc) (defaults to: Noop)

    if an object is passed, it will be used as default. If a Proc is passed, it will be evaluated during object initialization

Returns:

  • (Symbol)

    reader method name



35
36
37
38
39
40
41
# File 'lib/fried/schema/struct.rb', line 35

def attribute(name, type, default: Noop)
  schema = CreateDefinitionIfMissing.(self)
  definer = Attribute::Definition
  attribute_definition = definer.new(name, type, default)
  schema.add_attribute(attribute_definition)
  Attribute::DefineMethods.(attribute_definition, self)
end