Sha256: 49e4f90dd2cef671a42b02bbf33f3bb5906ead76d9b388375c65d177291834f0

Contents?: true

Size: 1.02 KB

Versions: 15

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module WebConsole
  # A context lets you get object names related to the current session binding.
  class Context
    def initialize(binding)
      @binding = binding
    end

    # Extracts entire objects which can be called by the current session unless
    # the inputs is present.
    #
    # Otherwise, it extracts methods and constants of the object specified by
    # the input.
    def extract(input = nil)
      input.present? ? local(input) : global
    end

    private

      GLOBAL_OBJECTS = [
        "instance_variables",
        "local_variables",
        "methods",
        "class_variables",
        "Object.constants",
        "global_variables"
      ]

      def global
        GLOBAL_OBJECTS.map { |cmd| eval(cmd) }
      end

      def local(input)
        [
          eval("#{input}.methods").map { |m| "#{input}.#{m}" },
          eval("#{input}.constants").map { |c| "#{input}::#{c}" },
        ]
      end

      def eval(cmd)
        @binding.eval(cmd) rescue []
      end
  end
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/web-console-4.2.1/lib/web_console/context.rb
web-console-4.2.1 lib/web_console/context.rb
web-console-4.2.0 lib/web_console/context.rb
web-console-4.1.0 lib/web_console/context.rb
web-console-4.0.4 lib/web_console/context.rb
web-console-4.0.3 lib/web_console/context.rb
web-console-4.0.2 lib/web_console/context.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/web-console-4.0.1/lib/web_console/context.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/web-console-4.0.1/lib/web_console/context.rb
web-console-4.0.1 lib/web_console/context.rb
web-console-4.0.0 lib/web_console/context.rb
web-console-3.7.0 lib/web_console/context.rb
web-console-3.6.2 lib/web_console/context.rb
web-console-3.6.1 lib/web_console/context.rb
web-console-3.6.0 lib/web_console/context.rb