Class: ActiveProject::ResourceFactory

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(adapter:, resource_class:) ⇒ ResourceFactory

Returns a new instance of ResourceFactory.

Parameters:

  • adapter (Adapters::Base)

    The adapter instance.

  • resource_class (Class)

    The resource class (e.g., Resources::Project).



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.

Parameters:

  • args (Array)

    Arguments to pass to the adapter’s list method (e.g., project_id for issues).

Returns:

  • (Array<BaseResource>)

    An array of resource objects.



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
  options = 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, options)
  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, options)
  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.

Parameters:

  • args (Array)

    List of IDs to fetch resources for.

Returns:

  • (Async::Task<BaseResource>)

    Flattened array of results from async fetches.



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.

Parameters:

  • attributes (Hash) (defaults to: {})

    Attributes for the new resource.

Returns:

  • (BaseResource)

    A new instance of the resource class.



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.

Parameters:

  • attributes (Hash) (defaults to: {})

    Attributes for the new resource.

Returns:

  • (BaseResource)

    The created resource object.

Raises:



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.

Parameters:

  • id (String, Integer)

    The ID or key of the resource.

  • context (Hash) (defaults to: {})

    Optional context needed by some find methods (e.g., :project_id for Basecamp issues).

Returns:

  • (BaseResource, nil)

    The found resource object or nil.



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.

Parameters:

  • args (Array)

    Arguments to pass to the adapter’s list method.

Returns:

  • (BaseResource, nil)

    The first resource object found or nil.



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).

Parameters:

  • conditions (Hash)

    Conditions to filter by.

  • list_args (Array)

    Arguments for the underlying #all call.

Returns:

  • (Array<BaseResource>)

    Matching resources.



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