Class: ServiceProvider::MethodDecorators::Requires

Inherits:
MethodDecorator
  • Object
show all
Defined in:
lib/service_provider/method_decorators/requires.rb

Instance Method Summary collapse

Constructor Details

#initialize(service_name) ⇒ Requires

Returns a new instance of Requires.



4
5
6
# File 'lib/service_provider/method_decorators/requires.rb', line 4

def initialize(service_name)
  @service_name = service_name
end

Instance Method Details

#add_service_getter(this) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/service_provider/method_decorators/requires.rb', line 23

def add_service_getter(this)
  service_name = @service_name
  this.class.send(:define_method, "#{service_name}") do 
    service = instance_variable_get("@#{service_name.to_sym}")
    unless service
      service = ServiceProvider.provider.get_service(service_name.to_sym)
      instance_variable_set("@#{service_name.to_sym}", service)
    end  
    service
  end
end

#add_service_setter(this) ⇒ Object



16
17
18
19
20
21
# File 'lib/service_provider/method_decorators/requires.rb', line 16

def add_service_setter(this)
  service_name = @service_name
  this.class.send(:define_method, "#{service_name}=") do |value|
    instance_variable_set("@#{service_name}", value)
  end
end

#call(orig, this, *args, &blk) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/service_provider/method_decorators/requires.rb', line 8

def call(orig, this, *args, &blk)
  orig.call(*args, &blk)
  this.instance_variable_set("@#{@service_name.to_sym}", ServiceProvider.provider.get_service(@service_name.to_sym))
  add_service_setter(this)
  add_service_getter(this)
  this
end