Class: EDango::DI::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/edango/di/container.rb

Defined Under Namespace

Classes: TrapError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Container

Returns a new instance of Container.



34
35
36
37
38
39
# File 'lib/edango/di/container.rb', line 34

def initialize(&block)
  @services = {}
  @current_service = nil

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#servicesObject (readonly)

Returns the value of attribute services.



32
33
34
# File 'lib/edango/di/container.rb', line 32

def services
  @services
end

Instance Method Details

#[](name, option = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/edango/di/container.rb', line 87

def [](name, option = nil)
  result = nil

  service_name = name.intern()
  service = @services[service_name]

  if service
    result = service[:instance]
    if result.nil? or option == :init
      instance = service[:block].call()
      if instance
        result = service[:instance] =
          Proxy.new(instance, service[:interfaces])
      end
    end
  end

  option == :raw ? result.delegate : result
end

#asset(arg, options = {}, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/edango/di/container.rb', line 53

def asset(arg, options = {}, &block)
  name, instance = arg, nil

  if arg.is_a?(Hash)
    name, instance = arg.to_a().first
  end

  path = options[:file]
  if path
    instance = (YAML.load_file(path) rescue nil) || instance
  end

  if instance or block_given?
    service = find_service(name)

    service[:block]    = block
    service[:instance] = instance
    service[:file]     = path
  end
end

#interface(args, service_name = nil, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/edango/di/container.rb', line 74

def interface(args, service_name = nil, &block)
  service_name ||= @current_service

  if block_given?
    args = [args] unless args.is_a?(Array)

    args.each do |name|
      service = find_service(service_name)
      service[:interfaces][name.intern()] = block if service
    end
  end
end

#on_creation(&block) ⇒ Object



49
50
51
# File 'lib/edango/di/container.rb', line 49

def on_creation(&block)
  asset(@current_service, &block)
end

#service(name, &block) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/edango/di/container.rb', line 41

def service(name, &block)
  @current_service = name

  instance_eval(&block) if block_given?

  @current_service = nil
end

#update(arg, file = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/edango/di/container.rb', line 107

def update(arg, file = nil)
  services = if arg.is_a?(Symbol)
    if arg == :all
      file = nil; @services.values
    else
      [find_service(arg.intern())]
    end
  else
    [arg]
  end

  services.each do |service|
    serialize(service, file) if service
  end
end