Passed
Push — master ( 444e7c...f23cdb )
by Michael
03:40
created

NoSECLI.expose_result()   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.512

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 9
ccs 1
cts 5
cp 0.2
crap 1.512
rs 9.6666
c 0
b 0
f 0
1
# frozen_string_literal: true
2
3 1
module NoSE
4 1
  module CLI
5
    # Start a pry console while preloading configured objects
6 1
    class NoSECLI < Thor
7 1
      desc 'console PLAN_FILE', 'open a pry console preconfigured with ' \
8
                                'variables from the given PLAN_FILE'
9
10 1
      long_desc <<-LONGDESC
11
        `nose console` gives a convenient way to perform manual exploration of
12
        generated plan data. It will load plans from the given file and then
13
        define a number of variables containing this data. This includes all
14
        instance variables in the `Search::Results` object as well as the
15
        `model` used to generate the results, the `options` loaded from the
16
        configuration file, and an instance of the configured `backend`.
17
      LONGDESC
18
19 1
      def console(plan_file)
20
        # Load the results from the plan file and define each as a variable
21 1
        result = load_results plan_file
22
        expose_result result
23
24
        # Also extract the model as a variable
25
        TOPLEVEL_BINDING.local_variable_set :model, result.workload.model
26
27
        # Load the options and backend as variables
28
        TOPLEVEL_BINDING.local_variable_set :options, options
29
        TOPLEVEL_BINDING.local_variable_set :backend,
30
                                            get_backend(options, result)
31
32
        TOPLEVEL_BINDING.pry
33
      end
34
35 1
      private
36
37
      # Expose the properties of the results object for use in the console
38
      # @return [void]
39 1
      def expose_result(result)
40
        exposed = result.instance_variables.map do |var|
41
          var[1..-1].to_sym
42
        end & result.methods
43
44
        exposed.each do |name|
45
          TOPLEVEL_BINDING.local_variable_set name, result.method(name).call
46
        end
47
      end
48
    end
49
  end
50
end
51