Class: FunRuby::Container::Define

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(container:, namespaces:, resolve:) ⇒ Define

Returns a new instance of Define.



32
33
34
35
36
# File 'lib/fun_ruby/container/define.rb', line 32

def initialize(container:, namespaces:, resolve:)
  @container = container
  @namespaces = namespaces
  @resolve = resolve
end

Class Method Details

.build(container: FunRuby.container, namespaces: []) ⇒ Object

Raises:

  • (TypeError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fun_ruby/container/define.rb', line 13

def self.build(container: FunRuby.container, namespaces: [])
  raise TypeError, "namespaces: should be an array" unless namespaces.is_a?(::Array)
  raise TypeError, "container: should be an instance of #{Container.name}" unless container.is_a?(Container)

  namespaces = namespaces.map(&:to_s)
  resolve = Resolve.build(
    container: container,
    aliases: (0...namespaces.size).reduce([]) do |combos, index|
      [namespaces[0..index].join(NAMESPACE_SEPARATOR), *combos]
    end
  )
  new(
    container: container,
    namespaces: namespaces,
    resolve: resolve
  )
end

Instance Method Details

#call(&block) ⇒ Object



46
47
48
# File 'lib/fun_ruby/container/define.rb', line 46

def call(&block)
  instance_exec(&block)
end

#function(key, &block) ⇒ Object Also known as: f



51
52
53
54
55
56
57
58
# File 'lib/fun_ruby/container/define.rb', line 51

def function(key, &block)
  if block.nil?
    resolve.(key)
  else
    full_key = [*namespaces, key].join(NAMESPACE_SEPARATOR)
    container.define(full_key, &block)
  end
end

#namespace(namespace, &block) ⇒ Object



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

def namespace(namespace, &block)
  self.class.build(container: container, namespaces: [*namespaces, namespace]).tap do |define|
    define.instance_exec(&block)
  end
end