Class: Rusty::CallbackBinding

Inherits:
Object
  • Object
show all
Defined in:
lib/rusty/callback_binding.rb

Overview

CallbackBinding objects are used to provide name lookup for callbacks. They are set up in such a way that unknown identifiers might traverse up the parents of a node to find a finally matching node. This allows the “top” node in the following callback to be refered by the name “top”, like this:

on "top p" { top.attribute = "p" }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ CallbackBinding

create an event scope which wraps a node scope.



27
28
29
# File 'lib/rusty/callback_binding.rb', line 27

def initialize(scope)
  @scope = scope
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

If the missing method is an identifier and has no arguments, this method looks in this node scope and its parent scopes for a scope with that name. If there is no such target scope, the message is forwarded to the node scope (which has its own set of magic, see Rusty::DX)



54
55
56
57
58
59
60
# File 'lib/rusty/callback_binding.rb', line 54

def method_missing(sym, *args, &block)
  target = if !block && args.empty? && sym =~ /^[A-Za-z_][A-Za-z_0-9]*$/
    up_scope!(sym.to_s)
  end

  (target || @scope).send sym, *args, &block
end

Class Method Details

.subclass_with_name_and_helpers(name, *helpers) ⇒ Object

Create a subclass with a given name and a set of helper modules.



17
18
19
20
21
22
23
24
# File 'lib/rusty/callback_binding.rb', line 17

def self.subclass_with_name_and_helpers(name, *helpers)
  name = name.split("::").last

  ::Class.new(self).tap do |klass|
    const_set name, klass
    klass.send :include, *helpers
  end
end

Instance Method Details

#callback(&block) ⇒ Object

callback: set a callback proc which will be called after a node is processed completely.



35
36
37
38
# File 'lib/rusty/callback_binding.rb', line 35

def callback(&block)
  @callback = block if block
  @callback
end

#skip!Object

skip!: allow to skip completely skip children.



41
42
43
# File 'lib/rusty/callback_binding.rb', line 41

def skip!
  @skip = true
end

#skip?Boolean

Should children be skipped?

Returns:

  • (Boolean)


46
47
48
# File 'lib/rusty/callback_binding.rb', line 46

def skip?
  !!@skip
end