Class: FunRuby::Container
- Inherits:
-
Object
- Object
- FunRuby::Container
- 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
- #define(key, &block) ⇒ Object
- #fetch(key) ⇒ Object
- #import(*aliases) ⇒ Object
-
#initialize ⇒ Container
constructor
A new instance of Container.
Constructor Details
#initialize ⇒ Container
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
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] = (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 = storage.fetch(key) value = storage[key].fetch(:value) return value unless value.equal?(NOT_EVALUATED) .fetch(:definition).().tap do |evaluated| storage[key][:value] = evaluated end end |