Class: KManager::Resources::ResourceManager

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
KDoc::Guarded, KLog::Logging
Defined in:
lib/k_manager/resources/resource_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(area) ⇒ ResourceManager

Returns a new instance of ResourceManager.



44
45
46
47
48
49
50
51
52
53
# File 'lib/k_manager/resources/resource_manager.rb', line 44

def initialize(area)
  @area             = area

  @fileset          = KFileset::FileSet.new
  @webset           = nil # TODO: when ready to implement URL based resources
  @memset           = nil # TODO: when ready to implement dynamic memory resources

  @resource_factory = KManager::Resources::ResourceFactory.new
  @resource_set     = KManager::Resources::ResourceSet.new(area)
end

Instance Attribute Details

#areaObject

Returns the value of attribute area.



35
36
37
# File 'lib/k_manager/resources/resource_manager.rb', line 35

def area
  @area
end

#filesetObject

Returns the value of attribute fileset.



37
38
39
# File 'lib/k_manager/resources/resource_manager.rb', line 37

def fileset
  @fileset
end

#memsetObject

Returns the value of attribute memset.



39
40
41
# File 'lib/k_manager/resources/resource_manager.rb', line 39

def memset
  @memset
end

#resource_setObject

Resource set is built using file, web and memory resources



42
43
44
# File 'lib/k_manager/resources/resource_manager.rb', line 42

def resource_set
  @resource_set
end

#websetObject

Returns the value of attribute webset.



38
39
40
# File 'lib/k_manager/resources/resource_manager.rb', line 38

def webset
  @webset
end

Instance Method Details

#add_resource(resource_uri, **opts) ⇒ Object

Add a resource based on a resource_uri

TODO: Test File URI: relative_path TODO: Test File URI: absolute_path TODO: Test URI TODO: Test URI (Web) TODO: Test URI (File)

Parameters:

  • resource_uri (URI|String)

    Path to the resource, if the path uses file:/// then it will add a file resource, if the path http: or https: then a web resource will be added



124
125
126
# File 'lib/k_manager/resources/resource_manager.rb', line 124

def add_resource(resource_uri, **opts)
  resource_set.add(build_resource(resource_uri, **opts))
end

#add_resource_expand_path(file, **opts) ⇒ Object



112
113
114
# File 'lib/k_manager/resources/resource_manager.rb', line 112

def add_resource_expand_path(file, **opts)
  add_resource(File.expand_path(file), **opts)
end

#add_resourcesObject



134
135
136
137
# File 'lib/k_manager/resources/resource_manager.rb', line 134

def add_resources
  add_web_resources
  add_file_resources
end

#build_resource(resource_uri, **opts) ⇒ Object

TODO: Not sure if this is right



129
130
131
132
# File 'lib/k_manager/resources/resource_manager.rb', line 129

def build_resource(resource_uri, **opts)
  resource_uri = parse_uri(resource_uri)
  @resource_factory.instance(resource_uri, **opts)
end

#create_resource(resource_uri) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/k_manager/resources/resource_manager.rb', line 76

def create_resource(resource_uri)
  # TODO: skip if not whitelisted

  # warn if the resource already existed (this should not happen)
  # if fileset.whitelist
  # build resource
  # add resource to fileset
end

#debugObject



197
198
199
200
# File 'lib/k_manager/resources/resource_manager.rb', line 197

def debug
  resources.each(&:debug)
  nil
end

#delete_resource(resource_uri) ⇒ Object



105
106
107
108
109
110
# File 'lib/k_manager/resources/resource_manager.rb', line 105

def delete_resource(resource_uri)
  # TODO: skip if not whitelisted

  # find resource
  # if found, remove it
end

#find_document(tag) ⇒ Object



193
194
195
# File 'lib/k_manager/resources/resource_manager.rb', line 193

def find_document(tag)
  resources.flat_map(&:documents).find { |d| d.tag == tag }
end

#fire_actions(*actions) ⇒ Object

Fire actions against all resources in this manager.

Parameters:

  • actions (*Array<Symbol>)

    List of actions to run. [:load_content, :register_document, :load_document]



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

def fire_actions(*actions)
  guard_action(actions)

  load_content        if actions.include?(:load_content)
  register_documents  if actions.include?(:register_document)
  preload_documents   if actions.include?(:preload_document)
  load_documents      if actions.include?(:load_document)
end

#guard_action(actions) ⇒ Object



158
159
160
161
162
# File 'lib/k_manager/resources/resource_manager.rb', line 158

def guard_action(actions)
  actions.each do |action|
    warn "ResourceManager.fire_actions - unknown action: #{action}" unless KManager::Resources::BaseResource.valid_action?(action)
  end
end

#load_contentObject



164
165
166
167
168
# File 'lib/k_manager/resources/resource_manager.rb', line 164

def load_content
  resources.each do |resource|
    resource.fire_action(:load_content)
  end
end

#load_documentsObject



186
187
188
189
190
191
# File 'lib/k_manager/resources/resource_manager.rb', line 186

def load_documents
  # second pass will finalize any documents that were partially load due to dependencies
  resources.each do |resource|
    resource.fire_action(:load_document)
  end
end

#preload_documentsObject



176
177
178
179
180
181
182
183
184
# File 'lib/k_manager/resources/resource_manager.rb', line 176

def preload_documents
  # first pass will attempt to load every document, if a document has dependencies
  # it will be loaded in a second pass after dependencies are available
  resources.each do |resource|
    resource.fire_action(:preload_document)
  end

  attach_dependencies
end

#register_documentsObject



170
171
172
173
174
# File 'lib/k_manager/resources/resource_manager.rb', line 170

def register_documents
  resources.each do |resource|
    resource.fire_action(:register_document)
  end
end

#resource_changed(resource_uri, state) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/k_manager/resources/resource_manager.rb', line 65

def resource_changed(resource_uri, state)
  create_resource(resource_uri) if state == :created
  update_resource(resource_uri) if state == :updated
  delete_resource(resource_uri) if state == :deleted

  return if valid?

  log_any_messages
  clear_errors
end

#resource_modelsObject

def resources

resource_set.resources

end



61
62
63
# File 'lib/k_manager/resources/resource_manager.rb', line 61

def resource_models
  resource_set.resources.map(&:as_model)
end

#update_resource(resource_uri) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/k_manager/resources/resource_manager.rb', line 85

def update_resource(resource_uri)
  # TODO: skip if not whitelisted

  resource = resource_set.find_by_uri(resource_uri)

  return warn("Resource not in Resource Set - Skipping: #{resource_uri.path}") unless resource

  # TODO: Do I need to recreate, can I just reset instead?
  replace_resource = resource.recreate(resource)
  replace_resource.fire_action(:load_content)
  replace_resource.fire_action(:register_document)
  replace_resource.fire_action(:preload_document)
  replace_resource.fire_action(:load_document)
  resource_set.replace(replace_resource)

  # This is a bit of a hack, but it works for now
  # TODO: I don't think is actually working.
  attach_dependencies
end