Class: Projects::ProjectService
- Inherits:
-
Object
- Object
- Projects::ProjectService
- Defined in:
- lib/projects/project_service.rb
Overview
Handles access to project metadata
Constant Summary
- @@append_to_list =
[]
- @@fetch_project =
[]
Instance Attribute Summary (collapse)
-
- (Object) env
Rack environment.
Class Method Summary (collapse)
-
+ (Object) append_to_list
Register a callback method to implement listing projects in your system.
-
+ (Object) fetch_project
Register a callback method that can fetch project details given a project id.
Instance Method Summary (collapse)
-
- (Object) fetch(id)
Iterate through our handlers and return the first one that actually has a project.
-
- (ProjectService) initialize(env: nil)
constructor
TODO how do we provide context information, via the env?.
-
- (Object) list(details: false, extension: nil)
Iterate through the append_to_list handlers and return all projects.
Constructor Details
- (ProjectService) initialize(env: nil)
TODO how do we provide context information, via the env?
16 17 18 |
# File 'lib/projects/project_service.rb', line 16 def initialize(env: nil) @env = env end |
Instance Attribute Details
- (Object) env
Rack environment
13 14 15 |
# File 'lib/projects/project_service.rb', line 13 def env @env end |
Class Method Details
+ (Object) append_to_list
Register a callback method to implement listing projects in your system.
Each callback takes three parameters:
-
details
[Boolean]: Iftrue
, you should include the entire project JSON. Iffalse
, you should only return an array of project IDs. -
extension
[String]: If set, we're only looking for projects that provide this extension. (If it's set, and you know you don't provide this extension, then return nil.) -
env
[Hash]: The current Rack environment
Example:
Projects::ProjectService.append_to_list << lambda do |details, extension, env|
return unless i_provide?(extension)
puts "S'up user #{env['AUTH_CREDENTIALS'].first}"
if details
go_get_projects(env)
else
go_get_project_ids(env)
end
end
47 48 49 |
# File 'lib/projects/project_service.rb', line 47 def self.append_to_list @@append_to_list end |
+ (Object) fetch_project
Register a callback method that can fetch project details given a project id.
Your callback will take two paramters:
-
id
[String] - The project ID -
env
[Hash] - The current rack environment
Example:
Projects::ProjectService.fetch_project << lambda do |id, env|
have_project?(id) ? get_project(id, env) : nil
end
74 75 76 |
# File 'lib/projects/project_service.rb', line 74 def self.fetch_project @@fetch_project end |
Instance Method Details
- (Object) fetch(id)
Iterate through our handlers and return the first one that actually has a project
79 80 81 82 83 84 85 |
# File 'lib/projects/project_service.rb', line 79 def fetch(id) for handler in @@fetch_project p = handler.call(id, env) return p if p end return nil end |
- (Object) list(details: false, extension: nil)
Iterate through the append_to_list handlers and return all projects
52 53 54 55 56 57 58 59 |
# File 'lib/projects/project_service.rb', line 52 def list(details: false, extension: nil) projects = [] @@append_to_list.each do |handler| to_append = handler.call(details, extension, env) projects.concat(to_append) if to_append end projects end |