Class: Codependent::InjectableBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/codependent/injectable_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, type) ⇒ InjectableBuilder

Returns a new instance of InjectableBuilder.



16
17
18
19
20
21
22
# File 'lib/codependent/injectable_builder.rb', line 16

def initialize(id, type)
  @id = id
  @type = type
  @dependencies = []
  @state = {}
  @skip_checks = false
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



24
25
26
# File 'lib/codependent/injectable_builder.rb', line 24

def dependencies
  @dependencies
end

#idObject (readonly)

Returns the value of attribute id.



24
25
26
# File 'lib/codependent/injectable_builder.rb', line 24

def id
  @id
end

#resolverObject (readonly)

Returns the value of attribute resolver.



24
25
26
# File 'lib/codependent/injectable_builder.rb', line 24

def resolver
  @resolver
end

#stateObject (readonly)

Returns the value of attribute state.



24
25
26
# File 'lib/codependent/injectable_builder.rb', line 24

def state
  @state
end

#typeObject (readonly)

Returns the value of attribute type.



24
25
26
# File 'lib/codependent/injectable_builder.rb', line 24

def type
  @type
end

#validatorObject (readonly)

Returns the value of attribute validator.



24
25
26
# File 'lib/codependent/injectable_builder.rb', line 24

def validator
  @validator
end

Instance Method Details

#buildObject



61
62
63
64
65
66
67
# File 'lib/codependent/injectable_builder.rb', line 61

def build
  return unless @validator

  validate unless @skip_checks

  Injectable.new(@type, @dependencies, @state, @resolver)
end

#depends_on(*dependencies) ⇒ Object



57
58
59
# File 'lib/codependent/injectable_builder.rb', line 57

def depends_on(*dependencies)
  @dependencies.concat(dependencies)
end

#from_provider(&block) ⇒ Object



32
33
34
35
36
# File 'lib/codependent/injectable_builder.rb', line 32

def from_provider(&block)
  @state = { provider: block }
  @validator = Validators::ProviderValidator
  @resolver = Resolvers::ProviderResolver
end

#from_type(klass, additional_args = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/codependent/injectable_builder.rb', line 38

def from_type(klass, additional_args = nil)
  @state = {
    type: klass,
    additional_args: additional_args
  }

  @validator = Validators::ConstructorInjectionValidator
  @resolver = Resolvers::EagerTypeResolver
end

#from_value(value) ⇒ Object



26
27
28
29
30
# File 'lib/codependent/injectable_builder.rb', line 26

def from_value(value)
  @state = { value: value }
  @validator = Validators::ValueValidator
  @resolver = Resolvers::ValueResolver
end

#inject_settersObject



48
49
50
51
# File 'lib/codependent/injectable_builder.rb', line 48

def inject_setters
  @validator = Validators::SetterInjectionValidator
  @resolver = Resolvers::DeferredTypeResolver
end

#skip_checksObject



53
54
55
# File 'lib/codependent/injectable_builder.rb', line 53

def skip_checks
  @skip_checks = true
end