Class: Reality::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/reality/command_line.rb

Overview

This module contains services used from bin/reality, you typically don't need to think of it.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommandLine

Returns a new instance of CommandLine.



10
11
12
# File 'lib/reality/command_line.rb', line 10

def initialize
  parse_arguments
end

Instance Attribute Details

#articleObject

Returns the value of attribute article.



8
9
10
# File 'lib/reality/command_line.rb', line 8

def article
  @article
end

#commandsObject

Returns the value of attribute commands.



8
9
10
# File 'lib/reality/command_line.rb', line 8

def commands
  @commands
end

#errorsObject

Returns the value of attribute errors.



8
9
10
# File 'lib/reality/command_line.rb', line 8

def errors
  @errors
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/reality/command_line.rb', line 8

def options
  @options
end

#search_termObject

Returns the value of attribute search_term.



8
9
10
# File 'lib/reality/command_line.rb', line 8

def search_term
  @search_term
end

Class Method Details

.display_usageObject



14
15
16
# File 'lib/reality/command_line.rb', line 14

def self.display_usage
  puts "usage: reality \"[search-string]\" [entity-command] [chained-command(s) ...]"
end

Instance Method Details

#article_found?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/reality/command_line.rb', line 22

def article_found?
  !self.article.nil?
end

#interactive?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/reality/command_line.rb', line 18

def interactive?
  !!self.options[:interactive]
end

#resultsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/reality/command_line.rb', line 36

def results
  return "Nothing found for: #{self.search_term}" unless self.article_found?
  puts "Attempting entity chain: #{self.commands} on #{self.article.inspect}" if ENV['DEBUG']

  result = subcommand_for(self.article, self.commands.shift)

  while subcommand = self.commands.shift
    result = subcommand_for(result, subcommand)
  end

  if self.errors.any?
    "Error: #{ self.errors.join("\n") }"
  else
    result
  end
end

#run_interactive_shellObject



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
# File 'lib/reality/command_line.rb', line 53

def run_interactive_shell
  require 'irb'
  require 'reality/shortcuts'
  ::ARGV.clear

  # FIXME: can't see better means to have everything accessible & included
  #IRB::ExtendCommandBundle.include(Reality)
  TOPLEVEL_BINDING.receiver.send(:include, Reality)

  IRB.setup nil

  IRB.conf[:IRB_NAME] = 'reality'

  IRB.conf[:PROMPT] = {}
  IRB.conf[:PROMPT][:REALITY] = {
    :PROMPT_I => '%N:%03n:%i> ',
    :PROMPT_S => '%N:%03n:%i%l ',
    :PROMPT_C => '%N:%03n:%i* ',
    :RETURN => "# => %s\n"
  }
  IRB.conf[:PROMPT_MODE] = :REALITY
  IRB.conf[:RC] = false

  IRB.conf[:MAIN_CONTEXT] = IRB::Irb.new.context

  require 'irb/ext/multi-irb'
  IRB.irb nil, TOPLEVEL_BINDING
end

#subcommand_for(object, subcommand) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/reality/command_line.rb', line 26

def subcommand_for(object, subcommand)
  if object.respond_to?(subcommand.to_sym)
    puts "Calling #{subcommand} on #{object.inspect}" if ENV['DEBUG']
    object.send(subcommand.to_sym)
  else
    self.errors << "#{object} doesn't respond to #{subcommand}"
    nil
  end
end