Class: FunRuby::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/fun_ruby/container.rb,
lib/fun_ruby/container/mixin.rb,
lib/fun_ruby/container/define.rb,
lib/fun_ruby/container/resolve.rb

Defined Under Namespace

Modules: Mixin Classes: Define, Resolve

Constant Summary collapse

NAMESPACE_SEPARATOR =
"."
NOT_EVALUATED =
Object.new.freeze

Instance Method Summary collapse

Constructor Details

#initializeContainer

Returns a new instance of Container.



12
13
14
15
# File 'lib/fun_ruby/container.rb', line 12

def initialize
  @storage = {}
  @mutex = Mutex.new
end

Instance Method Details

#define(key, &block) ⇒ Object

Raises:

  • (KeyError)


18
19
20
21
22
23
24
25
# File 'lib/fun_ruby/container.rb', line 18

def define(key, &block)
  key = key.to_s

  raise KeyError, "#{key.inspect} is already defined" if storage.key?(key)
  raise TypeError, "block should be given" unless block_given?

  storage[key] = init_meta(block)
end

#fetch(key) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/fun_ruby/container.rb', line 28

def fetch(key)
  key = key.to_s
  meta = storage.fetch(key)
  value = storage[key].fetch(:value)
  return value unless value.equal?(NOT_EVALUATED)

  meta.fetch(:definition).().tap do |evaluated|
    storage[key][:value] = evaluated
  end
end

#import(*aliases) ⇒ Object



40
41
42
# File 'lib/fun_ruby/container.rb', line 40

def import(*aliases)
  Mixin.build(aliases: aliases)
end