Class: KManager::Manager

Inherits:
Object
  • Object
show all
Includes:
KLog::Logging
Defined in:
lib/k_manager/manager.rb

Overview

KManager is designed to work with one or more areas of concern

TODO: Write Tests

Defined Under Namespace

Classes: Options, Show

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#active_uriObject

Returns the value of attribute active_uri.



10
11
12
# File 'lib/k_manager/manager.rb', line 10

def active_uri
  @active_uri
end

#current_resourceObject (readonly)

NOTE: rename current_resource to active_resource, focused_resource?



13
14
15
# File 'lib/k_manager/manager.rb', line 13

def current_resource
  @current_resource
end

Instance Method Details

#add_area(name, namespace: nil) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/k_manager/manager.rb', line 88

def add_area(name, namespace: nil)
  area = find_area(name)

  return area if area

  area = KManager::Area.new(manager: self, name: name, namespace: namespace)
  areas << area
  area
end

#area_documents(area: nil) ⇒ Object

List of documents for an area.

if area is nil, for the area matching the current_resource



84
85
86
# File 'lib/k_manager/manager.rb', line 84

def area_documents(area: nil)
  area_resources(area: area)&.flat_map(&:documents)
end

#area_resources(area: nil) ⇒ Object

List of resources for an area.

if area is nil, then return resources for the area matching the current_resource



73
74
75
76
77
78
79
# File 'lib/k_manager/manager.rb', line 73

def area_resources(area: nil)
  area = resolve_area(area)

  log.error 'Could not resolve area' if area.nil?

  area&.resources
end

#areasObject



66
67
68
# File 'lib/k_manager/manager.rb', line 66

def areas
  @areas ||= []
end

#bootObject



140
141
142
# File 'lib/k_manager/manager.rb', line 140

def boot
  KManager.fire_actions(:load_content, :register_document, :preload_document, :load_document)
end

#debug(*sections) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/k_manager/manager.rb', line 150

def debug(*sections)
  log.structure(KUtil.data.to_hash(opts))

  areas.each do |area|
    area.debug(*sections)
  end
end

#find_area(name) ⇒ Object



109
110
111
# File 'lib/k_manager/manager.rb', line 109

def find_area(name)
  areas.find { |a| a.name == name }
end

#find_document(tag, area: nil) ⇒ Object



98
99
100
# File 'lib/k_manager/manager.rb', line 98

def find_document(tag, area: nil)
  area_documents(area: area)&.find { |d| d.tag == tag }
end

#fire_actions(*actions) ⇒ Object



102
103
104
105
106
107
# File 'lib/k_manager/manager.rb', line 102

def fire_actions(*actions)
  areas.each do |area|
    # delegate
    area.resource_manager.fire_actions(*actions)
  end
end

#for_current_resource {|@current_resource| ... } ⇒ Object

Yields:

Raises:



27
28
29
30
31
# File 'lib/k_manager/manager.rb', line 27

def for_current_resource
  raise KManager::Error, 'Attempting to yield current_resource, when a different thread has the lock?' unless resource_mutex.owned?

  yield(@current_resource)
end

#for_resource(resource = nil) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/k_manager/manager.rb', line 19

def for_resource(resource = nil)
  resource_mutex.synchronize do
    @current_resource = resource
    yield(current_resource)
    @current_resource = nil
  end
end

#optsObject

Parameters:

  • sleep (Integer)

    Seconds to sleep after running, 0 = no sleep

  • exception_style (Symbol)

    Format for exception messages caught by watcher. :message - message only :short - message and short backtrace :long - message and long backtrace



62
63
64
# File 'lib/k_manager/manager.rb', line 62

def opts
  @opts ||= Options.new('', :message, false, 1, 0, Show.new(true, true, 'FINISHED :)'))
end

#rebootObject

Raises:

  • (SystemExit)


144
145
146
147
148
# File 'lib/k_manager/manager.rb', line 144

def reboot
  puts 'Fire reboot'
  KManager.opts.reboot_on_kill = 1
  raise SystemExit
end

#resolve_area(area) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/k_manager/manager.rb', line 113

def resolve_area(area)
  if area.nil?
    return current_resource.area if current_resource

    return areas.first
  end

  return area if area.is_a?(Area)

  find_area(area)
end

#resource_changed(uri, state) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/k_manager/manager.rb', line 132

def resource_changed(uri, state)
  @active_uri = uri
  areas.each do |area|
    area.resource_changed(uri, state)
  end
  @active_uri = nil
end

#resource_mutexObject



15
16
17
# File 'lib/k_manager/manager.rb', line 15

def resource_mutex
  @resource_mutex ||= Mutex.new
end

#resources_by_uri(uri) ⇒ Object

Return a list of resources for a URI.

Generally only one resource is returned, unless the URI exists in more than one area



128
129
130
# File 'lib/k_manager/manager.rb', line 128

def resources_by_uri(uri)
  areas.map { |area| area.resources_by_uri(uri) }.compact
end