Module: HackTree

Defined in:
lib/hack_tree.rb,
lib/hack_tree/node.rb,
lib/hack_tree/tools.rb,
lib/hack_tree/config.rb,
lib/hack_tree/instance.rb,
lib/hack_tree/node/base.rb,
lib/hack_tree/node/hack.rb,
lib/hack_tree/node/group.rb,
lib/hack_tree/dsl_context.rb,
lib/hack_tree/parser/base.rb,
lib/hack_tree/parser/desc.rb,
lib/hack_tree/action_context.rb

Defined Under Namespace

Modules: Node, Parser, Tools Classes: ActionContext, Config, DslContext, Instance

Constant Summary collapse

VERSION =
"0.1.0"
STD_HACKS =

Standard hacks bundled with the gem, their global names.

[
  "hack_tree.reload",
  "ls",
]

Class Method Summary collapse

Class Method Details

.clearObject

Clear everything. See Instance#clear.



19
20
21
# File 'lib/hack_tree.rb', line 19

def self.clear
  instance.clear
end

.clear_confObject

Clear config. See Instance#clear_conf.



24
25
26
# File 'lib/hack_tree.rb', line 24

def self.clear_conf
  instance.clear_conf
end

.clear_nodesObject

Clear group/hack definitions (“nodes” in general). See Instance#clear_nodes.



29
30
31
# File 'lib/hack_tree.rb', line 29

def self.clear_nodes
  instance.clear_nodes
end

.confObject

Get configuration object.

See also:

  • HackTree::Config

  • Instance#conf



39
40
41
# File 'lib/hack_tree.rb', line 39

def self.conf
  instance.conf
end

.define(&block) ⇒ Object

Define hacks.

HackTree.define do
  group :greeting do
    desc "Say hello"
    hack :hello do |*args|
      puts "Hello, %s!" % (args[0] || "world")
    end
  end
end


53
54
55
# File 'lib/hack_tree.rb', line 53

def self.define(&block)
  instance.define(&block)
end

.enable(method_name = :c, options = {}) ⇒ Object

Enable HackTree globally.

>> HackTree.enable
Greetings.
>> c
hello         # Say hello
>> c.hello
Hello, world!

Options:

:completion => T|F      # Enable completion enhancement. Default is true.
:with_std => [...]      # Load only these standard hacks.
:without_std => [...]   # Load all but these standard hacks.
:quiet => T|F           # Be quiet. Default is false.

Examples:

TODO.

Raises:

  • (ArgumentError)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/hack_tree.rb', line 76

def self.enable(method_name = :c, options = {})
  options = options.dup
  o = {}

  o[k = :completion] = (v = options.delete(k)).nil?? true : v
  o[k = :with_std] = options.delete(k)
  o[k = :without_std] = options.delete(k)
  o[k = :quiet] = (v = options.delete(k)).nil?? false : v

  raise ArgumentError, "Unknown option(s): #{options.inspect}" if not options.empty?

  if o[:with_std] and o[:without_std]
    # Exception is better than warning. It has a stack trace.
    raise ArgumentError, "Options `:with_std` and `:without_std` are mutually exclusive"
  end

  @enabled_as = method_name

  if not o[:quiet]
    # Print the banner before everything. If there are warnings, we'll know they are related to us somehow.
    ::Kernel.puts "Console hacks are available. Use `%s`, `%s.hack?`, `%s.hack [args]`" % ([@enabled_as]*3)
  end

  # NOTE: This can't be put into `Instance`, it's a name-based global.
  eval <<-EOT
    module ::Kernel
      private

      def #{method_name}
        ::HackTree.instance.action
      end
    end
  EOT

  # Install completion enhancement.
  if o[:completion]
    old_proc = Readline.completion_proc

    Readline.completion_proc = lambda do |input|
      candidates = instance.completion_logic(input, :enabled_as => @enabled_as)

      # NOTE: Block result.
      if candidates.is_a? Array
        candidates
      elsif old_proc
        # Pass control.
        old_proc.call(input)
      else
        # Nothing we can do.
        []
      end
    end # @completion_proc =
  end # if o[:completion]

  # Load standard hacks.

  global_names = if (ar = o[:with_std])
    # White list.
    STD_HACKS & ar.map(&:to_s)
  elsif (ar = o[:without_std])
    # Black list.
    STD_HACKS - ar.map(&:to_s)
  else
    # Default.
    STD_HACKS
  end

  global_names.each do |global_name|
    bn = global_name.gsub(".", "/") + ".rb"
    fn = File.join(File.dirname(__FILE__), "../hacks", bn)
    load fn
  end

  nil
end

.enabled_asObject



152
153
154
# File 'lib/hack_tree.rb', line 152

def self.enabled_as
  @enabled_as
end

.instanceObject



156
157
158
# File 'lib/hack_tree.rb', line 156

def self.instance
  @instance ||= Instance.new
end