Module: NamedImports
- Defined in:
- lib/named_imports.rb,
lib/named_imports/error.rb,
lib/named_imports/version.rb
Defined Under Namespace
Modules: Error
Constant Summary collapse
- VERSION =
"0.2.2"
- @@sandboxes =
{}
Class Method Summary collapse
- .from(raw_path, constant_names, context = Object) ⇒ Object
- .import(context = Object, &block) ⇒ Object
- .make_named_imports_available(in_context: Object) ⇒ Object
Class Method Details
.from(raw_path, constant_names, context = Object) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/named_imports.rb', line 10 def from(raw_path, constant_names, context = Object) into_path, import_line = caller_path_and_line from_path = full_path_for_import(from_path: raw_path, into_path: into_path) sandbox = sandbox_eval(file_path: from_path) load_constants(constant_names, from_sandbox: sandbox, into_context: context) end |
.import(context = Object, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/named_imports.rb', line 17 def import(context = Object, &block) into_path, import_line = caller_path_and_line constant_names = [] begin block.call rescue => e constant_name = constant_name_from_error(e) if constant_name.nil? raise NamedImports::Error::ImportBlockError.new(into_path, import_line, e) end if !constant_names.include?(constant_name) constant_names << constant_name context.const_set(constant_name, nil) end retry end constant_names end |
.make_named_imports_available(in_context: Object) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/named_imports.rb', line 41 def make_named_imports_available(in_context: Object) def_method = in_context == Object ? :define_method : :define_singleton_method in_context.send(def_method, :from) do |path, constants| NamedImports.from(path, constants, in_context) end in_context.send(def_method, :import) do |&block| NamedImports.import(in_context, &block) end end |