Module: Baltix::Loader::Certain

Includes:
Baltix::Log, Rake::DSL
Defined in:
lib/baltix/loader.rb

Constant Summary

Constants included from Baltix::Log

Baltix::Log::DEFAULT_IO_NAMES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Baltix::Log

default_prefix, io_name_parse, ios, level, #level_match, #log, prefix, prefix_for, setup, setup_kind

Instance Attribute Details

#object_hashObject (readonly)

Returns the value of attribute object_hash.



13
14
15
# File 'lib/baltix/loader.rb', line 13

def object_hash
  @object_hash
end

#object_idsObject (readonly)

Returns the value of attribute object_ids.



13
14
15
# File 'lib/baltix/loader.rb', line 13

def object_ids
  @object_ids
end

Instance Method Details

#load_file(file, type_hash) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/baltix/loader.rb', line 46

def load_file file, type_hash
   # NOTE this forces not to share namespace but avoid exception when calling
   # main space methods, see Rakefile of racc gem
   # also named module is required instead of anonymous one to allow root level defined methods access
   store_object_hash(type_hash)
   value = nil

   begin
      push
      Dir.chdir(File.dirname(file)) do
         _file = File.basename(file).untaint
         code = File.read(file, mode: 'r:UTF-8:-')
         code.untaint

         value =
            begin
               # evaluation required not to lost loaded object,
               # the instance_eval is used in favor of eval to avoid lost predefined vars
               # like for chef-utils gem
               instance_eval(code, _file)
            rescue Exception
               # thrown for setup gem
               load(File.basename(file), true)
            end
      end
   rescue Exception => e
      raise e
   ensure
      debug("value: #{value.inspect}")
      pop
      store_object_hash(type_hash)
   end

   self
rescue Exception => e
   debug("[#{e.class}]: #{e.message}\n\t#{e.backtrace.join("\n\t")}")

   self
end

#popObject



91
92
93
94
95
96
97
# File 'lib/baltix/loader.rb', line 91

def pop
   debug("Subtract paths: " + ($: - @paths).join("\n\t"))
   # NOTE this sequency of merging is required to correct loading libs leads
   # to show warning and break building rdoc documentation
   $:.replace(@paths | $:)
   debug("Replaced paths with: " + $:.join("\n\t"))
end

#pushObject



86
87
88
89
# File 'lib/baltix/loader.rb', line 86

def push
   @paths = $:.dup
   debug("Stored paths are: " + @paths.join("\n\t"))
end

#require(*args) ⇒ Object



17
18
19
20
21
# File 'lib/baltix/loader.rb', line 17

def require *args
   require_orig(*args)
rescue LoadError
   true
end

#require_origObject



15
# File 'lib/baltix/loader.rb', line 15

alias_method :require_orig, :require

#store_object_hash(type_hash) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/baltix/loader.rb', line 23

def store_object_hash type_hash
   object_hash =
      type_hash.map do |(klass, types)|
         objects = types.split(',').map do |type|
            begin
               ObjectSpace.each_object(type.constantize).map { |h| h }
            rescue NameError
               []
            end
         end.flatten

         [klass, objects]
      end.to_h

   if @object_hash
      @object_ids = object_hash.map do |(k, oh)|
         [k, oh.map {|h| h.__id__ } - @object_hash[k].map {|h| h.__id__ }]
      end.to_h
   end

   @object_hash = object_hash
end