Class: Loom::Mods::ActionProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/loom/mods/action_proxy.rb

Defined Under Namespace

Classes: ActionMap

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod, shell_api) ⇒ ActionProxy

Returns a new instance of ActionProxy.



6
7
8
9
10
# File 'lib/loom/mods/action_proxy.rb', line 6

def initialize(mod, shell_api)
  @mod = mod
  @shell_api = shell_api
  @nested_action_proxies = {}
end

Class Method Details

.install_action_map(action_map) ⇒ Object



29
30
31
32
# File 'lib/loom/mods/action_proxy.rb', line 29

def install_action_map(action_map)
  install_root_actions action_map
  install_namespace_action_proxies action_map
end

.install_namespace_action_proxies(action_map) ⇒ Object

This gets a bit tricky



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/loom/mods/action_proxy.rb', line 61

def install_namespace_action_proxies(action_map)
  action_map.ns_actionmaps.each do |ns, ns_action_map|
    @nested_action_proxy_klasses ||= {}
    @nested_action_proxy_klasses[self.hash] ||= {}
    @nested_action_proxy_klasses[self.hash][ns] ||=
      ActionProxy.subclass_for_action_map ns_action_map
    action_proxy_klass = @nested_action_proxy_klasses[self.hash][ns]

    define_method ns do
      @nested_action_proxies[ns] ||= action_proxy_klass.new @mod
    end
    Loom.log.debug2 self do
      "defined action proxy ns: #{ns}"
    end
  end
end

.install_root_actions(action_map) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/loom/mods/action_proxy.rb', line 34

def install_root_actions(action_map)
  action_map.action_tuples.each do |tuple|
    public_action_name = tuple[0]
    # TODO: What I've done here w/ bound_action_name (remapping methods
    # from a given name to a flattened namespace on the Mod object) is
    # very very strange. Just storing/binding/calling a Proc would be more
    # idiomatic.
    bound_action_name = tuple[1]

    define_method public_action_name do |*args, **kwargs, &block|
      # TODO[P0]: Effectively this is the API for all mods, but it's
      # burried here in the middle of nowhere. Add documentation - or make
      # it easier to read.
      Loom.log.debug(self) do
        "proxy to mod action: #{public_action_name} => #{bound_action_name}, #{@mod}"
      end

      @mod.send bound_action_name, *args, **kwargs, &block
    end
    Loom.log.debug2 self do
      "defined action proxy action: #{public_action_name} => #{bound_action_name}"
    end
  end
end

.new_action_mapObject



19
20
21
# File 'lib/loom/mods/action_proxy.rb', line 19

def new_action_map
  ActionMap.new
end

.subclass_for_action_map(action_map) ⇒ Object



23
24
25
26
27
# File 'lib/loom/mods/action_proxy.rb', line 23

def subclass_for_action_map(action_map)
  sub_class = Class.new ActionProxy
  sub_class.install_action_map action_map
  sub_class
end

Instance Method Details

#proxy_for_namespace(ns = nil) ⇒ Object



12
13
14
# File 'lib/loom/mods/action_proxy.rb', line 12

def proxy_for_namespace(ns=nil)
  ns.nil? ? self : @nested_action_proxies[ns]
end