Class: ApiMaker::CommandSpecHelper

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

Defined Under Namespace

Classes: AddedCommand

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command:, collection: nil, controller: nil) ⇒ CommandSpecHelper

Returns a new instance of CommandSpecHelper.



4
5
6
7
8
9
# File 'lib/api_maker/command_spec_helper.rb', line 4

def initialize(command:, collection: nil, controller: nil)
  @collection = collection
  @command_class = command
  @commands = {}
  @controller = controller || double
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



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

def collection
  @collection
end

#command_classObject (readonly)

Returns the value of attribute command_class.



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

def command_class
  @command_class
end

#commandsObject (readonly)

Returns the value of attribute commands.



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

def commands
  @commands
end

Instance Method Details

#add_command(args: {}, primary_key: nil) ⇒ Object



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

def add_command(args: {}, primary_key: nil)
  id = commands.length + 1

  commands[id] = {
    args: ActionController::Parameters.new(args),
    id: id,
    primary_key: primary_key
  }

  AddedCommand.new(id, response)
end

#collection_instanceObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/api_maker/command_spec_helper.rb', line 52

def collection_instance
  @collection_instance ||= if command_class.const_defined?(:CollectionInstance)
    command_class.const_get(:CollectionInstance).new(
      ability: controller.__send__(:current_ability),
      api_maker_args: controller.__send__(:api_maker_args),
      collection: collection,
      commands: commands,
      command_response: response,
      controller: controller
    )
  end
end

#commandObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/api_maker/command_spec_helper.rb', line 23

def command
  @command ||= begin
    raise "No commands have been added" if commands.empty?

    command_id = commands.keys.first
    command_data = commands.values.first

    individual_command = ApiMaker::IndividualCommand.new(
      args: ApiMaker::Deserializer.execute!(arg: command_data[:args]),
      collection: collection,
      command: self,
      id: command_id,
      primary_key: command_data[:primary_key],
      response: response
    )

    command_class.new(
      ability: controller.__send__(:current_ability),
      api_maker_args: controller.__send__(:api_maker_args),
      collection: collection,
      collection_instance: collection_instance,
      command: individual_command,
      commands: commands,
      command_response: response,
      controller: controller
    )
  end
end

#controllerObject



65
66
67
# File 'lib/api_maker/command_spec_helper.rb', line 65

def controller
  @controller ||= instance_double("ApplicationController", current_user: user)
end

#execute!Object



69
70
71
# File 'lib/api_maker/command_spec_helper.rb', line 69

def execute!
  command.execute_with_response
end

#responseObject



73
74
75
# File 'lib/api_maker/command_spec_helper.rb', line 73

def response
  @response ||= ApiMaker::CommandResponse.new(controller: @controller)
end