Class: ActiveProject::ResourceFactory
- Inherits:
-
Object
- Object
- ActiveProject::ResourceFactory
- Defined in:
- lib/active_project/resource_factory.rb
Overview
Factory class for creating and finding resource objects (Project, Issue, etc.) associated with a specific adapter.
Instance Method Summary collapse
-
#all(*args) ⇒ Array<BaseResource>
Fetches all resources of the associated type.
-
#all_async(*args) ⇒ Async::Task<BaseResource>
Fetches all resources in parallel, because patience is for people with no deadlines.
-
#build(attributes = {}) ⇒ BaseResource
Builds a new, unsaved resource instance.
-
#create(attributes = {}) ⇒ BaseResource
Builds and saves a new resource instance.
-
#find(id, context = {}) ⇒ BaseResource?
Finds a specific resource by its ID.
-
#first(*args) ⇒ BaseResource?
Fetches the first resource of the associated type.
-
#initialize(adapter:, resource_class:) ⇒ ResourceFactory
constructor
A new instance of ResourceFactory.
-
#where(conditions, *list_args) ⇒ Array<BaseResource>
Filters resources based on given conditions (client-side).
Constructor Details
#initialize(adapter:, resource_class:) ⇒ ResourceFactory
Returns a new instance of ResourceFactory.
9 10 11 12 |
# File 'lib/active_project/resource_factory.rb', line 9 def initialize(adapter:, resource_class:) @adapter = adapter @resource_class = resource_class end |
Instance Method Details
#all(*args) ⇒ Array<BaseResource>
Fetches all resources of the associated type. Delegates to the appropriate adapter list method.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/active_project/resource_factory.rb', line 18 def all(*args) list_method = determine_list_method = args.last.is_a?(Hash) ? args.pop : {} primary_arg = args.first # Call adapter method with appropriate arguments if primary_arg @adapter.send(list_method, primary_arg, ) elsif @adapter.method(list_method).arity.zero? # Handle case where list method might not take options (like list_projects) @adapter.send(list_method) else @adapter.send(list_method, ) end end |
#all_async(*args) ⇒ Async::Task<BaseResource>
Fetches all resources in parallel, because patience is for people with no deadlines. Wraps ‘#all` calls in async tasks for each provided ID.
38 39 40 41 42 43 |
# File 'lib/active_project/resource_factory.rb', line 38 def all_async(*args) ActiveProject::Async.run do |task| tasks = args.map { |id| task.async { all(id) } } tasks.flat_map(&:wait) end end |
#build(attributes = {}) ⇒ BaseResource
Builds a new, unsaved resource instance.
86 87 88 89 90 91 92 |
# File 'lib/active_project/resource_factory.rb', line 86 def build(attributes = {}) merged_attrs = attributes.merge( adapter_source: @adapter.class.name.split("::").last.sub("Adapter", "").downcase.to_sym, raw_data: attributes ) @resource_class.new(@adapter, merged_attrs) end |
#create(attributes = {}) ⇒ BaseResource
Builds and saves a new resource instance.
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/active_project/resource_factory.rb', line 98 def create(attributes = {}) # Determine the correct adapter create method based on resource type create_method = determine_create_method # NOTE: Assumes create methods on adapters take attributes hash directly # Context like project_id needs to be part of the attributes hash if required by adapter @adapter.send(create_method, attributes) # A full implementation would likely involve build then save: # resource = build(attributes) # resource.save # resource end |
#find(id, context = {}) ⇒ BaseResource?
Finds a specific resource by its ID. Delegates to the appropriate adapter find method.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/active_project/resource_factory.rb', line 50 def find(id, context = {}) find_method = determine_find_method # Pass context only if provided and the find method accepts it if !context.empty? @adapter.send(find_method, id, context) else @adapter.send(find_method, id) end rescue ActiveProject::NotFoundError nil # Return nil if the resource is not found by the adapter end |
#first(*args) ⇒ BaseResource?
Fetches the first resource of the associated type.
66 67 68 |
# File 'lib/active_project/resource_factory.rb', line 66 def first(*args) all(*args).first end |
#where(conditions, *list_args) ⇒ Array<BaseResource>
Filters resources based on given conditions (client-side).
74 75 76 77 78 79 80 81 |
# File 'lib/active_project/resource_factory.rb', line 74 def where(conditions, *list_args) resources = all(*list_args) resources.select do |resource| conditions.all? do |key, value| resource.respond_to?(key) && resource.send(key) == value end end end |