Module: Fried::Dependency::Schema::ClassMethods

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

Constant Summary collapse

DefaultInit =
->(type) { type.new }

Instance Method Summary collapse

Instance Method Details

#dependency(name, klass, default = DefaultInit) ⇒ Symbol

Defines a dependency

Parameters:

  • name (Symbol)

    defines a public attr_accessor using given Symbol

  • klass (Class)

    defines the class to use in case the dependency is not provided in ‘initialize`. When it’s not provided, the default value of the dependency will be ‘klass.new`

  • default (Proc) (defaults to: DefaultInit)

    lambda which optionally accepts ‘type` and `obj`, which are the class of the dependency and the object being initialized. The returned value is used as default value when the dependency is not passed in the initializer. This lambda is optional, in which case the default value will just be `->(type) { type.new }`

Returns:

  • (Symbol)

    the value passed as ‘name`



38
39
40
41
42
43
44
# File 'lib/fried/dependency/schema.rb', line 38

def dependency(name, klass, default = DefaultInit)
  ns = ::Fried::Dependency
  definition = ns::CreateDefinitionIfMissing.(self)
  dep = ns::DependencyDefinition.new(name, klass, default)
  definition.add_dependency(dep)
  ns::DefineMethods.(dep, self)
end