Class: Injector

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

Defined Under Namespace

Classes: RuleNotFound

Instance Method Summary collapse

Constructor Details

#initialize(**params) ⇒ Injector

Returns a new instance of Injector.



7
8
9
10
11
12
13
14
# File 'lib/injector.rb', line 7

def initialize(**params)
  @rules = {}
  @injected = {injector: self}
  @callbacks = {}
  params.each do |k, v|
    set(k, v)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



136
137
138
139
# File 'lib/injector.rb', line 136

def method_missing(method, *args)
  super unless method.to_s.end_with? "="
  self.set(method[0...-1].to_sym, *args)
end

Instance Method Details

#get(key, overrides = {}) ⇒ Object Also known as: []



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/injector.rb', line 16

def get(key, overrides = {})
  # (lazily) evaluate rule, then store it
  @injected[key] ||= begin
    arr = @rules[key]
    if arr.nil?
      raise RuleNotFound,
        "No rule for #{key.inspect}. Available fields: #{@rules.keys.map(&:inspect).join(', ')}"
    end
    result = nil
    arr.to_a.reverse.each do |rule|
      rule.value.tap do |x|
        # if it's a callable value, call that and return its result, else return itself
        result = if x.is_a?(Proc) or x.is_a?(Method)
          inject(overrides, &x)
        else
          x
        end
      end
      unless result.nil?
        fire(:after_fetch, key, result, rule.identifier)
        break
      end
    end
    result
  end
end

#inject(overrides = {}, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/injector.rb', line 43

def inject(overrides = {}, &block)
  params = []
  keys = {}
  block.parameters.each_with_index do |(type, value), index|
    raise(
      ArgumentError,
      [
        "splash operator arguments are not permitted when injecting.",
        ":#{value} in #{ block.source_location }"
      ].join(" ")
    ) if type == :rest or type == :keyrest

    # fetch by index first! e.g. inject(0 => 40) { |x| x + 2 }
    result = overrides.fetch(index) do
      # then fetch by value
      overrides.fetch(value) do
        # Unfortunately, optional arguments are not possible, as
        # a Ruby block passed to `inject` will have exclusively optional arguments.
        # Instead, key arguments introduced in Ruby 2.0 should be used.
        if type == :key
          begin self.get(value)
          rescue RuleNotFound
          end
        else
          self.get(value)
        end
      end
    end
    if type == :key
      keys[value] = result
    else
      params << result
    end
  end

  if keys.empty?
    block.call(*params)
  else
    block.call(*params, keys)
  end
end

#insert(key, identifier = nil, block, **options) ⇒ Object Also known as: provide



96
97
98
# File 'lib/injector.rb', line 96

def insert(key, identifier = nil, block, **options)
  insert_rule(Inject::Rule.new(key, identifier, block, **options))
end

#insert_rule(rule) ⇒ Object



105
106
107
108
# File 'lib/injector.rb', line 105

def insert_rule(rule)
  (@rules[rule.key] ||= PQueue.new) << rule
  define_singleton_method(rule.key) { get(rule.key) }
end

#insert_rules(rules) ⇒ Object



101
102
103
# File 'lib/injector.rb', line 101

def insert_rules(rules)
  rules.each(&method(:insert_rule))
end

#invalidate(key, identifier = :all) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/injector.rb', line 110

def invalidate(key, identifier = :all)
  if queue = @rules[key]
    new_queue = case identifier
    when :all then []
    else
      queue.to_a.delete_if do |rule|
        rule.identifier == identifier
      end
    end
    queue.replace(new_queue)
    true
  end
end

#set(key, value) ⇒ Object Also known as: []=



85
86
87
# File 'lib/injector.rb', line 85

def set(key, value)
  insert(key, :setter, value, before: :all)
end

#when_fetched(key = nil, &block) ⇒ Object



92
93
94
# File 'lib/injector.rb', line 92

def when_fetched(key = nil, &block)
  add_callback(:after_fetch, key, &block)
end