Class: ApiMaker::BaseCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/api_maker/base_command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ability:, args:, collection:, commands:, command_response:, controller:) ⇒ BaseCommand

Returns a new instance of BaseCommand.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/api_maker/base_command.rb', line 10

def initialize(ability:, args:, collection:, commands:, command_response:, controller:)
  @api_maker_args = args
  @current_ability = ability
  @collection = collection
  @commands = commands
  @command_response = command_response
  @controller = controller

  # Make it possible to do custom preloads (useful in threadded mode that doesnt support Goldiloader)
  @collection = custom_collection(@collection) if respond_to?(:custom_collection)
end

Instance Attribute Details

#api_maker_argsObject (readonly)

Returns the value of attribute api_maker_args.



2
3
4
# File 'lib/api_maker/base_command.rb', line 2

def api_maker_args
  @api_maker_args
end

#collectionObject (readonly)

Returns the value of attribute collection.



2
3
4
# File 'lib/api_maker/base_command.rb', line 2

def collection
  @collection
end

#command_responseObject (readonly)

Returns the value of attribute command_response.



2
3
4
# File 'lib/api_maker/base_command.rb', line 2

def command_response
  @command_response
end

#commandsObject (readonly)

Returns the value of attribute commands.



2
3
4
# File 'lib/api_maker/base_command.rb', line 2

def commands
  @commands
end

#controllerObject (readonly)

Returns the value of attribute controller.



2
3
4
# File 'lib/api_maker/base_command.rb', line 2

def controller
  @controller
end

#current_abilityObject (readonly)

Returns the value of attribute current_ability.



2
3
4
# File 'lib/api_maker/base_command.rb', line 2

def current_ability
  @current_ability
end

Class Method Details

.execute_in_thread!(**args) ⇒ Object



22
23
24
25
26
# File 'lib/api_maker/base_command.rb', line 22

def self.execute_in_thread!(**args)
  args.fetch(:command_response).with_thread do
    new(**args).execute!
  end
end

.goldiloader?Boolean

Returns true if the gem “goldiloader” is present in the app

Returns:

  • (Boolean)


5
6
7
8
# File 'lib/api_maker/base_command.rb', line 5

def self.goldiloader?
  @goldiloader = Gem::Specification.find_all_by_name("goldiloader").any? if @goldiloader.nil?
  @goldiloader
end

Instance Method Details

#each_command(args = {}, &blk) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/api_maker/base_command.rb', line 28

def each_command(args = {}, &blk)
  if args[:threadded]
    # Goldiloader doesn't work with threads (loads all relationships for each thread)
    @collection = @collection.auto_include(false) if ApiMaker::BaseCommand.goldiloader?

    # Load relationship before commands so each command doesn't query on its own
    @collection.load
  end

  @commands.each do |command_id, command_data|
    if args[:threadded]
      command_response.with_thread do
        run_command(command_id, command_data, &blk)
      end
    else
      run_command(command_id, command_data, &blk)
    end
  end
end