Class: Wonkavision::EventNamespace

Inherits:
EventPathSegment show all
Defined in:
lib/wonkavision/event_namespace.rb

Instance Attribute Summary collapse

Attributes inherited from EventPathSegment

#name, #options, #subscribers

Instance Method Summary collapse

Methods inherited from EventPathSegment

#notify_subscribers, #path, #root_namespace, #subscribe

Constructor Details

#initialize(name = nil, namespace = nil, opts = {}) ⇒ EventNamespace

Returns a new instance of EventNamespace.



7
8
9
10
# File 'lib/wonkavision/event_namespace.rb', line 7

def initialize(name=nil,namespace = nil,opts={})
  super name, namespace,opts
  @children=HashWithIndifferentAccess.new
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



5
6
7
# File 'lib/wonkavision/event_namespace.rb', line 5

def children
  @children
end

Instance Method Details

#event(*args) {|evt| ... } ⇒ Object

Yields:

  • (evt)


63
64
65
66
67
68
69
70
# File 'lib/wonkavision/event_namespace.rb', line 63

def event(*args)
  name, opts  = args.shift, args.extract_options!
  opts[:source_events] = (opts[:source_events] || []).concat(args) unless args.blank?
  evt = @children[name] || Wonkavision::Event.new(name,self,opts)
  yield evt if block_given?
  @children[evt.name] = evt
  evt
end

#find_matching_segments(event_path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/wonkavision/event_namespace.rb', line 31

def find_matching_segments (event_path)
  selected = {}
  @children.each_value do |child|
    if (child.is_a?(Wonkavision::Event))
      select_segment(child,selected) if child.matches(event_path)
    elsif (child.is_a?(Wonkavision::EventNamespace))
      selected.merge!(child.find_matching_segments(event_path))
    else
      raise "An unexpected child type was encountered in find_matching_events #{child.class}"
    end
  end
  #If no child was found, and the event matches this namespace, we should add ourselves to the list.
  #This is not necessary if any child event was located, because in that case event notifications
  #would bubble up to us anyway. If no event was found, there's nobody to blow the bubble but us.
  select_segment(self,selected) if selected.blank? && matches_event(event_path)
  selected
end

#find_or_create(path, final_segment_type = :event) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/wonkavision/event_namespace.rb', line 12

def find_or_create (path, final_segment_type = :event)
  if path.is_a?(Array)
    child_name = path.shift
    segment_type = path.blank? ? final_segment_type : :namespace
    child = @children[child_name] ||=  self.send(segment_type,child_name)
    return child if path.blank?
    raise "Events cannot have children. The path  you requested is not valid" if child.is_a?(Wonkavision::Event)
    child.find_or_create(path,final_segment_type)
  else
    path = Wonkavision.normalize_event_path(path)
    source_ns = self
    if (Wonkavision.is_absolute_path(path)) #indicates an absolute path, because it begins with a '/'
      source_ns = root_namespace
      path = path[1..-1]
    end
    source_ns.find_or_create(path.split(Wonkavision.event_path_separator), final_segment_type)
  end
end

#matches_event(event_path) ⇒ Object



49
50
51
# File 'lib/wonkavision/event_namespace.rb', line 49

def matches_event(event_path)
  Wonkavision.split(path) == Wonkavision.split(event_path).slice(0..-2)
end

#namespace(*args) {|ns| ... } ⇒ Object

dsl

Yields:

  • (ns)


54
55
56
57
58
59
60
61
# File 'lib/wonkavision/event_namespace.rb', line 54

def namespace(*args)
  return super if args.blank?
  name, opts = args.shift, (args.shift || {})
  ns = @children[name] || Wonkavision::EventNamespace.new(name,self,opts)
  yield ns if block_given?
  @children[ns.name] = ns
  ns
end