Class: HackTree::ActionContext

Inherits:
Object
  • Object
show all
Defined in:
lib/hack_tree/action_context.rb

Overview

Context to execute the actions.

Instance Method Summary collapse

Constructor Details

#initialize(instance, parent = nil) ⇒ ActionContext

NOTE: No useless methods here, please.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hack_tree/action_context.rb', line 6

def initialize(instance, parent = nil)
  @instance, @parent = instance, parent

  # Suppress warnings.
  vrb, $VERBOSE = $VERBOSE, nil

  # Insert lookup routine for existing methods.
  (methods.map(&:to_s) - Node::FORBIDDEN_NAMES.map(&:to_s)).each do |method_name|
    next if not method_name =~ /\A(#{Node::NAME_REGEXP})\?{,1}\z/
    node_name = $1.to_sym
    instance_eval <<-EOT
      def #{method_name}(*args)
        if @instance.find_local_node(:#{node_name}, @parent)
          _dispatch(:#{method_name}, *args)
        else
          super
        end
      end
    EOT
  end

  # Restore warnings.
  $VERBOSE = vrb

  # Create direct methods. In case your console's completion is sane by itself, it will help.
  # IRB's default completion isn't sane yet (2012-02-25).
  @instance.nodes.select do |node|
    node.parent == @parent
  end.each do |node|
    # NOTE: This is slightly different from "existing method lookup routine" used above. Let's keep them separate.
    instance_eval <<-EOT
      def #{node.name}(*args)
        _dispatch(:#{node.name}, *args)
      end
    EOT
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



85
86
87
# File 'lib/hack_tree/action_context.rb', line 85

def method_missing(method_name, *args)
  _dispatch(method_name.to_sym, *args)
end

Instance Method Details

#inspectObject



44
45
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
# File 'lib/hack_tree/action_context.rb', line 44

def inspect
  # NOTES:
  #
  # * Exceptions raised here result in `(Object doesn't support #inspect)`. No other details are available, be careful.
  # * We don't return value from here, we **print** it directly.

  nodes = @instance.nodes.select {|node| node.parent == @parent}

  # Empty group?
  if nodes.empty?
    ::Kernel.puts "No groups/hacks here"
    return nil
  end

  # Not empty group, list contents.

  nodes = nodes.sort_by do |node|
    [
      node.is_a?(Node::Group) ? 0 : 1,    # Groups first.
      node.name.to_s,
    ]
  end

  # Compute name alignment width.
  names = nodes.map {|node| Tools.format_node_name(node)}
  name_align = Tools.compute_name_align(names, @instance.conf.local_name_align)

  nodes.each do |node|
    brief_desc = node.brief_desc || @instance.conf.brief_desc_stub

    fmt = "%-#{name_align}s%s"

    ::Kernel.puts(fmt % [
      Tools.format_node_name(node),
      brief_desc ? " # #{brief_desc}" : "",
    ])
  end # nodes.each

  nil
end