Module: Constant::Import

Defined in:
lib/constant/import.rb,
lib/constant/import/macro.rb

Defined Under Namespace

Modules: Macro

Constant Summary collapse

Error =
Class.new(RuntimeError)

Class Method Summary collapse

Class Method Details

.call(origin_constant, destination_constant, **kwargs) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/constant/import.rb', line 9

def self.call(origin_constant, destination_constant, **kwargs)
  alias_name = kwargs[:alias]

  if alias_name.nil? && destination_constant.ancestors.include?(origin_constant)
    raise Error, "#{destination_constant} already includes #{origin_constant}"
  end

  target = destination_constant

  if not alias_name.nil?
    target = Define.(alias_name, destination_constant)
  end

  inherit = false

  import_constant_names = origin_constant.constants(inherit)

  imported_constants = import_constant_names.map do |import_constant_name|
    import_constant = origin_constant.const_get(import_constant_name, inherit)
    target.const_set(import_constant_name, import_constant)
    import_constant
  end

  imported_constants
end

.included(base) ⇒ Object



5
6
7
# File 'lib/constant/import.rb', line 5

def self.included(base)
  base.extend(Macro)
end